【问题标题】:ValueError: window must be an integer 0 or greater in rollingValueError:窗口在滚动时必须是整数 0 或更大
【发布时间】:2021-12-15 19:33:22
【问题描述】:

我想在我的数据集中计算 7 天的滚动平均值。

transaction_week.dtypes

transaction_week.dtypes
zisk_obj               float64
datetime_add    datetime64[ns]
dtype: object

这个:

transaction_week["mean_weeks_zisk_obj"] = transaction_week.rolling('7D').mean()

还有这个:

transaction_week["mean_weeks_zisk_obj"] = transaction_week.reset_index().rolling('7D').mean()

给出同样的错误:

Traceback (most recent call last):
  File "/usr/lib/python3.8/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/home/vojtam/Desktop/poptavka_cena/venv/lib/python3.8/site-packages/pandas/core/generic.py", line 10868, in rolling
    return Rolling(
  File "/home/vojtam/Desktop/poptavka_cena/venv/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 158, in __init__
    self.validate()
  File "/home/vojtam/Desktop/poptavka_cena/venv/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 1532, in validate
    raise ValueError("window must be an integer 0 or greater")
ValueError: window must be an integer 0 or greater

请问我该如何解决?

这个问题对我没有帮助:Rolling Mean with Time Offset Pandas

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    我自带了这个解决方案,但一定有更好的解决方案:

    transaction_week['week'] = transaction_week['datetime_add'].apply(lambda x: x.week)
    transaction_week['mean_weeks_zisk_obj'] = transaction_week.groupby(['week'])["zisk_obj"].transform('mean')
    

    【讨论】:

      猜你喜欢
      • 2018-11-07
      • 2018-10-25
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      相关资源
      最近更新 更多