[BUG] Chapter 15: pandas-implementation for 'resample' has changed
Describe the bug IPYNB for chapter 15, section "Basic RNNs" crashes on resampling a dataframe
To Reproduce Please copy the code that fails here, using code blocks like this:
period = slice("2001", "2019")
df_monthly = df.resample('ME')
#.mean() # compute the mean for each month
rolling_average_12_months = df_monthly[period].rolling(window=12).mean()
And if you got an exception, please copy the full stacktrace here:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[25], line 4
2 df_monthly = df.resample('ME')
3 #.mean() # compute the mean for each month
----> 4 rolling_average_12_months = df_monthly[period].rolling(window=12).mean()
File C:\Programs\WPy64-31220\python-3.12.2.amd64\Lib\site-packages\pandas\core\base.py:244, in SelectionMixin.__getitem__(self, key)
242 else:
243 if key not in self.obj:
--> 244 raise KeyError(f"Column not found: {key}")
245 ndim = self.obj[key].ndim
246 return self._gotitem(key, ndim=ndim)
KeyError: "Column not found: slice('2001', '2019', None)"
Versions (please complete the following information):
- OS: Win 11
- Python: '3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)]'
- pandas: '2.2.0'
Additional info There are two problems:
df_monthly.mean()does not work, unless the columnday_typeis droppeddf_monthly[period]also crashes (with the stacktrace as given above)
Hello, you can change the df_monthly because df contains the column day_type that is object type.
df_monthly = df[["bus","rail"]].resample('ME').mean() # compute the mean for each month
@ageron , The code for chapter 15 continues to have this issue, see https://github.com/ageron/handson-ml3/blob/main/15_processing_sequences_using_rnns_and_cnns.ipynb
df_monthly = df.resample('M').mean() # compute the mean for each month should be changed to df_monthly = df[["bus","rail"]].resample('ME').mean() # compute the mean for each month
Thanks!
This problem should now be fixed, please reopen if you still see it.