ageron/handson-ml3

[BUG] Chapter 15: TypeError in code for Figure 15-8

vaharoni opened this issue · 0 comments

The book has the following code:

period = slice("2001", "2019")
df_monthly = df.resample('M').mean()  # compute the mean for each month
rolling_average_12_months = df_monthly[period].rolling(window=12).mean()

fig, ax = plt.subplots(figsize=(8, 4))
df_monthly[period].plot(ax=ax, marker=".")
rolling_average_12_months.plot(ax=ax, grid=True, legend=False)
plt.show()

However, the second line fails with TypeError: Could not convert UWWWWAUWWWWWAUWWWWWAUWWWWWAUWWW to numeric.

The correct code seems to be:

period = slice("2001", "2019")
df_monthly = df[['bus', 'rail']].resample('M').mean()  # compute the mean for each month
rolling_average_12_months = df_monthly[period].rolling(window=12).mean()

fig, ax = plt.subplots(figsize=(8, 4))
df_monthly[period].plot(ax=ax, marker=".")
rolling_average_12_months.plot(ax=ax, grid=True, legend=False)
plt.show()