Pandas 确实有一个内置方法 between_time() 来选择特定时间段内的行,但它只适用于日期时间对象是索引(或最近的列)。
由于您已经有一个 datetime 列,您可以像这样提取行(改编自 here 的示例):
import pandas as pd
data = [["20090102 04:51:00", 89.9900, 89.9900, 89.9900, 89.9900, 100], ["20190102 05:36:00", 90.0100, 90.0100, 90.0100, 90.0100, 200], ["20090102 05:44:00", 90.1400, 90.1400, 90.1400, 90.1400, 100], ["20090102 05:50:00", 90.0500, 90.0500, 90.0500, 90.0500, 500], ["20090102 05:56:00", 90.1000, 90.1000, 90.1000, 90.1000, 300], ["20090102 05:57:00", 90.1000, 90.1000, 90.1000, 90.1000, 200]]
# Building sample dataframe with Datetime column
df = pd.DataFrame(data)
df.columns = ["Datetime", "1", "2", "3", "4", "5"]
df['Datetime'] = pd.to_datetime(df['Datetime'], format="%Y%m%d %H:%M:%S")
# Extract rows with datetime matching index range
print(df.set_index("Datetime").between_time('5:30:00', '5:45:00'))
这只是输出时间范围之间的记录。
1 2 3 4 5
Datetime
2019-01-02 05:36:00 90.01 90.01 90.01 90.01 200
2009-01-02 05:44:00 90.14 90.14 90.14 90.14 100