0

I have what I thought was a simple problem but it is becoming more confusing as I read more and browse the questions on this forum . I apologize if this has been asked earlier but so far I could not find out any source which treats both the steps mentioned below together.

Problem: I have got a dataframe with time like this (without mentioning the timezone or offset)

Date Time 
2020-01-02 22:00:00
2020-01-03 01:00:00 
2002-01-03 01:05:00  

and so on. These times are in EST. EST follows daylight savings time

I need to convert this into Japan standard time which does not follow daylight savings time

So first, I need to convert the time 2020-01-02 22:00:00 into EST datetime object. I was going to use datetime.strptime but I don't know where to put the timezone.

Next, I need to convert the EST datetime object into japan standard time. But I am not sure if python will automatically figure out when the daylight savings time started in US EST timezone. There have been some changes by US congress in the past and so , how will Python know about this. Similarly, if in future there are some changes by US congress e.g. they remove daylight savings time, will we able to keep the same code by just updating the python routines for timezone.

Thank you for any inputs

Ramana
  • 243
  • 4
  • 15

1 Answers1

1

assuming your dataframe value is timezone naive a.k.a datetime without timezone, then you need to attach EST timezone with DST with tz_localize('EST', ambiguous='infer') and then use tz_convert('Asia/Tokyo') to change to correct time for Japan.

Without specific info about which city that data is for, it can be impossible to determine if DST is in effect. According to https://www.timeanddate.com/time/zone/usa/new-york, NY is EST until 2020-05-07, and then will be in EDT UTC-04:00 until 31 Oct.

see https://pandas.pydata.org/docs/reference/api/pandas.DatetimeIndex.tz_localize.html#pandas-datetimeindex-tz-localize for more info on Daylight Saving Time

In some cases, inferring the DST is impossible. In such cases, you can pass an ndarray to the ambiguous parameter to set the DST explicitly

...

s.dt.tz_localize('CET', ambiguous=np.array([True, True, False]))

I'm starting with string values

# just str, not a datetime index
>>> df.info()                                                                                                                                                            
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 1 columns):
Datetime    3 non-null object
dtypes: object(1)
memory usage: 152.0+ bytes

# convert to datetime
>>> tz_naive = pd.to_datetime(df['Datetime'])
>>> tz_naive
0   2020-01-02 22:00:00
1   2020-01-03 01:00:00
2   2002-01-03 01:05:00
Name: Datetime, dtype: datetime64[ns]

>>> tz_aware = tz_naive.dt.tz_localize('EST', ambiguous='infer')
>>> tz_aware                                                                                                                                                              
0   2020-01-02 22:00:00-05:00
1   2020-01-03 01:00:00-05:00
2   2002-01-03 01:05:00-05:00
Name: Datetime, dtype: datetime64[ns, EST]

>>> tz_aware.dt.tz_convert('Asia/Tokyo')
0   2020-01-03 12:00:00+09:00
1   2020-01-03 15:00:00+09:00
2   2002-01-03 15:05:00+09:00
Name: Datetime, dtype: datetime64[ns, Asia/Tokyo]
Community
  • 1
  • 1
deadvoid
  • 1,270
  • 10
  • 19