【问题标题】:Pandas Date MultiIndex with missing dates - Rolling sum缺少日期的 Pandas Date MultiIndex - 滚动总和
【发布时间】:2017-07-04 20:21:45
【问题描述】:

我有一个熊猫系列,看起来像

Attribute      DateEvent     Value
Type A         2015-04-01    4
               2015-04-02    5
               2015-04-05    3
Type B         2015-04-01    1
               2015-04-03    4
               2015-04-05    1

如何将值转换为滚动总和(例如,过去两天),同时确保在我的 DateEvent 索引中考虑缺失的日期(假设它的开始日期和结束日期是完整的范围?(例如, 2015-04-032015-04-04 对于 A 类缺失,2015-04-022015-04-04 对于 B 类缺失)。

【问题讨论】:

    标签: python pandas datetime indexing


    【解决方案1】:

    我对你想要什么做了几个假设,请澄清

    1. 您希望将缺少日期的行视为具有Value = NaN
    2. 因此,只要滚动窗口中缺少日期,过去 2 天滚动总和应返回 NaN
    3. 您想要计算滚动总和在每个组内 Type AType B

    如果我猜对了,

    创建样本数据集

    import pandas as pd
    import numpy as np
    import io
    
    datastring = io.StringIO(
    """
    Attribute,DateEvent,Value
    Type A,2017-04-02,1
    Type A,2017-04-03,2
    Type A,2017-04-04,3
    Type A,2017-04-05,4
    Type B,2017-04-02,1
    Type B,2017-04-03,2
    Type B,2017-04-04,3
    Type B,2017-04-05,4
    """)
    
    s = pd.read_csv(
                datastring, 
                index_col=['Attribute', 'DateEvent'],
                parse_dates=True)
    print(s)
    

    这就是它的样子。 Type AType B 中的每一个都缺少 2017-04-01

                          Value
    Attribute DateEvent        
    Type A    2017-04-02      1
              2017-04-03      2
              2017-04-04      3
              2017-04-05      4
    Type B    2017-04-02      1
              2017-04-03      2
              2017-04-04      3
              2017-04-05      4
    

    解决方案

    根据this answer,您必须重建索引,然后重新索引您的Series 以获得包含所有日期的索引。

    # reconstruct index with all the dates
    dates = pd.date_range("2017-04-01","2017-04-05", freq="1D")
    attributes = ["Type A", "Type B"]
    # create a new MultiIndex
    index = pd.MultiIndex.from_product([attributes,dates], 
            names=["Attribute","DateEvent"])
    # reindex the series
    sNew = s.reindex(index)
    

    添加了缺少的日期,Value = NaN

                          Value
    Attribute DateEvent        
    Type A    2017-04-01    NaN
              2017-04-02    1.0
              2017-04-03    2.0
              2017-04-04    3.0
              2017-04-05    4.0
    Type B    2017-04-01    NaN
              2017-04-02    1.0
              2017-04-03    2.0
              2017-04-04    3.0
              2017-04-05    4.0
    

    现在将SeriesAttribute 索引列分组,并应用大小为2sum() 的滚动窗口

    # group the series by the `Attribute` column
    grouped = sNew.groupby(level="Attribute")
    # Apply a 2 day rolling window
    summed = grouped.rolling(2).sum()
    

    最终输出

                                    Value
    Attribute Attribute DateEvent        
    Type A    Type A    2017-04-01    NaN
                        2017-04-02    NaN
                        2017-04-03    3.0
                        2017-04-04    5.0
                        2017-04-05    7.0
    Type B    Type B    2017-04-01    NaN
                        2017-04-02    NaN
                        2017-04-03    3.0
                        2017-04-04    5.0
                        2017-04-05    7.0
    

    最后说明:不知道为什么现在有两个 Attribute 索引列,如果有人知道,请告诉我。

    编辑:结果类似的问题被问到here。看看吧。

    来源:How to fill in missing values with a multiIndex

    【讨论】:

      猜你喜欢
      • 2017-06-27
      • 2021-01-16
      • 2020-06-05
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多