Data Analysis with Python Projects - Page View Time Series Visualizer

Tell us what’s happening:
Hi, when i run the project i get the following error:

ERROR: test_data_cleaning (test_module.DataCleaningTestCase)

Traceback (most recent call last):
File “/home/runner/boilerplate-page-view-time-series-visualizer/test_module.py”, line 7, in test_data_cleaning
actual = int(time_series_visualizer.df.count(numeric_only=True))
File “/home/runner/boilerplate-page-view-time-series-visualizer/.pythonlibs/lib/python3.10/site-packages/pandas/core/series.py”, line 230, in wrapper
raise TypeError(f"cannot convert the series to {converter}")
TypeError: cannot convert the series to <class ‘int’>


Ran 11 tests in 4.031s

FAILED (errors=1)

I think the project works fine, i get the plots like the example but i don’t understand why i have the error.

When i run the code in a ipynb it works fine without any error message.
I also have tried to re import the project as a new, copy code from the first one and paste in the new imported project but i have the same error.

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"]).set_index("date")

# Clean data
top_threshold = df['value'].quantile(0.975)
bottom_threshold = df['value'].quantile(0.025)

df = df[(df['value'] <= top_threshold) & (df['value'] >= bottom_threshold)]



def draw_line_plot():
    # Draw line plot
    fig, ax = plt.subplots()
    plot = ax.plot(df["value"])

    # Customize the plot (optional)
    ax.set_xlabel("Date")
    ax.set_ylabel("Page Views")
    ax.set_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
    # Extract year and month from the 'date' column
    df_bar['year'] = df.index.year
    df_bar['month'] = df.index.month

    # Create a pivot table to calculate average page views per month
    pivot_table = df.pivot_table(index='year', columns='month', values='value', aggfunc='mean')

    month_labels = ['January', 'February', 'March', 'April', 'May', 'June',
                    'July', 'August', 'September', 'October', 'November', 'December']
    
    # Draw bar plot
    
    fig, ax = plt.subplots(figsize=(12, 6))
    pivot_table.plot(kind='bar', ax=ax)
    
    
    ax.set_xlabel('Years')
    ax.set_ylabel('Average Page Views')
    ax.set_title('Average Daily Page Views per Month (2016-2019)')
    ax.set_xticklabels(df['year'].unique(), rotation=0)
    ax.set_xticks(range(len(df['year'].unique())))
    ax.legend(title='Months', labels=month_labels)



    # 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)
    df_box["month_num"]=df_box["date"].dt.month
    df_box=df_box.sort_values("month_num")
    fig, axes = plt.subplots(1, 2, figsize=(15, 6))

    
    
    # Plot Year-wise Box Plot
    sns.boxplot(x='year', y='value', data=df_box, ax=axes[0])
    axes[0].set_xlabel('Year')
    axes[0].set_ylabel('Page Views')
    axes[0].set_title('Year-wise Box Plot (Trend)')
    
    # Plot 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_xlabel('Month')
    axes[1].set_ylabel('Page Views')
    axes[1].set_title('Month-wise Box Plot (Seasonality)')



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

Could you please help me with this?

link to replit:
https://replit.com/@info359/boilerplate-page-view-time-series-visualizer#time_series_visualizer.py

Thanks a lot

Your browser information:

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

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

Link to the challenge:

The test script is trying to measure df and it’s expecting 1 column, but there are multiple columns.

You can find a solution here:

You can read more about copying a dataframe here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html

1 Like

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