我正在尝试在特定日期时间内提取行。我在做什么错了?
import pandas as pd
df = pd.DataFrame({'year': [2015, 2016, 2017, 2016],
'month': [2, 3, 4, 6],
'day': [4, 5, 4, 3]})
df = pd.to_datetime(df)
df = df.to_frame(name='test')
start_date = pd.Timestamp(2016,1,1)
end_date = pd.Timestamp(2017, 5, 1)
start_date = pd.to_datetime(start_date)
end_date = pd.to_datetime(end_date)
print (start_date)
print (end_date)
print (df[df['test'] > start_date & df['test'] < end_date])
答案 0 :(得分:1)
由于运营商的优先级,缺少()
:
print (df[(df['test'] > start_date) & (df['test'] < end_date)])
或使用Series.between
:
print (df[(df['test'].between(start_date, end_date, inclusive=False)])