dt.time.between以熊猫为单位返回空列

问题描述 投票:3回答:1
slots = pd.DataFrame({'times': ['2020-02-01 18:40:00', '2020-02-01 08:40:00',
                                '2020-02-01 03:40:00', '2020-02-01 14:40:00',
                                '2010-05-05 22:00:00', '2018-03-08 23:00:00']})
print(slots)
slots['times'] = pd.to_datetime(slots.times)

from datetime import datetime
start = datetime.strptime('17:09:00', '%H:%M:%S').time()
print(start)
end = datetime.strptime('01:59:00', '%H:%M:%S').time()
print(end)


print(slots[slots['times'].dt.time.between(start, end)])


output: Empty DataFrame
Columns: [times]
Index: []

我正在获取空的数据框。有人可以指导还是有其他方法可以做到这一点。

python pandas datetime
1个回答
1
投票

Pandas有方法DataFrame.between_time,所以我建议使用它,因为方法与DataFrame.between_time一起使用,所以还添加了DataFrame.set_indexDataFrame.set_index

DataFrame.reset_index
© www.soinside.com 2019 - 2024. All rights reserved.