首页 > 解决方案 > 限制python中2个持续时间之间的时间戳列?

问题描述

我有以下数据框。

df = {'location_id': [1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5],
      'timestamp':['2020-05-26 06:00:00','2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', 
                 '2020-05-26 10:00:00','2020-05-26 11:00:00','2020-05-26 12:00:00', '2020-05-26 13:00:00',
                 '2020-05-26 06:00:00','2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', 
                 '2020-05-26 10:00:00','2020-05-26 11:00:00','2020-05-26 12:00:00', '2020-05-26 13:00:00',
                 '2020-05-26 06:00:00','2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', 
                 '2020-05-26 10:00:00','2020-05-26 11:00:00','2020-05-26 12:00:00', '2020-05-26 13:00:00',
                 '2020-05-26 06:00:00','2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', 
                 '2020-05-26 10:00:00','2020-05-26 11:00:00','2020-05-26 12:00:00', '2020-05-26 13:00:00',
                 '2020-05-26 06:00:00','2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', 
                 '2020-05-26 10:00:00','2020-05-26 11:00:00','2020-05-26 12:00:00', '2020-05-26 13:00:00']
     }
dataframe = pd.DataFrame(df)

每个 location_id 都有一个从 2020-05-06 06:00:00 到 2020-05-26 13:00:00 的时间戳。我想要实现的是限制每个 location_id 的时间戳,以便所有 id 的时间戳仅在 07:00:00 和 10:00:00 之间。

最终数据框应如下所示:

df = {'location_id': [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5],
      'timestamp':['2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', '2020-05-26 10:00:00',
                   '2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', '2020-05-26 10:00:00',
                   '2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', '2020-05-26 10:00:00',
                   '2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', '2020-05-26 10:00:00',
                   '2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', '2020-05-26 10:00:00']
     }
dataframe = pd.DataFrame(df)

到目前为止,我尝试的是以下内容:

dataframe[(dataframe['timestamp'] >= '2020-05-26 07:00:00') & (dataframe['timestamp'] <= '2020-05-26 10:00:00')]

在这个例子中,它可以工作,但是在大型数据集上应用它时,它并没有给我正确的答案。我尝试根据 location_id 和时间戳对值进行排序,然后应用相同的条件,但仍然无法正常工作。它总是占用整个数据帧。如何实现?请注意,这是取自一个非常大的数据集的示例。(13,000,000 行和 2 列,每个 location_id 的实际时间戳在 2020-05-26 00:00:00 到 2020-07-05 23:00:00 之间) . 如果有人能给我一个解决方案,我将不胜感激:)!

标签: pythonpandas

解决方案


set_index在您的时间列上并使用between_time

df = pd.DataFrame(df)

df["timestamp"] = pd.to_datetime(df["timestamp"])

print (df.set_index("timestamp").between_time("07:00","11:00"))

                     location_id
timestamp                       
2020-05-26 07:00:00            1
2020-05-26 08:00:00            1
2020-05-26 09:00:00            1
2020-05-26 10:00:00            1
2020-05-26 11:00:00            1
2020-05-26 07:00:00            2
2020-05-26 08:00:00            2
2020-05-26 09:00:00            2
2020-05-26 10:00:00            2
2020-05-26 11:00:00            2
2020-05-26 07:00:00            3
2020-05-26 08:00:00            3
2020-05-26 09:00:00            3
2020-05-26 10:00:00            3
2020-05-26 11:00:00            3
2020-05-26 07:00:00            4
2020-05-26 08:00:00            4
2020-05-26 09:00:00            4
2020-05-26 10:00:00            4
2020-05-26 11:00:00            4
2020-05-26 07:00:00            5
2020-05-26 08:00:00            5
2020-05-26 09:00:00            5
2020-05-26 10:00:00            5
2020-05-26 11:00:00            5

推荐阅读