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

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

Python StatsModels: ValueError: Expected frequency D. Got M

问题

我正在使用 statsmodels.graphicskaggle 数据集 中的时间序列数据绘制一个 month_plot。我已将数据转换为所需的每日频率均值数据以进行绘图。然而,我收到了一个错误,错误信息说“期望的数据频率是 D,但实际的数据频率是 M”,而我的实际数据已经是 D。

  1. import pandas as pd
  2. from statsmodels.graphics.tsaplots import month_plot
  3. import matplotlib.pyplot as plt
  4. df = pd.read_csv('/kaggle/input/hourly-energy-consumption/DOM_hourly.csv')
  5. df.set_index('Datetime', inplace=True, drop=True)
  6. df.index = pd.to_datetime(df.index, format='%Y-%m-%d %H:%M:%S')
  7. # 去除重复的索引
  8. df = df[~df.index.duplicated(keep='first')]
  9. # 将 df 转换为每日均值频率的数据框
  10. ddf = df.resample(rule='24H', kind='interval').mean().to_period('d')
  11. # 打印示例数据框 ddf
  12. #
  13. # DOM_MW
  14. # Datetime
  15. # 2005-05-01 7812.347826
  16. # 2005-05-02 8608.083333
  17. # ... ...
  18. # 2017-12-30 14079.125000
  19. # 2017-12-31 15872.833333
  20. # 从每日频率数据绘制月度图
  21. plt.figure(figsize=(14,4))
  22. month_plot(ddf)
  23. plt.show()

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

  1. ---------------------------------------------------------------------------
  2. ValueError Traceback (most recent call last)
  3. <ipython-input-7-675f2911920c> in <module>
  4. 7
  5. 8 plt.figure(figsize=(14,4))
  6. ----> 9 month_plot(ddf)
  7. 10 plt.show()
  8. 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.

  1. import pandas as pd
  2. from statsmodels.graphics.tsaplots import month_plot
  3. import matplotlib.pyplot as plt
  4. df = pd.read_csv('/kaggle/input/hourly-energy-consumption/DOM_hourly.csv')
  5. df.set_index('Datetime', inplace=True, drop=True)
  6. df.index = pd.to_datetime(df.index, format='%Y-%m-%d %H:%M:%S')
  7. # drop duplicated index
  8. df = df[~df.index.duplicated(keep='first')]
  9. # convert df to daily mean frequency dataframe
  10. ddf = df.resample(rule='24H', kind='interval').mean().to_period('d')
  11. # print example dataframe ddf
  12. #
  13. # DOM_MW
  14. # Datetime
  15. # 2005-05-01 7812.347826
  16. # 2005-05-02 8608.083333
  17. # ... ...
  18. # 2017-12-30 14079.125000
  19. # 2017-12-31 15872.833333
  20. # Monthly plot from the Daily frequency data
  21. plt.figure(figsize=(14,4))
  22. month_plot(ddf)
  23. 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).

  1. ---------------------------------------------------------------------------
  2. ValueError Traceback (most recent call last)
  3. <ipython-input-7-675f2911920c> in <module>
  4. 7
  5. 8 plt.figure(figsize=(14,4))
  6. ----> 9 month_plot(ddf)
  7. 10 plt.show()
  8. ValueError: Expected frequency D. Got M

答案1

得分: 1

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

  1. >>> 帮助(month_plot)
  2. ...
  3. x: array_like
  4. 要绘制的季节性数据。如果未提供日期,则 x 必须是带有**月度频率**的 Pandas 对象,具有 PeriodIndex DatetimeIndex
  5. ...
  1. month_plot(ddf.resample('M').mean())
  2. 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:

  1. >>> help(month_plot)
  2. ...
  3. x : array_like
  4. Seasonal data to plot. If dates is None, x must be a pandas object
  5. with a PeriodIndex or DatetimeIndex with a **monthly frequency**.
  6. ...
  1. month_plot(ddf.resample('M').mean())
  2. 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:

确定