将日期字符串更改为日期时间格式熊猫

时间:2020-07-03 17:05:23

标签: python pandas datetime

我正在尝试将数据帧中的字符串Datetime转换为datetime,但是出现错误internal double[] fillingArray() { a = 1.5; b = .5; for (i = 0; i<10; i++) { c = a * b; a = a + 1; b = b + 1; arrayInt[i] = c; } return arrayInt; }

ValueError: time data DATETIME doesn't match format specified

这就是我所做的。

print(chunk['DATETIME']) gives this

0               DATETIME
1    2019-12-25 15:46:42
2    2019-12-25 15:46:43
3    2019-12-25 15:47:39
4    2019-12-25 15:47:42
Name: DATETIME, dtype: object

这给了我错误。我试图删除可能存在的空白空间

chunk['DATETIME'] = pd.to_datetime(chunk['DATETIME'], format='%Y-%m-%d %H:%M:%S')
print(chunk['DATETIME'])

但是我仍然遇到相同的错误。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我尝试了您的代码,它似乎对我有用。您能否提供有关数据的更多信息?还是尝试重新启动内核?

import pandas as pd
chunk = pd.DataFrame({"DATETIME":['2019-12-25 15:46:42','2019-12-25 15:46:43']})
chunk["DATETIME"] = pd.to_datetime(chunk["DATETIME"], format='%Y-%m-%d %H:%M:%S')
print(chunk['DATETIME'])
chunk.info()

输出

0   2019-12-25 15:46:42
1   2019-12-25 15:46:43
Name: DATETIME, dtype: datetime64[ns]
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 1 columns):
 #   Column    Non-Null Count  Dtype         
---  ------    --------------  -----         
 0   DATETIME  2 non-null      datetime64[ns]
dtypes: datetime64[ns](1)
memory usage: 144.0 bytes