Poisson
Poisson

Reputation: 1623

Error when resample dataframe with python

I create a dataframe

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

I try to resample it per day like this :

df5.index = pd.to_datetime(df5.index)
df5 = df5.set_index('TIMESTAMP')
df5 = df5.resample('1d').mean()

But I get a problem :

KeyError                                  Traceback (most recent call last)
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\indexes\base.py

in get_loc(self, key, method, tolerance) 1944 try: -> 1945 return self._engine.get_loc(key) 1946 except 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

(pandas\hashtable.c:12368)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item

(pandas\hashtable.c:12322)()

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

in set_index(self, keys, drop, append, inplace, verify_integrity) 2835 names.append(None) 2836 else: -> 2837 level = frame[col]._values 2838 names.append(col) 2839 if drop:

C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\frame.py

in getitem(self, key) 1995 return self._getitem_multilevel(key) 1996 else: -> 1997 return self._getitem_column(key) 1998 1999 def _getitem_column(self, key):

C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\frame.py

in _getitem_column(self, key) 2002 # get column 2003 if self.columns.is_unique: -> 2004 return self._get_item_cache(key) 2005 2006 # duplicate columns & possible reduce dimensionality

C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\generic.py

in _get_item_cache(self, item) 1348 res = cache.get(item) 1349 if res is None: -> 1350 values = 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 if not isnull(item): -> 3290 loc = self.items.get_loc(item) 3291 else: 3292 indexer = np.arange(len(self.items))[isnull(self.items)]

C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\indexes\base.py

in get_loc(self, key, method, tolerance) 1945 return self._engine.get_loc(key) 1946 except KeyError: -> 1947 return 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

(pandas\hashtable.c:12368)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item

(pandas\hashtable.c:12322)()

KeyError: 'TIMESTAMP'

Any idea please to help me to resolve this problem?

Kind regards

Upvotes: 2

Views: 42348

Answers (2)

Ravi Malvia
Ravi Malvia

Reputation: 11

this below part should be written as df5 = df5.set_index('TIMESTAMP ') i think problem is with quotation mark , may be there is some gap between TIMESTAMP and last quotation mark , it would be like "TIMESTAMP " OR " TIMESTAMP " OR " TIMESTAMP" This can be solution. i have used google colab to resolve there i have found this solution in my case key error was same , google colab automatically verify and give a option to choose a index to be given which is already quoted

Upvotes: 1

SerialDev
SerialDev

Reputation: 2847

remove pd.to_datetime and try df.set_index directly:

*df5.index = pd.to_datetime(df5.index)* # Delete this one
df5 = df5.set_index('TIMESTAMP')
df5 = df5.resample('1d').mean()

Upvotes: 2

Related Questions