def test_days_at_time_open(): cal = NYSEExchangeCalendar() # check if market_open before/after 1985 is correct valid = cal.valid_days("1984-12-30", "1985-01-03") at_open = cal.days_at_time(valid, "market_open") assert_index_equal( at_open, pd.DatetimeIndex([ '1984-12-31 10:00:00', '1985-01-02 09:30:00', '1985-01-03 09:30:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC")) # check if it is rounded valid = cal.valid_days("1901-12-13", "1901-12-16") at_open = cal.days_at_time(valid, "market_open") assert_index_equal( at_open, pd.DatetimeIndex([ '1901-12-13 10:00:00', '1901-12-14 10:00:00', '1901-12-16 10:00:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC")) # check if chosen time is kept cal = NYSEExchangeCalendar(open_time=dt.time(9)) at_open = cal.days_at_time(valid, "market_open") assert_index_equal( at_open, pd.DatetimeIndex([ '1901-12-13 09:00:00', '1901-12-14 09:00:00', '1901-12-16 09:00:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC"))
def test_days_at_time_close(): cal = NYSEExchangeCalendar() # test market_close before/after 1952-09-29 valid = cal.valid_days("1952-09-26", "1952-09-30") at_close = cal.days_at_time(valid, "market_close") assert_index_equal( at_close, pd.DatetimeIndex([ '1952-09-26 15:00:00', '1952-09-29 15:30:00', '1952-09-30 15:30:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC")) # market_close before/after 1974-01-01 valid = cal.valid_days("1973-12-28", "1974-01-02") at_close = cal.days_at_time(valid, "market_close") assert_index_equal( at_close, pd.DatetimeIndex([ '1973-12-28 15:30:00', '1973-12-31 15:30:00', '1974-01-02 16:00:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC")) # test all three market_closes valid = cal.valid_days("1952-09-26", "1974-01-02") at_close = cal.days_at_time(valid, "market_close").tz_convert(cal.tz) assert at_close[0] == pd.Timestamp('1952-09-26 15:00:00').tz_localize( cal.tz) assert at_close[1] == pd.Timestamp('1952-09-29 15:30:00').tz_localize( cal.tz) assert at_close[-2] == pd.Timestamp('1973-12-31 15:30:00').tz_localize( cal.tz) assert at_close[-1] == pd.Timestamp('1974-01-02 16:00:00').tz_localize( cal.tz) # test Saturday closes valid = cal.valid_days("1952-05-23", "1952-05-26") at_close = cal.days_at_time(valid, "market_close") assert_index_equal( at_close, pd.DatetimeIndex([ '1952-05-23 15:00:00', '1952-05-24 12:00:00', '1952-05-26 15:00:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC")) # check if it is rounded valid = cal.valid_days("1901-12-13", "1901-12-16") at_close = cal.days_at_time(valid, "market_close") assert_index_equal( at_close, pd.DatetimeIndex([ '1901-12-13 15:00:00', '1901-12-14 12:00:00', '1901-12-16 15:00:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC")) # check if chosen time is kept cal = NYSEExchangeCalendar(close_time=dt.time(10)) at_close = cal.days_at_time(valid, "market_close") assert_index_equal( at_close, pd.DatetimeIndex([ '1901-12-13 10:00:00', '1901-12-14 10:00:00', '1901-12-16 10:00:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC"))