如何将系列转换为日期时间

时间:2019-06-20 15:16:02

标签: python pandas

我在下面有一个示例数据框。我希望合并日期和时间列,并创建另一个作为日期时间对象的列。

我不断收到ValueError: time data '13/06/2019 00:00:00.026250' does not match format '%d/%m/%Y %H:%M:%S.%f'

我在转换中缺少什么?

Index  Date     Time               Seconds             Counts   Pressure    
    12  12/06/2019  17:04:33.403250     1560359073.403250   12927   68.1614794953802
    13  12/06/2019  17:04:33.434500     1560359073.434500   12927   68.1614794953802
    14  12/06/2019  17:04:33.465750     1560359073.465750   12927   68.1614794953802
    15  12/06/2019  17:04:33.497000     1560359073.497000   12927   68.1614794953802
    16  12/06/2019  17:04:33.528250     1560359073.528250   12927   68.1614794953802

下面显示的代码是我用来将日期和时间转换为datetime对象的

df['DT'] = df['Date'] + ' ' + df['Time']
df['DT2'] = df['DT'].apply(lambda x : datetime.strptime(x, '%d/%m/%Y %H:%M:%S.%f'))
df.head()

1 个答案:

答案 0 :(得分:1)

这应该有效:

df['DT2'] = pd.to_datetime(df['DT'])

内置的pandas函数与日期时间函数相比具有一致的功能

相关问题