How to fix DatetimeIndex.union_many is deprecated warning in Python

Viewed 232

I have the following code:

import datetime, pytz
import pandas_market_calendars as mcal

today_date = datetime.datetime.now(tz=pytz.timezone('US/Eastern'))
start_date = datetime.date(today_date.year - 5, today_date.month, today_date.day)

end_date = datetime.date(today_date.year + 2, today_date.month, today_date.day + 5)

nyse_calender = mcal.get_calendar('NYSE')
nyse_business_days = nyse_calender.schedule(start_date=start_date, end_date=end_date)

When I run this, I get the following warning:

 FutureWarning: DatetimeIndex.union_many is deprecated and will be removed in a future version. Use obj.union instead.   
  nyse_business_days = nyse_calender.schedule(start_date=start_date, end_date=end_date)

How am I supposed to solve this problem? The warning doesn't really help to take an action..

2 Answers

I met the same problem, just saw this one when I am about to raising mine. Here I provide couple more details of my debugging:

pandas version 1.4.0

pandas_market_calendars version 3.3(it also happens on version3.2)

the warning comes from line:

    nyse_business_days = nyse_calender.schedule(start_date=start_date, end_date=end_date)

I did a further debugging into pandas_market_calendars, it was actually from market_calendar.py line 514, which looks like:

        for market_time in market_times:
            temp = self.days_at_time(_all_days, market_time) # standard times
            if _adj_col:
                # mark here, according with Yunfei's debugging, it orients from here
                # create an array of special times
                special = self.special_dates(market_time, start_date, end_date, filter_holidays= False)

from my side, the inputs to the function are like:

market_time
'market_open'
start_date
Timestamp('2021-07-05 00:00:00')
end_date
Timestamp('2021-07-25 00:00:00')

as the line 514 is actually running from loop, my console shows thousands of such 'futurewarning', crying for help

A PR for pandas_market_calendar was merged a couple of days ago which fixes this issue.

Related