python - 缺少日期的 Pandas Date MultiIndex - 滚动总和

标签 python pandas datetime indexing

我有一个 pandas 系列,看起来像

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 缺失)。

最佳答案

我对你想要的做了几个假设,请澄清:

  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

现在将 Series 分组通过 Attribute索引列并应用大小为 2 的滚动窗口与 sum()

# 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

关于python - 缺少日期的 Pandas Date MultiIndex - 滚动总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42261823/

相关文章:

python - 将 linspace 向量发送到函数会使该向量在函数启动之前全部为零

python - 用于 Python 的 OpenCV - AttributeError : 'module' object has no attribute 'connectedComponents'

python - Pandas:解析 CSV,区分缺失值哨兵和恰好等于它的字符串

PHP: 查找两个日期之间的天差 ("YmdHis") 返回

c# - 如何在特定时区的 DateTime 中添加一天

python - 解码 Scapy ASN1 编码的 SSL/TLS 证书字段

python - 根据组过滤 DataFrame 行

python - 如何使用 pandas 用名称替换变量

c# - Controller 未收到英国格式的 MVC3 日期

python - 为什么我的 IDE 无法自动完成 python-docx?