0

我正在尝试重新采样时间序列。我似乎无法让它工作。基于其他示例,我不明白为什么这不返回时间序列:

df1 = pd.DataFrame({'Time': ['2019-08-02 09:50:10.100','2019-08-02 09:50:10.200','2019-08-02 09:50:10.400''2019-08-02 09:50:10.100','2019-08-02 09:50:10.200','2019-08-02 09:50:10.400'], 
                   'Object': ['A','A','A','B','B','B'],
                    })

df1['Time'] = pd.to_datetime(df1['Time'])

df1 = df1.set_index(['Time']).resample('100ms')

print(df1)

出去:

DatetimeIndexResampler [freq=<100 * Millis>, axis=0, closed=left, label=left, convention=start, base=0]

预期输出:

                     Time Object
0 2019-08-02 09:50:10.100      A
1 2019-08-02 09:50:10.200      A
2 2019-08-02 09:50:10.300      Nan
3 2019-08-02 09:50:10.400      A
4 2019-08-02 09:50:10.100      B
5 2019-08-02 09:50:10.200      B
6 2019-08-02 09:50:10.300      Nan
7 2019-08-02 09:50:10.400      B
4

1 回答 1

3

我相信你想要做的是:

df1['Time'] = pd.to_datetime(df1['Time'])

df1.set_index(['Time'], inplace = True)
df1.groupby("Object").resample("100ms").asfreq()

输出是:

                               Object
Object Time                          
A      2019-08-02 09:50:10.100      A
       2019-08-02 09:50:10.200      A
       2019-08-02 09:50:10.300    NaN
       2019-08-02 09:50:10.400      A
B      2019-08-02 09:50:10.100      B
       2019-08-02 09:50:10.200      B
       2019-08-02 09:50:10.300    NaN
       2019-08-02 09:50:10.400      B

如果您愿意,现在可以删除索引的第一级:

df1 = df1.groupby("Object").resample("100ms").asfreq()
df1.index = df1.index.droplevel(0)

输出:

                        Object
Time                          
2019-08-02 09:50:10.100      A
2019-08-02 09:50:10.200      A
2019-08-02 09:50:10.300    NaN
2019-08-02 09:50:10.400      A
2019-08-02 09:50:10.100      B
2019-08-02 09:50:10.200      B
2019-08-02 09:50:10.300    NaN
2019-08-02 09:50:10.400      B
于 2020-08-11T07:05:18.677 回答