首页 > 解决方案 > 对象不匹配格式 '%Y-%m-%d %H:%M:%S

问题描述

转换为数据类型(在 csv 文件中)
(yyyy-mm-dd h:mm:ss(当前列数据类型)--(转换)--> 143141231(纳秒))

问题 -> 对象与格式不匹配'%Y-%m-%d %H:%M:%S'

csv 文件
ex)
时间
2019-01-01 0:00:00
2019-01-01 8:50:00
2019-01-01 8:50:00
2019-01-01 8:51:00
2019-01-01 2019-01-01
8:51:00 8:51:00

代码

import time
import datetime
import pandas as pd

df = pd.read_csv("data/test.csv")

df.head()

mydate =str(df["time"])

timestamp = time.mktime(datetime.datetime.strptime(mydate, "%Y-%m-%d %H:%M:%S").timetuple())

df["time"] = [str(df[timestamp()][t]) for t in range(len(df))]

df.head()

ns_precision = df
ns_precision.to_csv("data/test_convert.csv", index=False)


预计

时间
1131314141
1412313131
1312414123
1231225244
2421412141


结果

ValueError: time data '0        
2019-01-01 0:00:00\n1 ... 2019-01-30 20:15:00\n
Name: time, Length: 77876, 
dtype: object' does not match format '%Y-%m-%d %H:%M:%S'

标签: pythonpandastypespython-datetime

解决方案


推荐阅读