我将数据保存到CSV文件中,如下所示:
Date,Bid,Ask
04/12/19 11:00 AM,1970,2010
05/12/19 11:00 AM,1960,2000
05/12/19 04:00 PM,1900,1925
06/12/19 11:00 AM,1890,1910
06/12/19 04:00 PM,2000,2025
我正在尝试使用 pd.to_datetime()将“日期”列转换为日期时间,但出现此错误:
ValueError:时间数据'04 / 12/19 11:00 AM'与格式'%d /%m /%y%I:%M%p'(匹配)不匹配 < / p>
这是我的代码:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("file_1.csv")
df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%y %I:%M %p')
df = df.set_index(["Date"])
df.plot()
plt.show()
这是整个错误:
Traceback (most recent call last):
File "/home/zakaria/.local/lib/python3.8/site-packages/pandas/core/tools/datetimes.py", line 450, in _convert_listlike_datetimes
values, tz = conversion.datetime_to_datetime64(arg)
File "pandas/_libs/tslibs/conversion.pyx", line 350, in pandas._libs.tslibs.conversion.datetime_to_datetime64
TypeError: Unrecognized value type: <class 'str'>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "file.py", line 6, in <module>
df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%y %I:%M %p')
File "/home/zakaria/.local/lib/python3.8/site-packages/pandas/core/tools/datetimes.py", line 803, in to_datetime
values = convert_listlike(arg._values, format)
File "/home/zakaria/.local/lib/python3.8/site-packages/pandas/core/tools/datetimes.py", line 454, in _convert_listlike_datetimes
raise e
File "/home/zakaria/.local/lib/python3.8/site-packages/pandas/core/tools/datetimes.py", line 417, in _convert_listlike_datetimes
result, timezones = array_strptime(
File "pandas/_libs/tslibs/strptime.pyx", line 144, in pandas._libs.tslibs.strptime.array_strptime
ValueError: time data '04/12/19 11:00 AM' does not match format '%d/%m/%y %I:%M %p' (match)
答案 0 :(得分:1)
为什么不pd.to_datetime
不使用格式?
df['Date'] = pd.to_datetime(df['Date'])
print(df)
Date Bid Ask
0 2019-04-12 11:00:00 1970 2010
1 2019-05-12 11:00:00 1960 2000
2 2019-05-12 16:00:00 1900 1925
3 2019-06-12 11:00:00 1890 1910
4 2019-06-12 16:00:00 2000 2025
答案 1 :(得分:1)
答案 2 :(得分:1)
已解决
Scaffold(...)
代替
df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)