我创建了一个数据框
df5 = pd.read_csv('C:/Users/Demonstrator/Downloads/Listeequipement.csv',delimiter=';', parse_dates=[0], infer_datetime_format = True)
df5['TIMESTAMP'] = pd.to_datetime(df5['TIMESTAMP'], '%d/%m/%y %H:%M')
df5['date'] = df5['TIMESTAMP'].dt.date
df5['time'] = df5['TIMESTAMP'].dt.time
date_debut = pd.to_datetime('2015-08-01 23:10:00')
date_fin = pd.to_datetime('2015-10-01 00:00:00')
df5 = df5[(df5['TIMESTAMP'] >= date_debut) & (df5['TIMESTAMP'] < date_fin)]
df5.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 8645 entries, 145 to 8789
Data columns (total 9 columns):
TIMESTAMP 8645 non-null datetime64[ns]
ACT_TIME_AERATEUR_1_F1 8645 non-null float64
ACT_TIME_AERATEUR_1_F3 8645 non-null float64
ACT_TIME_AERATEUR_1_F5 8645 non-null float64
ACT_TIME_AERATEUR_1_F6 8645 non-null float64
ACT_TIME_AERATEUR_1_F7 8645 non-null float64
ACT_TIME_AERATEUR_1_F8 8645 non-null float64
date 8645 non-null object
time 8645 non-null object
dtypes: datetime64[ns](1), float64(6), object(2)
memory usage: 675.4+ KB
我尝试每天重新取样:
df5.index = pd.to_datetime(df5.index)
df5 = df5.set_index('TIMESTAMP')
df5 = df5.resample('1d').mean()
但我遇到了问题:
get_loc中的KeyError Traceback (most recent call last) C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\indexes\base.py
(self,key,method,tolerance) 1944年尝试: - &GT; 1945年返回self._engine.get_loc(键) 1946年除了KeyError:
pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)() pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)() pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
(熊猫\ hashtable.c:12368)()
pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
(熊猫\ hashtable.c:12322)()
set_index中的KeyError: 'TIMESTAMP' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-109-bf3238788c3e> in <module>() 1 df5.index = pd.to_datetime(df5.index) ----> 2 df5 = df5.set_index('TIMESTAMP') 3 df5 = df5.resample('1d').mean() C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\frame.py
(self,keys,drop,append,inplace,verify_integrity) 2835 names.append(无) 2836其他: - &GT; 2837 level = frame [col] ._ values 2838 names.append(col) 2839如果下降:
getitem 中的C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\frame.py
(自我,密钥) 1995年返回self._getitem_multilevel(key) 1996其他: - &GT; 1997年返回self._getitem_column(关键) 1998年 1999 def _getitem_column(self,key):
_getitem_column中的C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\frame.py
(self,key) 2002#get column 2003年如果self.columns.is_unique: - &GT; 2004返回self._get_item_cache(键) 2005年 2006#duplicate columns&amp;可能降低维度
_get_item_cache中的C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\generic.py
(self,item) 1348 res = cache.get(item) 1349如果res为None: - &GT; 1350个值= self._data.get(item) 1351 res = self._box_item_values(item,values) 1352 cache [item] = res
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\internals.py
in get(self,item,fastpath) 3288 3289如果不是isnull(item): - &GT; 3290 loc = self.items.get_loc(item) 3291其他: 3292 indexer = np.arange(len(self.items))[isnull(self.items)]
get_loc中的C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\indexes\base.py
(self,key,method,tolerance) 1945年返回self._engine.get_loc(键) 1946年除了KeyError: - &GT; 1947年返回self._engine.get_loc(self._maybe_cast_indexer(key)) 1948年 1949年indexer = self.get_indexer([key],method = method,tolerance = tolerance)
pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)() pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)() pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
(熊猫\ hashtable.c:12368)()
pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
(熊猫\ hashtable.c:12322)()
KeyError: 'TIMESTAMP'
有任何想法请帮我解决这个问题吗?
亲切的问候
答案 0 :(得分:2)
删除pd.to_datetime
并直接尝试df.set_index
:
*df5.index = pd.to_datetime(df5.index)* # Delete this one
df5 = df5.set_index('TIMESTAMP')
df5 = df5.resample('1d').mean()
答案 1 :(得分:0)
下面这部分应该写成 df5 = df5.set_index('时间戳') 我认为问题出在引号上,可能是 TIMESTAMP 和最后一个引号之间存在一些差距,就像“TIMESTAMP”或“TIMESTAMP”或“TIMESTAMP” 这可以是解决方案。 我已经使用 google colab 来解决那里我发现这个解决方案在我的情况下关键错误是相同的,google colab 自动验证并提供一个选项来选择一个已经引用的索引