【问题标题】:Resample with asymmetrical timeperiods in pandas (8 months then 4 months)在 pandas 中以不对称的时间段重新采样(8 个月然后 4 个月)
【发布时间】:2020-06-04 17:27:57
【问题描述】:

我想重新采样 20 年的每日数据,以便将数据分解为 3 月 - 10 月和 11 月 - 2 月。基本上是8个月和4个月的块。有没有办法使用 resample 函数来做到这一点?

我认为 DateOffset'8MS' 将每 8 个月重新采样一次。有没有办法让它交替 8 个月和 4 个月的周期?

我实际上并不真正需要 4 个月期间的数据,因此我认为替代方法可能是删除该数据并将其保持在 8MS。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    由于您不需要这 4 个月的数据,最简单的方法是过滤这些月份,然后您可以按年使用 resamplegroupby。假设索引是DatetimeIndex,那么你可以这样做:

    #mask to keep months in [3, 4, ...10]
    m = df.index.month.isin(range(3, 11))
    
    #with resample for example
    df[m].resample('Y').mean()
    
    #with groupby
    df[m].groupby(df.index.year[m]).mean()
    

    除了索引名称之外的类似结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-30
      • 2019-09-17
      • 2020-08-18
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 2016-12-23
      相关资源
      最近更新 更多