Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Chapter 15: pandas-implementation for 'resample' has changed #148

Open
thomas-haslwanter opened this issue Jul 17, 2024 · 1 comment

Comments

@thomas-haslwanter
Copy link

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:

  1. df_monthly.mean() does not work, unless the column day_type is dropped
  2. df_monthly[period] also crashes (with the stacktrace as given above)
@marcello10
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants