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 pdIn [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.