1

I am using statsmodels.graphics to draw a month_plot from timeseries data in a kaggle dataset. I have converted the data to daily frequency mean data as required for the plot. However, I am getting an error that says the expected data frequency is D, but the actual data frequency is M where as my actual data is already D.

import pandas as pd
from statsmodels.graphics.tsaplots import month_plot
import matplotlib.pyplot as plt

df = pd.read_csv('/kaggle/input/hourly-energy-consumption/DOM_hourly.csv')
df.set_index('Datetime', inplace=True, drop=True)
df.index = pd.to_datetime(df.index, format='%Y-%m-%d %H:%M:%S')
# drop duplicated index
df = df[~df.index.duplicated(keep='first')]

# convert df to daily mean frequency dataframe
ddf = df.resample(rule='24H', kind='interval').mean().to_period('d')

# print example dataframe ddf
#
#                 DOM_MW
# Datetime  
# 2005-05-01    7812.347826
# 2005-05-02    8608.083333
# ...         ...
# 2017-12-30    14079.125000
# 2017-12-31    15872.833333

# Monthly plot from the Daily frequency data
plt.figure(figsize=(14,4))
month_plot(ddf)
plt.show()

Present output: As you can see above, my ddf is clearly a daily frequency data. But I am getting following weird error saying my ddf data is actually M (Monthly) but it expects D (Daily).

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-675f2911920c> in <module>
      7 
      8 plt.figure(figsize=(14,4))
----> 9 month_plot(ddf)
     10 plt.show()

ValueError: Expected frequency D. Got M
Mainland
  • 4,110
  • 3
  • 25
  • 56

1 Answers1

1

You try to use month_plot to plot daily data. If you want to use month_plot, you have to resample your data first:

>>> help(month_plot)
...
    x : array_like
        Seasonal data to plot. If dates is None, x must be a pandas object
        with a PeriodIndex or DatetimeIndex with a **monthly frequency**.
...
month_plot(ddf.resample('M').mean())
plt.show()

Output:

enter image description here

Corralien
  • 109,409
  • 8
  • 28
  • 52
  • 1
    You are spot on. I appreciate you a lot for going extra mile to see the cause and correct my knowledge about. I over read it and was wrong. I thought it to be Daily but I have to feed monthly frequency data. Now I understood: it actually plotting all years january month data in the Jan label and showing a mean in the red line. – Mainland Jun 15 '23 at 21:39
  • 1
    Yes that's it. You're welcome :) – Corralien Jun 15 '23 at 21:42
  • 1
    I got stuck here for two days, could not move an inch. Thinking what could be wrong. But you are a gem and thanks a lot. People like you make SF a truly humane (helping others but not knowing whom you are helping). This is really good place to be with. No more comments. Thanks again. – Mainland Jun 15 '23 at 21:46