0

I have the following code creating a table of business days in NYSE during the past 5 years:

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

And here is the output:

            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

I actually expect to see times in Eastern time zone in US but it is not the case.

For instance, market_open in the first row should be 2017-01-18 09:30:00+00:00 but what it shows is 2017-01-18 14:30:00+00:00.

What am I supposed to do to get timezones in both columns in US/Eastern? It shows timzones in UTC but it is not what I need.


ADDENDUM:

I tried the following based on @IanKenney 's comment:

nyse_business_days["market_open"] = nyse_business_days["market_open"].tz_localize(pytz.utc).tz_convert(pytz.timezone('US/Eastern'))

And when I print it out, here is what I get:

    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
3
  • does this help stackoverflow.com/questions/22800079/… Commented Jan 18, 2022 at 12:18
  • @IanKenney I tried but it didn't really help unfortunately. I added more info on what happened in the question above. Any further comments..? Commented Jan 18, 2022 at 12:28
  • since you working with a column (Series) instead of an index, use the dt accessor: nyse_business_days["market_open"] = nyse_business_days["market_open"].dt.tz_localize(pytz.utc).dt.tz_convert(pytz.timezone('US/Eastern')) Commented Jan 19, 2022 at 9:40

2 Answers 2

2

You can try the following to convert the timezones

nyse_business_days['market_open'] = [t.tz_convert(pytz.timezone('US/Eastern')) for t in nyse_business_days['market_open']]
Sign up to request clarification or add additional context in comments.

2 Comments

It worked! Than you so much!
why would you use a list comprehension if you have the dt accessor?
0

You can use tz parameter to set timezone:

nyse_business_days = nyse_calender.schedule(
                             start_date=start_date,
                             end_date=today_date,
                             tz='America/New_York'
                     )

1 Comment

thnaks! it work~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.