将时间列转换为日期时间格式的熊猫成语是什么?

时间:2015-12-29 06:24:47

标签: python datetime pandas

我正在尝试将列中的时间转换为日期时间:

(2015, 12, 29, 0, 30, 50, 1, 363, 0)

df_date = pd.DataFrame({'date': [(2015, 12, 29, 0, 30, 50, 1, 363, 0), 
                                 (2015, 12, 28, 23, 59, 12, 0, 362, 0),
                                 (2015, 12, 28, 23, 50, 40, 0, 362, 0)]})
df_date.date = df_date.date.apply(lambda x:x[:6])
# df_date['date'] =  pd.to_datetime(df_date['date'], format =%Y,%M,%D,%H,%M,%S) #(doesn't work)

感谢。

1 个答案:

答案 0 :(得分:2)

使用datetime.datetime转换数据:

import pandas as pd
import datetime

df_date = pd.DataFrame({'date': [(2015, 12, 29, 0, 30, 50, 1, 363, 0), 
                                 (2015, 12, 28, 23, 59, 12, 0, 362, 0),
                                 (2015, 12, 28, 23, 50, 40, 0, 362, 0)]})
df_date.date = df_date.date.apply(lambda x: datetime.datetime(*x[:6]))

>>> df_date.date
                 date
0 2015-12-29 00:30:50
1 2015-12-28 23:59:12
2 2015-12-28 23:50:40