15 2018-04-13 13:26:54 UTC
16
...
28
29 2018-05-15 00:00:00 UTC
30
...
40
41
42 2018-03-24 20:32:36 UTC
...
46 2018-04-10 20:41:39 UTC
47
48
49 2018-01-26 20:30:22 UTC
....
58 2017-05-30 09:26:04 UTC
59 2010-09-09 14:09:03 UTC
我正在搜索空值和日期范围内的值。不幸的是,没有像这样的工作
df[df['date_column'].loc['2017-01-01':'2018-01-01']]
df['date_column']isin(pd.date_range('two_months', periods=2, freq='M'))
df[df['date_column'].str.contains(regex_filters_date)]
如何正确选择给定范围内的日期?
答案 0 :(得分:1)
例如,您有以下数据框
df=pd.DataFrame({'Date':['2018-03-24 20:32:36 UTC','','2018-01-26 20:30:22 UTC','']})
s=pd.to_datetime(df.Date)
df[(s>pd.to_datetime('2018-02-01'))&(s<pd.to_datetime('2018-04-01'))]
Date
0 2018-03-24 20:32:36 UTC
如果要选择空白
df[((s > pd.to_datetime('2018-02-01')) & (s < pd.to_datetime('2018-04-01')))|s.isnull()]
Out[831]:
Date
0 2018-03-24 20:32:36 UTC
1
3
答案 1 :(得分:0)
我在pandas中指定日期范围的首选方法是使用布尔掩码,但是还有其他方法使用DatetimeIndex类等工具。
Here is some documentation from an earlier thread I think you would find useful!
使用布尔掩码,您的解决方案将类似于:
mask = (df['date_column'] > '2017-01-01') & (df['date_column'] <= '2018-01-01')
df = df.loc[[mask]]