Time_series_visualizer - AttributeError: 'Int64Index' object has no attribute 'month'

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

My assumption is, the Replit uses another version of a relevant library.
The version you have is newer and thus offers this way of interacting with the index.
Replit has an older version, so that there might never be a compatability issue with the implemented test-cases. Because nobody got time to regularly check ALL the tests and used modules and updates and adjust that - so the versions are fixed and you might be the first one to come across an instance where this causes a problem.

That was a good assumption, and one I thought off pretty quickly.
Replit environment
Python 3.8.12
Pandas 1.3.5

Jupyter environment
Python 3.8.8
Pandas 1.2.4

I would expect Python/Pandas to be backwards compatible or throw a warning, which I am not seeing in Jupyter.

Anyhoo thanks for your reply. I’m upgrading Anaconda/pandas as we speak so lets see what that does.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.