Data Analysis with Python Projects - Page View Time Series Visualizer

Tell us what’s happening:
The part where the data used to create the box plot keeps bringing up an error when I run my code.

python3 main.py
Traceback (most recent call last):
File “/home/runner/boilerplate-page-view-time-series-visualizer/main.py”, line 8, in
time_series_visualizer.draw_box_plot()
File “/home/runner/boilerplate-page-view-time-series-visualizer/time_series_visualizer.py”, line 57, in draw_box_plot
df_box[‘year’] = [d.year for d in df_box.date]
File “/home/runner/boilerplate-page-view-time-series-visualizer/time_series_visualizer.py”, line 57, in
df_box[‘year’] = [d.year for d in df_box.date]
AttributeError: ‘str’ object has no attribute ‘year’
exit status 1

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')
pd.DatetimeIndex(df['date'])
df.set_index('date', inplace = True)

# Clean data

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

def draw_line_plot():
    # Draw line plot
  
    fig, ax = plt.subplots(figsize=(10,5))
    plt.plot(df.index, df['value'])
    plt.xlabel('Date')
    plt.ylabel('Page Views')
    plt.title('Daily freeCodeCamp Forum Page Views 5/2016-12/2019')

    # 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_bar = df.copy()
    df_bar['year'] = pd.DatetimeIndex(df_bar.index).year
    df_bar['month'] = pd.DatetimeIndex(df_bar.index).month
    df_bar = df_bar.groupby(['year', 'month'])['value'].mean().unstack()
  
    # Draw bar plot
  
    fig = df_bar.plot.bar()
    plt.legend(['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], title='Months')
    plt.xlabel('Years')
    plt.ylabel('Average Page Views')
    plt.tight_layout()
    fig = fig.figure

    # 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]
    df_box['month_num'] = df_box['date'].dt.month
    df_box = df_box.sort_values('month_num')

    # Draw box plots (using Seaborn)

    fig, ax = plt.subplots(nrows=1, ncols = 2, figsize=(15,7))
  
    sns.boxplot(x='year', y = 'value', data=df_box, ax=ax[0]).set(xlabel='Year', ylabel='Page Views', title='Year-wise Box Plot (Trend)')

    sns.boxplot(x='month', y='value', data=df_box, ax=ax[1], order=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']).set(xlabel='Month', ylabel='Page Views', title='Month-wise Box Plot (Seasonality)')
    


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

Here’s the link to my project.

I’d greatly appreciate any help.
Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

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

Link to the challenge:

This means you’re treating a string as if it were a date object because you’ve not followed this bit of advice in the boilerplate (especially the parsing dates part):

If you’re not sure about the type or values, print the date column of your dataframe or use repr() on one of the items in the date column to check the type and value.

I parsed the dates and it worked. Thank you.

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