Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous time error pandas

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.

like image 454
trench Avatar asked May 09 '17 20:05

trench


1 Answers

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]
like image 113
Andy Hayden Avatar answered Oct 16 '22 06:10

Andy Hayden