Data Analysis with Python Projects - Page View Time Series Visualizer

Tell us what’s happening:
Hi, thanks for the video. It helped me a lot
I have this error
File “/home/runner/boilerplate-page-view-time-series-visualizer/time_series_visualizer.py”, line 31, in draw_bar_plot
df[‘month’] = df.index.month
AttributeError: Object ‘Index’ does not have attribute ‘month’.
output status 1

I don’t know why, I’ve already checked the dataframe index df = pd.read_csv(“fcc-forum-pageviews.csv”, parse_dates=[‘date’], dayfirst=True, index_col=‘date’)
And I even print the dir(df) and there is no month or year in the index. I don’t know how to fix it. If anyone knows please help

Your code so far

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Import data (Make sure to parse dates. Consider setting index column to 'date'.)
df =  pd.read_csv("fcc-forum-pageviews.csv", parse_dates=['date'], dayfirst=True, index_col='date')

# Clean data
df = df[(df['value'] <= df['value'].quantile(0.975)) & (df['value'] >= df['value'].quantile(0.025))]


def draw_line_plot():
    # Draw line plot
    fig,ax = plt.subplots(figsize=(10,7))
    ax.plot(df.index, df['value'], 'r', linewidth=1)
    ax.set_title('Daily freeCodeCamp Forum Page Views 5/2016-12/2019')
    ax.set_xlabel('Date')
    ax.set_ylabel('Page Views')
    



    # Save image and return fig (don't change this part)
    fig.savefig('line_plot.png')
    return fig

def draw_bar_plot():
    # Copy and modify data for monthly bar plot
    df['month'] = df.index.month
    df['year'] = df.index.year
    df_bar = df.groupby(['year', 'month'])['value'].mean() 
    df_bar=df_bar.unstack()

    
    

    # Draw bar plot
    fig= df_bar.plot.bar(legend=True, figsize= (10,5), ylabel='Average Page Views',       
    xlabel='years').figure
    plt.legend(['April',
      'August',
  'December',
  'February',
  'January',
  'July',
  'June',
  'March',
  'May',
  'November',
  'October',
  'September'])




    # Save image and return fig (don't change this part)
    fig.savefig('bar_plot.png')
    return fig

def draw_box_plot():
    # Prepare data for box plots (this part is done!)
    df_box = df.copy()
    df_box.reset_index(inplace=True)
    df_box['year'] = [d.year for d in df_box.date]
    df_box['month'] = [d.strftime('%b') for d in df_box.date]

    # Draw box plots (using Seaborn)
    fig, axes = plt.subplots(1, 2, figsize=(15, 6))
    sns.boxplot(x='year', y='value', data=df_box, ax=axes[0])
    axes[0].set_title('Year-wise Box Plot (Trend)')
    axes[0].set_xlabel('Year')
    axes[0].set_ylabel('Page Views')
    axes[0].set_ylim(0,200000)
    axes[0].set_yticks(range(0, 200001, 20000))
        # Plot the month-wise box plot
    sns.boxplot(x='month', y='value', data=df_box, ax=axes[1],
                    order=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                           'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
    axes[1].set_title('Month-wise Box Plot (Seasonality)')
    axes[1].set_xlabel('Month')
    axes[1].set_ylabel('Page Views')
    axes[1].set_ylim(0,200000)
    axes[1].set_yticks(range(0, 200001, 20000))




    # Save image and return fig (don't change this part)
    fig.savefig('box_plot.png')
    return fig

Your browser information:
Opera

Challenge: Data Analysis with Python Projects - Page View Time Series Visualizer

Link to the challenge:

Here’s where you read the csv:

df =  pd.read_csv("fcc-forum-pageviews.csv", parse_dates=['date'], dayfirst=True, index_col='date')

And here is the date format in the csv:

2016-06-24,19230

Your code by itself doesn’t generate any errors but the dayfirst=True option is breaking the test.

I don’t think you should use it since the date format in the csv is year, month, day. I believe the dayfirst=True option is to read files that are already in the year,day,month format, since after you read the file the order stays the same, it doesn’t translate it.

1 Like

Thank you so much! that was the error

1 Like

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