【问题标题】:How to impute missing value in time series data with the value of the same day and time from the previous week(day) in python如何用python中前一周(天)的同一天和同一时间的值来估算时间序列数据中的缺失值
【发布时间】:2021-06-07 14:57:20
【问题描述】:

我有一个包含时间戳和能源使用列的数据框。时间戳记为一天中的每一分钟,即每天总共有 1440 个读数。数据框中的缺失值很少。

我想用过去两到三周的同一天、同一时间的平均值来估算这些缺失值。这样,如果前一周也缺失,我可以使用两周前的值。

以下是数据示例:

                    mains_1
timestamp   
2013-01-03 00:00:00 155.00
2013-01-03 00:01:00 154.00
2013-01-03 00:02:00 NaN
2013-01-03 00:03:00 154.00
2013-01-03 00:04:00 153.00
... ...
2013-04-30 23:55:00 NaN
2013-04-30 23:56:00 182.00
2013-04-30 23:57:00 181.00
2013-04-30 23:58:00 182.00
2013-04-30 23:59:00 182.00

现在我有这行代码:

df['mains_1'] = (df
    .groupby((df.index.dayofweek * 24) + (df.index.hour) + (df.index.minute / 60))
    .transform(lambda x: x.fillna(x.mean()))
)

因此,它使用了整个数据集当天同一小时的平均使用量。我希望它更精确,并使用过去两三周的平均值。

【问题讨论】:

    标签: python pandas time-series missing-data


    【解决方案1】:

    您可以在循环中将concatshift 一起使用,因为索引对齐将确保它在前几周与同一小时匹配。然后取mean,用.fillna更新原版

    样本数据

    import pandas as pd
    import numpy as np
    
    np.random.seed(5)
    df = pd.DataFrame(index=pd.date_range('2010-01-01 10:00:00', freq='W', periods=10),
                      data = np.random.choice([1,2,3,4, np.NaN], 10),
                      columns=['mains_1'])
    #                     mains_1
    #2010-01-03 10:00:00      4.0
    #2010-01-10 10:00:00      1.0
    #2010-01-17 10:00:00      2.0
    #2010-01-24 10:00:00      1.0
    #2010-01-31 10:00:00      NaN
    #2010-02-07 10:00:00      4.0
    #2010-02-14 10:00:00      1.0
    #2010-02-21 10:00:00      1.0
    #2010-02-28 10:00:00      NaN
    #2010-03-07 10:00:00      2.0
    

    代码

    # range(4) for previous 3 weeks. 
    df1 = pd.concat([df.shift(periods=x, freq='W') for x in range(4)], axis=1)
    #                     mains_1  mains_1  mains_1  mains_1
    #2010-01-03 10:00:00      4.0      NaN      NaN      NaN
    #2010-01-10 10:00:00      1.0      4.0      NaN      NaN
    #2010-01-17 10:00:00      2.0      1.0      4.0      NaN
    #2010-01-24 10:00:00      1.0      2.0      1.0      4.0
    #2010-01-31 10:00:00      NaN      1.0      2.0      1.0
    #2010-02-07 10:00:00      4.0      NaN      1.0      2.0
    #2010-02-14 10:00:00      1.0      4.0      NaN      1.0
    #2010-02-21 10:00:00      1.0      1.0      4.0      NaN
    #2010-02-28 10:00:00      NaN      1.0      1.0      4.0
    #2010-03-07 10:00:00      2.0      NaN      1.0      1.0
    #2010-03-14 10:00:00      NaN      2.0      NaN      1.0
    #2010-03-21 10:00:00      NaN      NaN      2.0      NaN
    #2010-03-28 10:00:00      NaN      NaN      NaN      2.0
    
    df['mains_1'] = df['mains_1'].fillna(df1.mean(axis=1))
    

    print(df)
    
                          mains_1
    2010-01-03 10:00:00  4.000000
    2010-01-10 10:00:00  1.000000
    2010-01-17 10:00:00  2.000000
    2010-01-24 10:00:00  1.000000
    2010-01-31 10:00:00  1.333333
    2010-02-07 10:00:00  4.000000
    2010-02-14 10:00:00  1.000000
    2010-02-21 10:00:00  1.000000
    2010-02-28 10:00:00  2.000000
    2010-03-07 10:00:00  2.000000
    

    【讨论】:

    • 感谢您的帮助。当我尝试连接系列时,我收到此错误:Shape of passed values is (332640, 4), indices imply (182880, 4) 另外,这种方法是否考虑到分钟?正如我所提到的,我的时间序列分辨率是 1 分钟。
    • @AshkanLotfipoor 它确实需要考虑分钟和秒,因为索引对齐需要完全匹配,我们只是在日期上加上 7 天,包括时间。我不确定这个错误,也许你有重复的东西搞砸了?
    • 谢谢,我想通了。效果很好。
    猜你喜欢
    • 2021-07-08
    • 2021-07-20
    • 2021-07-01
    • 2022-12-06
    • 2021-06-21
    • 2013-03-18
    • 1970-01-01
    • 2020-11-17
    • 2019-01-13
    相关资源
    最近更新 更多