df['first_timestamp'] = pd.to_datetime(df['first_timestamp']).dt.tz_localize('US/Central', ambiguous='infer')
df['second_timestamp'] = pd.to_datetime(df['second_timestamp']).dt.tz_localize('US/Central', ambiguous='infer')
AmbiguousTimeError: 2014-11-02 01:02:14
These timestamps are from a csv file which doesn't include the time zone. I want to add the time zone before I load it into my database though, if possible. Is there something I did wrong with the ambiguous part of that code? I am still getting an error.
This datetime exists twice in central time, e.g. see here:
Nov 2, 2014 - Daylight Saving Time Ended
When local daylight time was about to reach
Sunday, November 2, 2014, 2:00:00 am clocks were turned backward 1 hour to
Sunday, November 2, 2014, 1:00:00 am local standard time instead
Sunrise and sunset was about 1 hour earlier on Nov 2, 2014 than the day before. There was more light in the morning.
If these are actually UTC, you should localize from UTC:
In [11]: pd.Series([pd.Timestamp("2014-11-02 01:02:14 UTC")]).dt.tz_convert('US/Central')
Out[11]:
0 2014-11-01 20:02:14-05:00
dtype: datetime64[ns, US/Central]
or pass True to the ambiguous flag (rather than 'infer'
) to just pick one:
In [12]: pd.Series([pd.Timestamp("2014-11-02 01:02:14")]).dt.tz_localize('US/Central', ambiguous=True)
Out[12]:
0 2014-11-02 01:02:14-05:00
dtype: datetime64[ns, US/Central]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With