Page View Time Series Visualizer issue

Hi everyone,

I’m running the code below without issues in Google Colab but will get the following error when testing in replit.

Thanks in advance for your assistance.


File “/home/runner/boilerplate-page-view-time-series-visualizer-2/”, line 32, in draw_bar_plot
AttributeError: ‘function’ object has no attribute ‘groupby’

My code:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from pandas.plotting import register_matplotlib_converters

df = pd.read_csv(‘fcc-forum-pageviews.csv’)
df = df.set_index(‘date’)
df.index = pd.to_datetime(df.index)
df = df[(df.iloc[:,0] >= df.iloc[:,0].quantile(0.025))&
(df.iloc[:,0] <= df.iloc[:,0].quantile(0.975))

def draw_bar_plot():
df_bar = df.copy()
df_bar[‘Months’]= pd.to_datetime(df_bar[‘Months’])
df_bar[‘years’]= pd.to_datetime(df_bar[‘years’])
indextest=pd.date_range(start=‘1/31/2016’, periods=4, freq =‘M’)
test = pd.DataFrame(np.zeros(4),index=indextest)‘date’
test[‘Months’]= pd.to_datetime(test[‘Months’])
test[‘years’]= pd.to_datetime(test[‘years’])
df_bar = pd.concat([test,df_bar])
fig, ax = plt.subplots(figsize=(6.65,7.57))
ax.set(xlabel=‘Years’, ylabel=‘Average Page Views’)

You’ll need to post a link to the notebook or repl on or use a code block and indent properly because this doesn’t seem like all the code and it’s hard to debug as-is.

I copied it into a working project and fixed it up to run and it did not have the error you mention. It did error on the undefined result later on.

Thanks for you reply, I was able to resolve the original issue but now I’m getting a new error that I’m not able to understand:

ERROR: test_box_plot_number_of_boxes (test_module.BoxPlotTestCase)

Traceback (most recent call last):
File “/home/runner/boilerplate-page-view-time-series-visualizer-1/”, line 68, in setUp
self.fig = time_series_visualizer.draw_box_plot()
File “/home/runner/boilerplate-page-view-time-series-visualizer-1/”, line 70, in draw_box_plot
df_box[‘year’] = [d.year for d in]
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/”, line 5575, in getattr
return object.getattribute(self, name)
AttributeError: ‘DataFrame’ object has no attribute ‘date’

Here’s a link to my repl:
repl link

It’s your data cleaning, for now anyway. When you load the data

# Import data (Make sure to parse dates. Consider setting index column to 'date'.)
df =pd.read_csv('fcc-forum-pageviews.csv')
df = df.set_index('date')
df.index = pd.to_datetime(df.index).date

I think you are trying to parse the dates to datetimes, but I don’t think that’s what the third line is doing (I didn’t investigate past “doesn’t work”). pandas.read_csv() will parse the dates to datetimes for you and I know that works; check the documentation for details.

If you print(df) after every step, you’ll see that the date column name disappears during the transformations. That is the cause of your missing attribute error.

It looks like the error is related to setting first the 1st column as index and then converting the index to datetime, rather then first converting the 1st column to datetime and then setting said column to the index. In other words, flipping:
df = df.set_index(‘date’)
df.index = pd.to_datetime(df.index)

fixes the issue. I am not sure why though as both solutions produce same result when I test in my notebook.

Thanks for your assistance

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