[英]Pandas Time Series Resampling: KeyError: “['year' 'month' 'day'] not in index”
請幫忙
我嘗試對數據進行重新采樣未成功。 它生成上面的錯誤,當應用DatetimeIndex時, 它將截斷時間戳,刪除HH:MM:SS 。 它仍然不能將數據識別為Datetime對象。 提前致謝。
源文件可以在這里找到
import pandas as pd
import numpy as np
df= pd.read_csv('20170713.csv')
df2= df.loc[:,['sen_id', 'pos_id', 'heat_val', 'sat_val', 'timestamp']]
cols = df2.columns.tolist()
cols = cols[-1:] + cols[:-1]
df2 = df2[cols]
#print(df2.head())
df3 = df2.set_index(['timestamp'])
df3.index = pd.DatetimeIndex(df3.index)
print(df3.head())
pd.to_datetime(df3[['year', 'month', 'day']])
df3.resample('1H').mean()
print(df3)
問題是pd.to_datetime()
使用不正確,其中您提供了df3
不存在的三列作為df3[['year','month','day']]
。 相反,您只想提供一個系列 。 然后,您要提供參數format='%d/%m/%Y %H:%M'
,它對應於您的日期strptime格式
df= pd.read_csv('20170713.csv')
df2= df.loc[:,['sen_id', 'pos_id', 'heat_val', 'sat_val', 'timestamp']]
cols = df2.columns.tolist()
cols = cols[-1:] + cols[:-1]
df2 = df2[cols]
#print(df2.head())
df3 = df2.set_index(['timestamp'])
#df3.index = pd.DatetimeIndex(df3.index)
#print(df3.head())
#pd.to_datetime(df3[['year', 'month', 'day']])
df3.index = pd.to_datetime(df3.index,format='%d/%m/%Y %H:%M')
df3 = df3.resample('1H').mean()
print(df3)
舉個例子,為了提高可讀性,您的代碼實際上也可以被壓縮,
df = pd.read_csv('20170713.csv')
#Preserve desired columns and reorder as df2
df2 = df[['timestamp', 'sen_id', 'pos_id', 'heat_val', 'sat_val']]
#set timestamp as index and convert to datetime
df2.set_index(['timestamp'],drop=True,inplace=True)
df2.index = pd.to_datetime(df2.index,format='%d/%m/%Y %H:%M')
#resample
df3 = df2.resample('1H').mean()
print df3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.