Pandas select date_range 空行不起作用
Pandas select date_range with empty rows does not work
在 pandas 数据框中,我有一列包含日期和类似的空值
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)]
如何在给定范围内正确 select 日期?
例如,您有以下数据框
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
我在 pandas 中指定日期范围的首选方法是使用布尔掩码,但是还有其他方法使用工具,例如 DatetimeIndex class.
使用布尔掩码,您的解决方案类似于:
mask = (df['date_column'] > '2017-01-01') & (df['date_column'] <= '2018-01-01')
df = df.loc[[mask]]
在 pandas 数据框中,我有一列包含日期和类似的空值
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)]
如何在给定范围内正确 select 日期?
例如,您有以下数据框
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
我在 pandas 中指定日期范围的首选方法是使用布尔掩码,但是还有其他方法使用工具,例如 DatetimeIndex class.
使用布尔掩码,您的解决方案类似于:
mask = (df['date_column'] > '2017-01-01') & (df['date_column'] <= '2018-01-01')
df = df.loc[[mask]]