rsheftel / pandas_market_calendars

Exchange calendars to use with pandas for trading applications
MIT License
803 stars 173 forks source link

exchange_calendar_NYSE.valid_days() incorrectly handles tz-aware inputs (regression from version 3.5?) #227

Closed andyschuster closed 2 years ago

andyschuster commented 2 years ago

Issue: valid_days() does not return correct tz-aware days if called with tz other than 'UTC'.

import pandas_market_calendars as market_cal
market_cal.get_calendar('NYSE').valid_days(
    start_date=pd.to_datetime('2022-01-01'),
    end_date=pd.Timestamp.today(),
    tz='US/Eastern')

In version 4.1.0 the above code incorrectly returns US/Eastern dates shifted back by 5 hours:

DatetimeIndex(['2022-01-02 19:00:00-05:00', '2022-01-03 19:00:00-05:00', ... ],
dtype='datetime64[ns, US/Eastern]', length=208, freq=None)

Notice the incorrect dates above. The first trading day in 2022 was 01-03.

Expected behavior should be the correct dates (with midnight normalized time), given NYSE operates on Eastern timezone:

DatetimeIndex(['2022-01-03 00:00:00-05:00', '2022-01-04 00:00:00-05:00', ... ],
dtype='datetime64[ns, US/Eastern]', length=208, freq=None)

This behavior still works as expected in version 3.5.

Suggested fix: Revert change made 2 months ago on line 1092 in exchange_calendar_nyse.py from

trading_days = super().valid_days(start_date, end_date, tz= 'UTC')

back to

trading_days = super().valid_days(start_date, end_date, tz=tz)

pandas_market_calendars version: 4.1.0 (issue observed), 3.5 (issue not observed) Python version: tested on both 3.10, 3.11 System: Win 11 / miniconda