英文:
How can I filter between two datetime timestamps in a dataframe?
问题
我有以下的数据框:
price_lvl_size_total raw_end_of_event receive_timestamp
154 0 True 2023-05-29 00:15:01.000138338
160 0 True 2023-05-29 00:15:01.138503551
我想要筛选出在上述两个时间戳之间的行。
我尝试过:
dateframe.between_time(2023-5-29 00:15:01.000138338, 2023-05-29 00:15:01.138503551)
但是我收到了一个invalid syntax
错误。我也尝试将日期时间放入字符串中,但是我得到了以下错误:
TypeError: Index must be DatetimeIndex
英文:
I have the following dataframe:
price_lvl_size_total raw_end_of_event receive_timestamp
154 0 True 2023-05-29 00:15:01.000138338
160 0 True 2023-05-29 00:15:01.138503551
And I would like to filter for rows between the above two timestamps.
I have tried:
dateframe.between_time(2023-5-29 00:15:01.000138338, 2023-05-29 00:15:01.138503551)
But I get a invalid syntax
error. I have also tried putting the datetime inside strings but I get
TypeError: Index must be DatetimeIndex
答案1
得分: 1
# (if needed) to make sure the timestamps are cast to int64
# file["receive_timestamp"] = file["receive_timestamp"].astype("int64")
out = file.loc.between(1685319301000138338, 1685319301138503551)]
#variant ?
out = file.loc[pd.to_datetime(file["receive_timestamp"]).dt.microsecond.between(138, 138503)]
print(out)
price_lvl_size_total raw_end_of_event receive_timestamp
154 0 True 1685319301000138338
160 0 True 1685319301138503551
***Update :***
Regarding the updated question, you can use :
# (if needed)
#file["receive_timestamp"] = pd.to_datetime(file["receive_timestamp"])
start, end = "2023-05-29 00:15:01.000138338", "2023-05-29 00:15:01.138503551"
out = file.loc.between(start, end)]
英文:
Don't you need to parse the timestamps first ? Anyways, you can try this :
# (if needed) to make sure the timestamps are cast to int64
# file["receive_timestamp"] = file["receive_timestamp"].astype("int64")
out = file.loc.between(1685319301000138338, 1685319301138503551)]
#variant ?
out = file.loc[pd.to_datetime(file["receive_timestamp"]).dt.microsecond.between(138, 138503)]
Output :
print(out)
price_lvl_size_total raw_end_of_event receive_timestamp
154 0 True 1685319301000138338
160 0 True 1685319301138503551
Update :
Regarding the updated question, you can use :
# (if needed)
#file["receive_timestamp"] = pd.to_datetime(file["receive_timestamp"])
start, end = "2023-05-29 00:15:01.000138338", "2023-05-29 00:15:01.138503551"
out = file.loc.between(start, end)]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论