如何将以YYYYMMDD存储的日期转换为大熊猫中的datetime格式

时间:2018-07-30 05:09:22

标签: python-3.x pandas

我在数据框中有一个要转换为datetime格式的YYYYMMDD格式的列。怎么在熊猫里做

    Input

20180504   
20180516   
20180516   
20180517   

**Expected Output**

Date         datetime
20180504   04/5/2018 00:00:00
20180516   16/5/2018 00:00:00
20180516   16/5/2018 00:00:00
20180517   17/5/2018 00:00:00

1 个答案:

答案 0 :(得分:1)

使用to_datetime

df['datetime'] = pd.to_datetime(df['Input'], format='%Y%m%d')
print (df)
      Input   datetime
0  20180504 2018-05-04
1  20180516 2018-05-16
2  20180516 2018-05-16
3  20180517 2018-05-17

如果为零,则它们不会显示在列中,但是如果转换为列表,则可以看到它:

print (df['datetime'].tolist())
[Timestamp('2018-05-04 00:00:00'), 
 Timestamp('2018-05-16 00:00:00'), 
 Timestamp('2018-05-16 00:00:00'), 
 Timestamp('2018-05-17 00:00:00')]

如果输入的是csv文件:

df = pd.read_csv(file, parse_dates=['Input'])

如果希望使用与上述格式相同的格式,则可以,但是输出是字符串,而不是日期时间:

df['datetime'] = pd.to_datetime(df['Input'], format='%Y%m%d').dt.strftime('%d/%m%Y %H:%M:%S')
print (df)

      Input            datetime
0  20180504  04/052018 00:00:00
1  20180516  16/052018 00:00:00
2  20180516  16/052018 00:00:00
3  20180517  17/052018 00:00:00

print (df['datetime'].tolist())

['04/052018 00:00:00', '16/052018 00:00:00', '16/052018 00:00:00', '17/052018 00:00:00']