我解析的數據來自CSV文件。我得到了上述錯誤。我相信這個格式是正確的。Python ValueError:時間數據'日期'與格式'%Y /%m /%d%H:%M:%S.%f'
我的代碼:
import pandas as pd
from datetime import datetime
import csv
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
headers = ['Sensor Value','Date','Time']
df = pd.read_csv('C:/Users\Lala Rushan\Downloads\DataLog.CSV',names=headers)
print (df)
df['Date'] = df['Date'].map(lambda x: datetime.strptime(str(x), '%Y/%m/%d %H:%M:%S.%f'))
x = df['Date']
y = df['Sensor Value']
# plot
plt.plot(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()
plt.show()
我的數據框:
0 Sensor Value Date Time
1 2 2017/02/17 19:06:17.188
2 72 2017/02/17 19:06:22.360
3 72 2017/02/17 19:06:27.348
控制檯錯誤:
new_values = map_f(values, arg)
File "pandas\src\inference.pyx", line 1207, in pandas.lib.map_infer (pandas\lib.c:66124)
File "C:/Users/Lala Rushan/PycharmProjects/newgraph/newgraph.py", line 10, in <lambda>
df['Date'] = df['Date'].map(lambda x: datetime.strptime(str(x), '%Y/%m/%d %H:%M:%S.%f'))
File "C:\Users\Lala Rushan\AppData\Local\Programs\Python\Python35\lib\_strptime.py", line 500, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "C:\Users\Lala Rushan\AppData\Local\Programs\Python\Python35\lib\_strptime.py", line 337, in _strptime
(data_string, format))
ValueError: time data 'Date' does not match format '%Y/%m/%d %H:%M:%S.%f'
CSV輸入:
Sensor Value Date Time
2 2017/02/17 19:06:17.188
72 2017/02/17 19:06:22.360
72 2017/02/17 19:06:27.348
72 2017/02/17 19:06:32.482
74 2017/02/17 19:06:37.515
70 2017/02/17 19:06:42.580
我敢肯定,你不需要做這種方式,嘗試'DF = PD。 read_csv('C:/ Users \ Lala Rushan \ Downloads \ DataLog.CSV',names = headers,parse_dates = ['Date'])' – EdChum
CSV中是日期和時間分隔的列嗎?請顯示CSV輸入的一些行。 – MKesper
我也試過這種方式,它給了我這個錯誤:** ValueError:時間數據'日期'不符合格式'%Y /%m /%d'**。 – rushan