简体   繁体   English

填写缺少的日期时间 pandas

[英]fill missing datetime pandas

I have a following problem.我有以下问题。 I have this df with 10Min interval:我有这个间隔 10 分钟的 df:

df_dict = {"value" : [1, 1, 2, 3], "datetime" : ["2022-09-05 07:20:00", "2022-09-05 07:30:00", "2022-09-05 07:20:00", "2022-09-05 07:20:00"],
           "expedice" : ["A", "A", "B", "C"] }

df = pd.DataFrame(df_dict)

I would like to fill missing datetime to have:我想填写缺少的日期时间:

df_dict = {"value" : [1, 1, 2, 0, 3, 0], "datetime" : ["2022-09-05 07:20:00", "2022-09-05 07:30:00", "2022-09-05 07:20:00", "2022-09-05 07:30:00", "2022-09-05 07:20:00", "2022-09-05 07:30:00"],
           "expedice" : ["A", "A", "B", "B", "C", "C"] }

df = pd.DataFrame(df_dict)

I tried我试过了

df.datetime = pd.to_datetime(df.datetime)


df.set_index(
            ['datetime', 'expedice']
        ).unstack(
            fill_value=0
        ).asfreq(
            "10Min", fill_value=0
        ).stack().sort_index(level=1).reset_index()

But I got an error TypeError: Cannot change data-type for object array.但我收到一个错误TypeError: Cannot change data-type for object array. . . How can I fix it please?请问我该如何解决?

Use DataFrame.reindex with DatetimeIndex created by minimal and maximal datetime:DataFrame.reindex与由最小和最大日期时间创建的 DatetimeIndex 一起使用:

df1 = df.set_index(['expedice', 'datetime'])


df1 = (df1.reindex(pd.MultiIndex.from_product([df1.index.levels[0], 
                                               pd.date_range(df1.index.levels[1].min(), 
                                                             df1.index.levels[1].max(), 
                                                             freq='10Min')], 
                                              names=df1.index.names), fill_value=0)
         .reset_index())
print (df1)
  expedice            datetime  value
0        A 2022-09-05 07:20:00      1
1        A 2022-09-05 07:30:00      1
2        B 2022-09-05 07:20:00      2
3        B 2022-09-05 07:30:00      0
4        C 2022-09-05 07:20:00      3
5        C 2022-09-05 07:30:00      0

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

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