I am working my way through the Python for Data Analysis projects and I seem to have hit an error I can’t work out
I run the below code in my Jupyter notebook and it renders the graph as requested
def draw_bar_plot():
# Copy and modify data for monthly bar plot
df_bar = df.copy()
df_bar['month'] = df_bar.index.month
df_bar['year'] = df_bar.index.year
df_bar = df_bar.groupby(['year', 'month'])['value'].mean()
df_bar = df_bar.unstack()
# Draw bar plot
fig = df_bar.plot.bar(legend = True, figsize = (13, 13), xlabel = 'years', ylabel = "Average Page Views").figure
plt.legend(['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'])
# Save image and return fig (don't change this part)
fig.savefig('bar_plot.png')
return fig
draw_bar_plot()
But when I run this code in the replit environment I get errors
def draw_bar_plot():
# Copy and modify data for monthly bar plot
df_bar = df.copy()
df_bar['month'] = df_bar.index.month
df_bar['year'] = df_bar.index.year
df_bar = df_bar.groupby(['year', 'month'])['value'].mean()
df_bar = df_bar.unstack()
# Draw bar plot
fig = df_bar.plot.bar(legend = True, figsize = (13, 13), xlabel = 'years', ylabel = "Average Page Views").figure
plt.legend(['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'])
# Save image and return fig (don't change this part)
fig.savefig('bar_plot.png')
return fig
This returns the following error
Traceback (most recent call last):
File "main.py", line 7, in <module>
time_series_visualizer.draw_bar_plot()
File "/home/runner/boilerplate-page-view-time-series-visualizer-1/time_series_visualizer.py", line 33, in draw_bar_plot
df_bar['month'] = df_bar.index.month
AttributeError: 'Int64Index' object has no attribute 'month'
exit status 1
I don’t understand why it works in Jupyter and not in replit. why the error if the code is good?
Any help gratefully recieved