pandas中將timestamp轉爲datetime

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

在pandas DataFrame中將timestamp轉爲datetime,關鍵要使用unit='s'。而不是ms、us或者其餘選項。字符串

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

轉換爲datetimeget

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]

轉爲字符串pandas

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
相關文章
相關標籤/搜索