Cannot convert the series to class 'int'

I’m currently working on trying the do the projects of the data analysis with python course. I’m stuck on the Page View Time Series Visualizer. It keeps giving me an Error saying that it cannot conver the series to type int. could you guys help me.
This is the code currently;

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'], index_col = 'date',dtype={'value': int})

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


def draw_line_plot():
    # Draw line plot
    fig = plt.figure(figsize=(6, 3))
    plt.plot(df.index, df['value']),
    plt.title('Daily freeCodeCamp Forum Page Views 5/2016-12/2019'),
    plt.xlabel('Date'),
    plt.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= (13,6), ylabel = 'Average Page Views', xlabel= 'Years').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

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(nrows=1, ncols=2, figsize=(10,5))
    axes[0] = sns.boxplot(x=df_box['year'], y=df_box['value'], ax= axes[0])
    axes[1] = sns.boxplot(x=df_box['month'], y=df_box['value'], ax= axes[1])

    axes[0].set_title('Year-wise Box Plot (Trend)')
    axes[0].set_ylabel('Page Views')
    axes[0].set_xlabel('Year')

    axes[1].set_title('Month-wise Box Plot (Seasonality)')
    axes[1].set_ylabel('Page Views')
    axes[1].set_xlabel('Month')

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

Could you paste complete traceback of the error?

This is the function that raises the error in the test section of the project:

class DataCleaningTestCase(unittest.TestCase):
    def test_data_cleaning(self):
        actual = int(time_series_visualizer.df.count(numeric_only=True))
        expected = 1238
        self.assertEqual(actual, expected, "Expected DataFrame count after cleaning to be 1238.")

The error states it’s trying to convert a series-object, but can’t.
The test itself is doing nothing but going into your code, taking out df and counting values.

How can this fail? The answer is: you apply a change to df, that makes the transformation impossible.
So look at your code and remove the part where you change df :wink:

Explanation for the error:

The error basically happens, because df.count() returns more than one number, at which point it cannot be cast into an int.
That’s because you added numeric columns year and month to the dataframe in the draw_bar_plot. Then .count() returns the count for all 3 columns, which will be saved as a series-object. However because it’s now 3 numbers, those cannot be turned into a single integer.

1 Like

Thank you very much, that was the solution!

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