John Zwinck
John Zwinck

Reputation: 249652

Pandas DatetimeIndex vs to_datetime discrepancies

I'm trying to convert a Pandas Series of epoch timestamps to human-readable times. There are at least two obvious ways to do this: pd.DatetimeIndex and pd.to_datetime(). They seem to work in quite different ways:

In [1]: import pandas as pd

In [3]: nanos = pd.Series([1462282258000000000, 1462282258100000000, 1462282258200000000])

In [4]: pd.to_datetime(nanos)
Out[4]: 
0   2016-05-03 13:30:58.000
1   2016-05-03 13:30:58.100
2   2016-05-03 13:30:58.200
dtype: datetime64[ns]

In [5]: pd.DatetimeIndex(nanos)
Out[5]: 
DatetimeIndex([       '2016-05-03 13:30:58', '2016-05-03 13:30:58.100000',
               '2016-05-03 13:30:58.200000'],
              dtype='datetime64[ns]', freq=None)

With to_datetime(), the display resolution is milliseconds, and .000 is printed on whole seconds. With DatetimeIndex, the display resolution is microseconds (which I like), but the decimal part is completely omitted on whole seconds.

Then, try converting the time zone:

In [12]: pd.DatetimeIndex(nanos).tz_localize('UTC')                   
Out[12]: 
DatetimeIndex([       '2016-05-03 13:30:58+00:00',
               '2016-05-03 13:30:58.100000+00:00',
               '2016-05-03 13:30:58.200000+00:00'],
              dtype='datetime64[ns, UTC]', freq=None)

In [13]: pd.to_datetime(nanos).tz_localize('UTC')  
TypeError: index is not a valid DatetimeIndex or PeriodIndex

This is strange: the timezone functions don't work with a plain datetime Series, only with a DatetimeIndex. Why would that be? The tz_localize() method exists and is documented here: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.tz_localize.html

I've tried Pandas 0.17.0 and 0.18.1 with the same results.

I'm not trying to make an actual index, so all else being equal I would have expected to use to_datetime() - I just can't get time zone methods to work with it.

Upvotes: 6

Views: 3656

Answers (1)

Jeff
Jeff

Reputation: 129078

There is 1 way to convert things, pd.to_datetime(), yes you can directly construct a DatetimeIndex, but it is restrictive on purpose, while to_datetime is quite flexible.

So to_datetime will give you a similar object to what you input, if you input an array-like, then you will get a DatetimeIndex, input a Series you will get a Series.

In [5]: nanos = [1462282258000000000, 1462282258100000000, 1462282258200000000]

By default it will convert with a unit='ns' which lines up here

In [7]: pd.to_datetime(nanos)
Out[7]: DatetimeIndex(['2016-05-03 13:30:58', '2016-05-03 13:30:58.100000', '2016-05-03 13:30:58.200000'], dtype='datetime64[ns]', freq=None)

So one thing we could do is make a Series out of this. The index is INTEGER here, the values are Datetimes.

In [10]: s = Series(pd.to_datetime(nanos))

In [11]: s
Out[11]: 
0   2016-05-03 13:30:58.000
1   2016-05-03 13:30:58.100
2   2016-05-03 13:30:58.200
dtype: datetime64[ns]

You then can use the .dt accessor to operate on the values. Series.tz_localize operates on the index.

In [12]: s.dt.tz_localize('US/Eastern')
Out[12]: 
0          2016-05-03 13:30:58-04:00
1   2016-05-03 13:30:58.100000-04:00
2   2016-05-03 13:30:58.200000-04:00
dtype: datetime64[ns, US/Eastern]

Upvotes: 6

Related Questions