6

I have a time series which was measured by a sensor with clock that doesn't adjust according to DST rules. Therefore, the datetime data is independent of DST. What is the best way to localize this data in Pandas?

Gabs
  • 61
  • 2

1 Answers1

3

You can localize to a non-changing time zone first, for example EST, then use tz_convert() to change it to a time zone region which automatically detects daylight savings, for example US/Eastern.

In [16]: a=pd.DataFrame(dict(t=(pd.Timestamp('01/01/2017'), pd.Timestamp('07/01/2017'))))

In [17]: a
Out[17]:
           t
0 2017-01-01
1 2017-07-01

In [18]: a.t=a.t.dt.tz_localize('EST')

In [19]: a
Out[19]:
                          t
0 2017-01-01 00:00:00-05:00
1 2017-07-01 00:00:00-05:00

In [20]: a.t=a.t.dt.tz_convert('US/Eastern')

In [21]: a
Out[21]:
                          t
0 2017-01-01 00:00:00-05:00
1 2017-07-01 01:00:00-04:00

By default pandas uses pytz for time zones; you can see this question for a list or how to check the time zones yourself on your installation.

Ian Kent
  • 733
  • 5
  • 12