About this deal
TypeError: unbound method holidays() must be called with TradingCalendar instance as first argument (got datetime instance instead) YESTERDAY = (datetime.today() - timedelta(max(1,(TODAY.weekday() + 6) % 7 - 3))) - 1 * US_BUSINESS_DAY In [481]: pd . Timestamp ( .....: datetime . datetime ( 2019 , 10 , 27 , 1 , 30 , 0 , 0 ), .....: tz = "dateutil/Europe/London" , .....: fold = 0 , .....: ) .....: Out[481]: Timestamp('2019-10-27 01:30:00+0100', tz='dateutil//usr/share/zoneinfo/Europe/London') In [482]: pd . Timestamp ( .....: year = 2019 , .....: month = 10 , .....: day = 27 , .....: hour = 1 , .....: minute = 30 , .....: tz = "dateutil/Europe/London" , .....: fold = 1 , .....: ) .....: Out[482]: Timestamp('2019-10-27 01:30:00+0000', tz='dateutil//usr/share/zoneinfo/Europe/London') Ambiguous times when localizing # In [485]: rng_hourly . tz_localize ( "US/Eastern" , ambiguous = "infer" ) Out[485]: DatetimeIndex(['2011-11-06 00:00:00-04:00', '2011-11-06 01:00:00-04:00', '2011-11-06 01:00:00-05:00', '2011-11-06 02:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) In [486]: rng_hourly . tz_localize ( "US/Eastern" , ambiguous = "NaT" ) Out[486]: DatetimeIndex(['2011-11-06 00:00:00-04:00', 'NaT', 'NaT', '2011-11-06 02:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) In [487]: rng_hourly . tz_localize ( "US/Eastern" , ambiguous = [ True , True , False , False ]) Out[487]: DatetimeIndex(['2011-11-06 00:00:00-04:00', '2011-11-06 01:00:00-04:00', '2011-11-06 01:00:00-05:00', '2011-11-06 02:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) Nonexistent times when localizing # Cell In [ 484 ], line 1 ----> 1 rng_hourly . tz_localize ( 'US/Eastern' ) File ~/work/pandas/pandas/pandas/core/indexes/datetimes.py:291, in DatetimeIndex.tz_localize (self, tz, ambiguous, nonexistent) 284 @doc ( DatetimeArray . tz_localize ) 285 def tz_localize ( 286 self , ( ... ) 289 nonexistent : TimeNonexistent = "raise" , 290 ) -> Self : --> 291 arr = self . _data . tz_localize ( tz , ambiguous , nonexistent ) 292 return type ( self ) . _simple_new ( arr , name = self . name ) File ~/work/pandas/pandas/pandas/core/arrays/_mixins.py:80, in ravel_compat.
In [84]: pd . date_range ( start , end , freq = "BM" ) Out[84]: DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29', '2011-05-31', '2011-06-30', '2011-07-29', '2011-08-31', '2011-09-30', '2011-10-31', '2011-11-30', '2011-12-30'], dtype='datetime64[ns]', freq='BM') In [85]: pd . date_range ( start , end , freq = "W" ) Out[85]: DatetimeIndex(['2011-01-02', '2011-01-09', '2011-01-16', '2011-01-23', '2011-01-30', '2011-02-06', '2011-02-13', '2011-02-20', '2011-02-27', '2011-03-06', '2011-03-13', '2011-03-20', '2011-03-27', '2011-04-03', '2011-04-10', '2011-04-17', '2011-04-24', '2011-05-01', '2011-05-08', '2011-05-15', '2011-05-22', '2011-05-29', '2011-06-05', '2011-06-12', '2011-06-19', '2011-06-26', '2011-07-03', '2011-07-10', '2011-07-17', '2011-07-24', '2011-07-31', '2011-08-07', '2011-08-14', '2011-08-21', '2011-08-28', '2011-09-04', '2011-09-11', '2011-09-18', '2011-09-25', '2011-10-02', '2011-10-09', '2011-10-16', '2011-10-23', '2011-10-30', '2011-11-06', '2011-11-13', '2011-11-20', '2011-11-27', '2011-12-04', '2011-12-11', '2011-12-18', '2011-12-25', '2012-01-01'], dtype='datetime64[ns]', freq='W-SUN') In [86]: pd . bdate_range ( end = end , periods = 20 ) Out[86]: DatetimeIndex(['2011-12-05', '2011-12-06', '2011-12-07', '2011-12-08', '2011-12-09', '2011-12-12', '2011-12-13', '2011-12-14', '2011-12-15', '2011-12-16', '2011-12-19', '2011-12-20', '2011-12-21', '2011-12-22', '2011-12-23', '2011-12-26', '2011-12-27', '2011-12-28', '2011-12-29', '2011-12-30'], dtype='datetime64[ns]', freq='B') In [87]: pd . bdate_range ( start = start , periods = 20 ) Out[87]: DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07', '2011-01-10', '2011-01-11', '2011-01-12', '2011-01-13', '2011-01-14', '2011-01-17', '2011-01-18', '2011-01-19', '2011-01-20', '2011-01-21', '2011-01-24', '2011-01-25', '2011-01-26', '2011-01-27', '2011-01-28'], dtype='datetime64[ns]', freq='B') whenever the dob is greater than now. You may want to subtract a few years to now in the condition df['dob'] < now since it may be slightly more likely to have a 101 year old worker than a 1 year old worker... In [218]: bh = pd . offsets . BusinessHour ( start = "17:00" , end = "09:00" ) In [219]: bh Out[219]:
In [242]: pd . date_range ( start , periods = 10 , freq = "2h20min" ) Out[242]: DatetimeIndex(['2011-01-01 00:00:00', '2011-01-01 02:20:00', '2011-01-01 04:40:00', '2011-01-01 07:00:00', '2011-01-01 09:20:00', '2011-01-01 11:40:00', '2011-01-01 14:00:00', '2011-01-01 16:20:00', '2011-01-01 18:40:00', '2011-01-01 21:00:00'], dtype='datetime64[ns]', freq='140T') In [243]: pd . date_range ( start , periods = 10 , freq = "1D10U" ) Out[243]: DatetimeIndex([ '2011-01-01 00:00:00', '2011-01-02 00:00:00.000010', '2011-01-03 00:00:00.000020', '2011-01-04 00:00:00.000030', '2011-01-05 00:00:00.000040', '2011-01-06 00:00:00.000050', '2011-01-07 00:00:00.000060', '2011-01-08 00:00:00.000070', '2011-01-09 00:00:00.000080', '2011-01-10 00:00:00.000090'], dtype='datetime64[ns]', freq='86400000010U') Anchored offsets # Moreover, I also want to mark the days around the third Fridays with different values, e.g. day +1 after third_friday is 1 and day+2 is 2. To do that, I wrote a second for loop. Here the full example: for beg in pd.bdate_range("2000-01-01", "2017-05-01"): into freq keyword arguments. The available date offsets and associated frequency strings can be found below: In [113]: dft2 = pd . DataFrame ( .....: np . random . randn ( 20 , 1 ), .....: columns = [ "A" ], .....: index = pd . MultiIndex . from_product ( .....: [ pd . date_range ( "20130101" , periods = 10 , freq = "12H" ), [ "a" , "b" ]] .....: ), .....: ) .....: In [114]: dft2 Out[114]: A 2013-01-01 00:00:00 a -0.298694 b 0.823553 2013-01-01 12:00:00 a 0.943285 b -1.479399 2013-01-02 00:00:00 a -1.643342 ... ... 2013-01-04 12:00:00 b 0.069036 2013-01-05 00:00:00 a 0.122297 b 1.422060 2013-01-05 12:00:00 a 0.370079 b 1.016331 [20 rows x 1 columns] In [115]: dft2 . loc [ "2013-01-05" ] Out[115]: A 2013-01-05 00:00:00 a 0.122297 b 1.422060 2013-01-05 12:00:00 a 0.370079 b 1.016331 In [116]: idx = pd . IndexSlice In [117]: dft2 = dft2 . swaplevel ( 0 , 1 ) . sort_index () In [118]: dft2 . loc [ idx [:, "2013-01-05" ], :] Out[118]: A a 2013-01-05 00:00:00 0.122297 2013-01-05 12:00:00 0.370079 b 2013-01-05 00:00:00 1.422060 2013-01-05 12:00:00 1.016331Cell In [ 489 ], line 1 ----> 1 dti . tz_localize ( 'Europe/Warsaw' ) File ~/work/pandas/pandas/pandas/core/indexes/datetimes.py:291, in DatetimeIndex.tz_localize (self, tz, ambiguous, nonexistent) 284 @doc ( DatetimeArray . tz_localize ) 285 def tz_localize ( 286 self , ( ... ) 289 nonexistent : TimeNonexistent = "raise" , 290 ) -> Self : --> 291 arr = self . _data . tz_localize ( tz , ambiguous , nonexistent ) 292 return type ( self ) . _simple_new ( arr , name = self . name ) File ~/work/pandas/pandas/pandas/core/arrays/_mixins.py:80, in ravel_compat.
This is because one day’s business hour end is equal to next day’s business hour start. For example,In [82]: pd . date_range ( start , periods = 1000 , freq = "M" ) Out[82]: DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-30', '2011-05-31', '2011-06-30', '2011-07-31', '2011-08-31', '2011-09-30', '2011-10-31', ... '2093-07-31', '2093-08-31', '2093-09-30', '2093-10-31', '2093-11-30', '2093-12-31', '2094-01-31', '2094-02-28', '2094-03-31', '2094-04-30'], dtype='datetime64[ns]', length=1000, freq='M') In [83]: pd . bdate_range ( start , periods = 250 , freq = "BQS" ) Out[83]: DatetimeIndex(['2011-01-03', '2011-04-01', '2011-07-01', '2011-10-03', '2012-01-02', '2012-04-02', '2012-07-02', '2012-10-01', '2013-01-01', '2013-04-01', ... '2071-01-01', '2071-04-01', '2071-07-01', '2071-10-01', '2072-01-01', '2072-04-01', '2072-07-01', '2072-10-03', '2073-01-02', '2073-04-03'], dtype='datetime64[ns]', length=250, freq='BQS-JAN') If it helps, I had a similar need for exchange trading calendars. There was some excellent code buried in the Zipline project by Quantopian. I extracted out the relevant part and created a new project for creating market exchange trading calendars in pandas. The links are here, with some of the functionality described below. is similar to a Timedelta that represents a duration of time but follows specific calendar duration rules. I'm trying to create a Trading calendar using Pandas. I'm able to create a cal instance based on the USFederalHolidayCalendar. The USFederalHolidayCalendar is not consistent with the Trading calendar in that the Trading calendar doesn't include Columbus Day and Veteran's Day. However, the Trading calendar includes Good Friday (not included in the USFederalHolidayCalendar). Everything except for the last line in following code works: from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory, GoodFriday
BUSINESS_DATE = "[Previous (" + DAY_NAME[YESTERDAY.weekday()] + "):'" + YESTERDAY.strftime('%y%m%d') This particular day contains a day light savings time transition In [146]: ts = pd . Timestamp ( "2016-10-30 00:00:00" , tz = "Europe/Helsinki" ) # Respects absolute time In [147]: ts + pd . Timedelta ( days = 1 ) Out[147]: Timestamp('2016-10-30 23:00:00+0200', tz='Europe/Helsinki') # Respects calendar time In [148]: ts + pd . DateOffset ( days = 1 ) Out[148]: Timestamp('2016-10-31 00:00:00+0200', tz='Europe/Helsinki') In [149]: friday = pd . Timestamp ( "2018-01-05" ) In [150]: friday . day_name () Out[150]: 'Friday' # Add 2 business days (Friday --> Tuesday) In [151]: two_business_days = 2 * pd . offsets . BDay () In [152]: friday + two_business_days Out[152]: Timestamp('2018-01-09 00:00:00') In [153]: ( friday + two_business_days ) . day_name () Out[153]: 'Tuesday' In [260]: from pandas.tseries.holiday import ( .....: Holiday , .....: USMemorialDay , .....: AbstractHolidayCalendar , .....: nearest_workday , .....: MO , .....: ) .....: In [261]: class ExampleCalendar ( AbstractHolidayCalendar ): .....: rules = [ .....: USMemorialDay , .....: Holiday ( "July 4th" , month = 7 , day = 4 , observance = nearest_workday ), .....: Holiday ( .....: "Columbus Day" , .....: month = 10 , .....: day = 1 , .....: offset = pd . DateOffset ( weekday = MO ( 2 )), .....: ), .....: ] .....: In [262]: cal = ExampleCalendar () In [263]: cal . holidays ( datetime . datetime ( 2012 , 1 , 1 ), datetime . datetime ( 2012 , 12 , 31 )) Out[263]: DatetimeIndex(['2012-05-28', '2012-07-04', '2012-10-08'], dtype='datetime64[ns]', freq=None) hint :
In [396]: ps [ "2011" ] Out[396]: 2011-01 -2.916901 2011-02 0.514474 2011-03 1.346470 2011-04 0.816397 2011-05 2.258648 2011-06 0.494789 2011-07 0.301239 2011-08 0.464776 2011-09 -1.393581 2011-10 0.056780 2011-11 0.197035 2011-12 2.261385 Freq: M, dtype: float64 In [397]: dfp = pd . DataFrame ( .....: np . random . randn ( 600 , 1 ), .....: columns = [ "A" ], .....: index = pd . period_range ( "2013-01-01 9:00" , periods = 600 , freq = "T" ), .....: ) .....: In [398]: dfp Out[398]: A 2013-01-01 09:00 -0.538468 2013-01-01 09:01 -1.365819 2013-01-01 09:02 -0.969051 2013-01-01 09:03 -0.331152 2013-01-01 09:04 -0.245334 ... ... 2013-01-01 18:55 0.522460 2013-01-01 18:56 0.118710 2013-01-01 18:57 0.167517 2013-01-01 18:58 0.922883 2013-01-01 18:59 1.721104 [600 rows x 1 columns] In [399]: dfp . loc [ "2013-01-01 10H" ] Out[399]: A 2013-01-01 10:00 -0.308975 2013-01-01 10:01 0.542520 2013-01-01 10:02 1.061068 2013-01-01 10:03 0.754005 2013-01-01 10:04 0.352933 ... ... 2013-01-01 10:55 -0.865621 2013-01-01 10:56 -1.167818 2013-01-01 10:57 -2.081748 2013-01-01 10:58 -0.527146 2013-01-01 10:59 0.802298 [60 rows x 1 columns] The tradingCal instance seems to work in that I'm able to view the Holiday rules. In[10]: tradingCal.rules In [256]: pd . Timestamp ( "2014-01-02" ) + pd . offsets . MonthBegin ( n = 0 ) Out[256]: Timestamp('2014-02-01 00:00:00') In [257]: pd . Timestamp ( "2014-01-02" ) + pd . offsets . MonthEnd ( n = 0 ) Out[257]: Timestamp('2014-01-31 00:00:00') In [258]: pd . Timestamp ( "2014-01-01" ) + pd . offsets . MonthBegin ( n = 0 ) Out[258]: Timestamp('2014-01-01 00:00:00') In [259]: pd . Timestamp ( "2014-01-31" ) + pd . offsets . MonthEnd ( n = 0 ) Out[259]: Timestamp('2014-01-31 00:00:00') Holidays / holiday calendars # If you just want to get the pandas Holiday Calendar that can be used in other pandas functions that take that as an argument: holidays = nyse.holidays() the next business hour start or previous day’s end. Different from other offsets, BusinessHour.rollforward