【问题标题】:Replace duplicated time index and fullfilling by time interpolation替换重复的时间索引并通过时间插值实现
【发布时间】:2023-03-18 16:49:02
【问题描述】:

我有一个时间戳错误的数据框

时间索引错误,不是以 1 分钟为周期采样,而是包含 10 分钟倍数的重复索引

2021-08-01 00:00:00
2021-08-01 00:00:00
2021-08-01 00:00:00
2021-08-01 00:00:00
...
2021-08-01 00:10:00
2021-08-01 00:10:00
....
2021-08-01 00:20:00
2021-08-01 00:20:00
... and so on

后处理后想要的结果应该是

2021-08-01 00:00:00
2021-08-01 00:01:00
2021-08-01 00:02:00
2021-08-01 00:03:00
...
2021-08-01 00:10:00
2021-08-01 00:11:00
...and so on

我一直在尝试使用 pandas.index 函数来用 nans 填充重复的索引,然后插值到 1 分钟但没有成功

有什么提示吗?

【问题讨论】:

    标签: python pandas datetime reindex


    【解决方案1】:

    你可以通过 GroupBy.cumcountto_timedelta 的重复索引通过计数器将 timedeltas 添加 1 分钟:

    print (df)
                         b
    a                     
    2021-08-01 00:00:00  1
    2021-08-01 00:00:00  1
    2021-08-01 00:00:00  1
    2021-08-01 00:00:00  1
    2021-08-01 00:10:00  1
    2021-08-01 00:10:00  1
    2021-08-01 00:20:00  1
    2021-08-01 00:20:00  1
    
    df.index = pd.to_datetime(df.index)
    
    df.index += pd.to_timedelta(df.groupby(level=0).cumcount(), 'Min')
    print (df)
                         b
    2021-08-01 00:00:00  1
    2021-08-01 00:01:00  1
    2021-08-01 00:02:00  1
    2021-08-01 00:03:00  1
    2021-08-01 00:10:00  1
    2021-08-01 00:11:00  1
    2021-08-01 00:20:00  1
    2021-08-01 00:21:00  1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      相关资源
      最近更新 更多