Boilerplate-page-view-time-series-visualizer - Series to Int Query

Hey, I’ve had this issue with the page-view-time-series-visualizer. The unit-test pops up an error stating

TypeError: cannot convert the series to <class ‘int’

I understand that the line

actual = int(time_series_visualizer.df.count(numeric_only=True))

means that only one column will be counted.

Previous posts state they removed the date column, however this breaks the rest of the code.

Another code stated that replacing that line with this one

actual = int(time_series_visualizer.df.count()[0])

Fixed the error created, as now it will count 1 column. This solved my problem too however I understand that amending the unittest is not allowed, so does anyone have any advice on fixing this issue.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
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"], index_col='date')
df.reset_index(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["date"], df["value"])
    plt.title("Daily freeCodeCamp Forum Page Views 5/2016-12/2019")
    plt.xlabel("Date")
    plt.ylabel("Page Views")
    pos = np.arange(len(df['date']))
    ticks = plt.xticks(pos[::160], df['date'].values[::160], rotation=0)

    # 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["month"] = df.date.dt.month
    df["year"] = df.date.dt.year
    df_bar = df.groupby(["year", "month"])["value"].mean()
    df_bar = df_bar.unstack()

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

    # 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=(10,5), dpi= 80)
    sns.boxplot(x='year', y='value', data=df_box, ax=axes[0]).set(xlabel = "Year", ylabel = "Page Views", title = "Year-wise Box Plot (Trend)")
    sns.boxplot(x='month',y='value',data=df_box,order=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],ax=axes[1]).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

I would greatly appreciate any help. Thank you

Heres my replit link if you would like to see my code!

You’re actually changing df here, not a copy. These modifications are breaking the test on df that the tests use.

2 Likes

Try to change the current df for a copy.
You have assigned the variable in his case

but not in the subsequent

Use the same df_bar

the problem is in the “kind” of df that you are using
hope this helps.

1 Like

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