Python StatsModels: ValueError: 预期频率为D。得到M。

huangapple go评论103阅读模式
英文:

Python StatsModels: ValueError: Expected frequency D. Got M

问题

我正在使用 statsmodels.graphicskaggle 数据集 中的时间序列数据绘制一个 month_plot。我已将数据转换为所需的每日频率均值数据以进行绘图。然而,我收到了一个错误,错误信息说“期望的数据频率是 D,但实际的数据频率是 M”,而我的实际数据已经是 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')
# 去除重复的索引
df = df[~df.index.duplicated(keep='first')]

# 将 df 转换为每日均值频率的数据框
ddf = df.resample(rule='24H', kind='interval').mean().to_period('d')

# 打印示例数据框 ddf
#
#                 DOM_MW
# Datetime 	
# 2005-05-01 	7812.347826
# 2005-05-02 	8608.083333
# ...         ...
# 2017-12-30 	14079.125000
# 2017-12-31 	15872.833333

# 从每日频率数据绘制月度图
plt.figure(figsize=(14,4))
month_plot(ddf)
plt.show()

当前输出:如上所示,我的 ddf 明显是每日频率数据。但我收到了以下奇怪的错误,说我的 ddf 数据实际上是 M(月度),但它期望的是 D(每日)。

---------------------------------------------------------------------------
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
英文:

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

答案1

得分: 1

你尝试使用 month_plot 绘制每日数据。如果想使用 month_plot,必须首先对数据进行重新采样:

>>> 帮助(month_plot)
...
    x: array_like
        要绘制的季节性数据。如果未提供日期,则 x 必须是带有**月度频率**的 Pandas 对象,具有 PeriodIndex 或 DatetimeIndex。
...
month_plot(ddf.resample('M').mean())
plt.show()

输出:

Python StatsModels: ValueError: 预期频率为D。得到M。

英文:

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:

Python StatsModels: ValueError: 预期频率为D。得到M。

huangapple
  • 本文由 发表于 2023年6月16日 05:25:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定