About this deal
In [143]: idx = pd . date_range ( start = "2019-12-29" , freq = "D" , periods = 4 ) In [144]: idx . isocalendar () Out[144]: year week day 2019-12-29 2019 52 7 2019-12-30 2020 1 1 2019-12-31 2020 1 2 2020-01-01 2020 1 3 In [145]: idx . to_series () . dt . isocalendar () Out[145]: year week day 2019-12-29 2019 52 7 2019-12-30 2020 1 1 2019-12-31 2020 1 2 2020-01-01 2020 1 3 DateOffset objects #
In [234]: bhour_mon = pd . offsets . CustomBusinessHour ( start = "10:00" , weekmask = "Tue Wed Thu Fri" ) # Monday is skipped because it's a holiday, business hour starts from 10:00 In [235]: dt + bhour_mon * 2 Out[235]: Timestamp('2014-01-21 10:00:00') Offset aliases # 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.
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() 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 #
For time series data, it’s conventional to represent the time component in the index of a Series or DataFrame
In [106]: dft = pd . DataFrame ( .....: np . random . randn ( 100000 , 1 ), .....: columns = [ "A" ], .....: index = pd . date_range ( "20130101" , periods = 100000 , freq = "T" ), .....: ) .....: In [107]: dft Out[107]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-03-11 10:35:00 -0.747967 2013-03-11 10:36:00 -0.034523 2013-03-11 10:37:00 -0.201754 2013-03-11 10:38:00 -1.509067 2013-03-11 10:39:00 -1.693043 [100000 rows x 1 columns] In [108]: dft . loc [ "2013" ] Out[108]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-03-11 10:35:00 -0.747967 2013-03-11 10:36:00 -0.034523 2013-03-11 10:37:00 -0.201754 2013-03-11 10:38:00 -1.509067 2013-03-11 10:39:00 -1.693043 [100000 rows x 1 columns]
Holiday: Dr. Martin Luther King Jr. (month=1, day=1, offset=