我使用以下代码创建了过去 5 年在 NYSE 的工作日表:
import pandas as pd
import datetime, pytz
import pandas_market_calendars as mcal
today_date = datetime.datetime.now(tz=pytz.timezone('US/Eastern'))
print(today_date)
start_date = datetime.date(today_date.year - 5, today_date.month, today_date.day)
print(start_date)
nyse_calender = mcal.get_calendar('NYSE')
print(nyse_calender.tz.zone)
nyse_business_days = nyse_calender.schedule(start_date=start_date,
end_date=today_date)
nyse_business_days
这是输出:
market_open market_close
2017-01-18 2017-01-18 14:30:00+00:00 2017-01-18 21:00:00+00:00
2017-01-19 2017-01-19 14:30:00+00:00 2017-01-19 21:00:00+00:00
2017-01-20 2017-01-20 14:30:00+00:00 2017-01-20 21:00:00+00:00
2017-01-23 2017-01-23 14:30:00+00:00 2017-01-23 21:00:00+00:00
2017-01-24 2017-01-24 14:30:00+00:00 2017-01-24 21:00:00+00:00
... ... ...
2022-01-11 2022-01-11 14:30:00+00:00 2022-01-11 21:00:00+00:00
2022-01-12 2022-01-12 14:30:00+00:00 2022-01-12 21:00:00+00:00
2022-01-13 2022-01-13 14:30:00+00:00 2022-01-13 21:00:00+00:00
2022-01-14 2022-01-14 14:30:00+00:00 2022-01-14 21:00:00+00:00
2022-01-18 2022-01-18 14:30:00+00:00 2022-01-18 21:00:00+00:00
1260 rows × 2 columns
我实际上希望看到美国东部时区的时间,但事实并非如此。
例如,第一行中的 market_open 应该是 2017-01-18 09:30:00+00:00 但它显示的是 2017-01-18 14:30:00+00:00 .
我应该怎么做才能获得美国/东部两列中的时区?它显示 UTC 时区,但这不是我需要的。
附录:
我根据@IanKenney 的评论尝试了以下操作:
nyse_business_days["market_open"] = nyse_business_days["market_open"].tz_localize(pytz.utc).tz_convert(pytz.timezone('US/Eastern'))
当我打印出来时,我得到的是:
market_open market_close
2017-01-18 NaT 2017-01-18 21:00:00+00:00
2017-01-19 NaT 2017-01-19 21:00:00+00:00
2017-01-20 NaT 2017-01-20 21:00:00+00:00
2017-01-23 NaT 2017-01-23 21:00:00+00:00
2017-01-24 NaT 2017-01-24 21:00:00+00:00
... ... ...
2022-01-11 NaT 2022-01-11 21:00:00+00:00
2022-01-12 NaT 2022-01-12 21:00:00+00:00
2022-01-13 NaT 2022-01-13 21:00:00+00:00
2022-01-14 NaT 2022-01-14 21:00:00+00:00
2022-01-18 NaT 2022-01-18 21:00:00+00:00
1260 rows × 2 columns
您可以尝试以下方法来转换时区
nyse_business_days['market_open'] = [t.tz_convert(pytz.timezone('US/Eastern')) for t in nyse_business_days['market_open']]
您可以使用 tz 参数来设置时区:
nyse_business_days = nyse_calender.schedule(
start_date=start_date,
end_date=today_date,
tz='America/New_York'
)