pandas中将timestamp转为datetime

参考自:http://stackoverflow.com/questions/35312981/using-pandas-to-datetime-with-timestamps

在pandas DataFrame中将timestamp转为datetime,关键要使用unit='s'。而不是ms、us或者其他选项。

0    1450753200.123213,
1    1450756800.123213,
2    1450760400.123213,
3    1450764000.123213,
4    1450767600.123213,

转换为datetime

In [106]:
pd.to_datetime(df['timestamp'], unit='s')
Out[106]:
index
0   2015-12-22 03:00:00
1   2015-12-22 04:00:00
2   2015-12-22 05:00:00
3   2015-12-22 06:00:00
4   2015-12-22 07:00:00
Name: timestamp, dtype: datetime64[ns]

转为字符串

In [107]:

pd.to_datetime(df['timestamp'], unit='s').dt.strftime('%Y-%m-%d %H:%M')
Out[107]:
index
0    2015-12-22 03:00
1    2015-12-22 04:00
2    2015-12-22 05:00
3    2015-12-22 06:00
4    2015-12-22 07:00
Name: timestamp, dtype: object

posted on 2016-09-27 21:54  rainduck  阅读(19195)  评论(0编辑  收藏  举报

导航