About this deal
In [476]: didx = pd . date_range ( start = "2014-08-01 09:00" , freq = "H" , periods = 3 , tz = "US/Eastern" ) In [477]: didx Out[477]: DatetimeIndex(['2014-08-01 09:00:00-04:00', '2014-08-01 10:00:00-04:00', '2014-08-01 11:00:00-04:00'], dtype='datetime64[ns, US/Eastern]', freq='H') In [478]: didx . tz_localize ( None ) Out[478]: DatetimeIndex(['2014-08-01 09:00:00', '2014-08-01 10:00:00', '2014-08-01 11:00:00'], dtype='datetime64[ns]', freq=None) In [479]: didx . tz_convert ( None ) Out[479]: DatetimeIndex(['2014-08-01 13:00:00', '2014-08-01 14:00:00', '2014-08-01 15:00:00'], dtype='datetime64[ns]', freq='H') # tz_convert(None) is identical to tz_convert('UTC').tz_localize(None) In [480]: didx . tz_convert ( "UTC" ) . tz_localize ( None ) Out[480]: DatetimeIndex(['2014-08-01 13:00:00', '2014-08-01 14:00:00', '2014-08-01 15:00:00'], dtype='datetime64[ns]', freq=None) Fold # Finally, there is the option of writing rules for calculating public holidays (as these are usually carried over to the next working day). 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
ts = pd . Timestamp ( 2022 , 12 , 9 , 15 ) >>> ts + pd . offsets . BusinessDay ( normalize = True ) Timestamp('2022-12-12 00:00:00') The CustomBusinessDay class has now been merged into the upcoming 0.12 release of Pandas where you will be able to do something like the following: >>> from pandas.tseries.offsets import CustomBusinessDay Perhaps it is more straightforward to create the trade calendar from scratch, like so: import datetime as dt In [275]: from pandas.tseries.holiday import get_calendar , HolidayCalendarFactory , USLaborDay In [276]: cal = get_calendar ( "ExampleCalendar" ) In [277]: cal . rules Out[277]: [Holiday: Memorial Day (month=5, day=31, offset=
into freq keyword arguments. The available date offsets and associated frequency strings can be found below: Holiday: New Years Day (month=1, day=1, observance=
See Iterating through groups or Resampler.__iter__ for more. Use origin or offset to adjust the start of the bins # Is there an easy fix so that I can replace the "D" with something else or do I have to rewrite everything? command refers to 20 days INCLUDING weekends, but I want it to refer to 20 WEEKDAYS; e.g. something like this: df["window"].loc[beg: beg + pd.to_timedelta(20, "Weekdays_only")] = 2 pandas.bdate_range # pandas. bdate_range ( start = None, end = None, periods = None, freq = 'B', tz = None, normalize = True, name = None, weekmask = None, holidays = None, inclusive = 'both', ** kwargs ) [source] # However, if we want more accuracy, we must consider bank holidays (for example, if we calculate costs that depend on the exact days between coupon and coupon, 1 day off in 20 is a 5% error). Knowing the holidays within a period is especially useful when estimating human habits and behaviors (medical care, travels, etc.). In the insurance sector, these patterns can directly affect accounting reserves; for example, when calculating costs incurred but not reported (IBNR). In fact, in some insurance companies it’s common to slightly increase the claims ratio in leap years due to having one calendar day more than the rest.In [13]: friday = pd . Timestamp ( "2018-01-05" ) In [14]: friday . day_name () Out[14]: 'Friday' # Add 1 day In [15]: saturday = friday + pd . Timedelta ( "1 day" ) In [16]: saturday . day_name () Out[16]: 'Saturday' # Add 1 business day (Friday --> Monday) In [17]: monday = friday + pd . offsets . BDay () In [18]: monday . day_name () Out[18]: 'Monday' Cell 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.
Here is what it can do by creating a pandas DatetimeIndex of all of the valid open hours for the NYSE: import pandas_market_calendars as mcalIn [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 # In [142]: ts2 . iloc [[ 0 , 2 , 6 ]] . index Out[142]: DatetimeIndex(['2011-01-02', '2011-01-16', '2011-02-13'], dtype='datetime64[ns]', freq=None) Time/date components # In [330]: small = pd . Series ( .....: range ( 6 ), .....: index = pd . to_datetime ( .....: [ .....: "2017-01-01T00:00:00" , .....: "2017-01-01T00:30:00" , .....: "2017-01-01T00:31:00" , .....: "2017-01-01T01:00:00" , .....: "2017-01-01T03:00:00" , .....: "2017-01-01T03:05:00" , .....: ] .....: ), .....: ) .....: In [331]: resampled = small . resample ( "H" ) In [332]: for name , group in resampled : .....: print ( "Group: " , name ) .....: print ( "-" * 27 ) .....: print ( group , end = " \n\n " ) .....: Group: 2017-01-01 00:00:00 --------------------------- 2017 - 01 - 01 00 : 00 : 00 0 2017 - 01 - 01 00 : 30 : 00 1 2017 - 01 - 01 00 : 31 : 00 2 dtype: int64 In [264]: pd . date_range ( .....: start = "7/1/2012" , end = "7/10/2012" , freq = pd . offsets . CDay ( calendar = cal ) .....: ) . to_pydatetime () .....: Out[264]: array([datetime.datetime(2012, 7, 2, 0, 0), datetime.datetime(2012, 7, 3, 0, 0), datetime.datetime(2012, 7, 5, 0, 0), datetime.datetime(2012, 7, 6, 0, 0), datetime.datetime(2012, 7, 9, 0, 0), datetime.datetime(2012, 7, 10, 0, 0)], dtype=object) In [265]: offset = pd . offsets . CustomBusinessDay ( calendar = cal ) In [266]: datetime . datetime ( 2012 , 5 , 25 ) + offset Out[266]: Timestamp('2012-05-29 00:00:00') In [267]: datetime . datetime ( 2012 , 7 , 3 ) + offset Out[267]: Timestamp('2012-07-05 00:00:00') In [268]: datetime . datetime ( 2012 , 7 , 3 ) + 2 * offset Out[268]: Timestamp('2012-07-06 00:00:00') In [269]: datetime . datetime ( 2012 , 7 , 6 ) + offset Out[269]: Timestamp('2012-07-09 00:00:00')
YESTERDAY = (datetime.today() - timedelta(max(1,(TODAY.weekday() + 6) % 7 - 3))) - 1 * US_BUSINESS_DAY When I try to list the holidays in a date range, I get the following error: In[11]: tradingCal.holidays(datetime(2014, 12, 31), datetime(2016, 12, 31))
When I am writing this answer, today is Friday in USA so next business day shall be Monday, in the meantime yesterday is thanksgiving holiday so previous business day should be Wednesday the next business hour start or previous day’s end. Different from other offsets, BusinessHour.rollforward In [218]: bh = pd . offsets . BusinessHour ( start = "17:00" , end = "09:00" ) In [219]: bh Out[219]: