繁体   English   中英

是否可以根据日期时间(按组)对日期进行插值或前向填充?

[英]Is it possible to interpolate, or forward fill, on date with respect to datetime (by group)?

最终目标:计算每日差异(使用一阶差分,一阶差分估计器)。

我通过在日期时间中按 ID 插入缺失日期时间的行来插入 dataframe:

df = df.drop_duplicates(subset=['ID','datetime'])
df_new = df.set_index('datetime').groupby('ID').resample('D').pad()

最重要的问题是生成的 df_new 不包括所有初始日期时间。 在 df 中的某些日期时间有多个事件。 似乎只有日期时间的最后一个事件被保存在 df_new 中。 有没有办法解决这个问题,以便保留所有先前的事件,仅通过没有日期时间值的日期进行插值?

另一个不太重要的问题是插值会移动日期时间或在日期时间上增加一天。 是否可以在不“移动”日期的情况下插入到另一个事件?

before interpolation

  event_type            datetime
       type1 2021-09-15 12:10:44
       type2 2021-09-15 12:38:58
       type3 2021-09-15 13:20:31
       type4 2022-08-04 14:45:09
       type2 2022-08-09 13:01:09
       type3 2022-08-09 14:15:30
       type5 2022-08-18 13:40:38
       type6 2022-09-25 16:04:07

after interpolation

  event_type   datetime
       type3 2021-09-16
            ...
       type4 2022-08-05
            ...
       type3 2022-08-10
            ...
       type5 2022-08-19
            ...
       type6 2022-09-26 --- I guess, that is tomorrow...tbc
            ...

请注意,我在显示的表格中重命名了变量和值,这是出于谨慎考虑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM