【问题标题】:Split DF on timestamp index when there is a jump in time当有时间跳跃时,在时间戳索引上拆分 DF
【发布时间】:2020-08-05 07:32:35
【问题描述】:

作为示例,我使用以下 DF:

Index                   Temperature
2019-11-14 08:25:30.000 50.776897   
2019-11-14 08:25:40.000 49.671967
2019-11-14 08:25:50.000 48.729610
2019-11-14 08:26:00.000 48.022270
2019-11-14 08:26:10.000 47.502003
2019-11-14 08:43:40.000 87.997314  <-- here i want to split the DF into two DF's
2019-11-14 08:43:50.000 90.791519
2019-11-14 08:44:00.000 93.656128
2019-11-14 08:44:10.000 95.525215
2019-11-14 08:44:20.000 95.694481

所以我使用了一个大的 DF,当时间戳索引出现跳跃时,我想将 DF 拆分为小的 DF。或者换句话说,我想在下一个时间步长大于 10 秒后分割数据帧。

【问题讨论】:

标签: python dataframe split timestamp


【解决方案1】:

我添加了一个组列来根据时间创建不同的组。所以当索引时间差>10s时,会创建一个新组。

然后我制作了一个 DF 列表并使用循环遍历 DF 以根据组号对其进行拆分。

df['groups'] = (df.index.to_series().diff().dt.seconds > 10).cumsum()

list_of_dfs = []
for ct, data in df.groupby('groups'):
    list_of_dfs.append(data)

结果:

Index                   Temperature  groups
2019-11-14 08:25:30.000 50.776897    0
2019-11-14 08:25:40.000 49.671967    0
2019-11-14 08:25:50.000 48.729610    0
2019-11-14 08:26:00.000 48.022270    0
2019-11-14 08:26:10.000 47.502003    0

2019-11-14 08:43:40.000 87.997314    1
2019-11-14 08:43:50.000 90.791519    1
2019-11-14 08:44:00.000 93.656128    1
2019-11-14 08:44:10.000 95.525215    1
2019-11-14 08:44:20.000 95.694481    1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 2018-10-07
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    相关资源
    最近更新 更多