我正在努力使用pandas日期时间格式。我的数据集如下(日期作为类型对象):
+--------+------------+----------+---------------------------+---------------------+
| | event_id_x | payback | event_starts_utc_datetime | dtScraped |
+--------+------------+----------+---------------------------+---------------------+
| 80325 | 1004179030 | 0.980840 | 2017-09-13 20:45:03.888 | 2017-09-06 17:06:32 |
| 104592 | 1004179030 | 0.980840 | 2017-09-13 20:45:03.888 | 2017-09-06 19:23:56 |
| 261304 | 1004179030 | 0.980840 | 2017-09-13 20:45:03.888 | 2017-09-07 06:21:47 |
| 657433 | 1004179030 | 0.980840 | 2017-09-13 20:45:03.888 | 2017-09-08 13:06:05 |
| 661013 | 1004179030 | 0.979975 | 2017-09-13 20:45:03.888 | 2017-09-11 09:04:15 |
+--------+------------+----------+---------------------------+---------------------+
我想将event_starts_utc_datetime和dtScraped传递给datetime,但是以下内容返回“ValueError:time data'vent_starts_utc_datetime'与指定的格式不匹配”
pinny_payback["event_starts_utc_datetime"] = pd.to_datetime(["event_starts_utc_datetime"], format='%Y-%m-%d %H:%M:%S.%f')
你能帮忙吗?
答案 0 :(得分:2)
以下是创建虚拟df的一些代码。我将其修改为使用逗号和pd.read_clipboard
。
"""
id,event_id_x,payback,event_starts_utc_datetime,dtScraped
80325,1004179030,0.980840,2017-09-13 20:45:03.888,2017-09-06 17:06:32
104592,1004179030,0.980840,2017-09-13 20:45:03.888,2017-09-06 19:23:56
261304,1004179030,0.980840,2017-09-13 20:45:03.888,2017-09-07 06:21:47
657433,1004179030,0.980840,2017-09-13 20:45:03.888,2017-09-08 13:06:05
661013,1004179030,0.979975,2017-09-13 20:45:03.888,2017-09-11 09:04:15
"""
import pandas as pd
df = pd.read_clipboard(sep = ',')
df['event_starts_utc_datetime'] = pd.to_datetime(df['event_starts_utc_datetime'], format='%Y-%m-%d %H:%M:%S.%f')
print(df.dtypes)
df
我怀疑您的错误可能未在pd.to_datetime()
声明中包含数据框名称...
输出:
id int64
event_id_x int64
payback float64
event_starts_utc_datetime datetime64[ns]
dtScraped object
dtype: object