Python - 从数据帧熊猫中检索最近30天的数据

Nai*_*bes 8 python dataframe pandas

我的数据框包含每天收集的六个月错误日志.我想从最后一个日期检索最近30天的记录.最后的日期不是今天.
例如:我有5月,6月,7月的数据,直到August 15我想要检索数据August 15July 1530天的记录.
有没有办法在Python Pandas中做到这一点?

这是示例数据帧:

Error_Description         Date        Weekend      Type
N17739 Limit switch X-    5/1/2015    5/3/2015    Critical
N17739 Limit switch Y-    5/1/2015    5/3/2015    Critical
N938 Key non-functional   5/1/2015    5/3/2015    Non-Critical
P124 Magazine is running  5/1/2015    5/3/2015    Non-Critical
N17738 Limit switch Z+    5/1/2015    5/3/2015    Critical
N938 Key non-functional   5/1/2015    5/3/2015    Non-Critical
     ...                    ...         ...          ...
P873 ENCLOSURE DOOR       8/24/2015   8/30/2015   Non-Critical
N3065 Reset M114          8/24/2015   8/30/2015   Non-Critical
N3065 Reset M114,         8/24/2015   8/30/2015   Non-Critical
N2853 Synchronization     8/24/2015   8/30/2015   Critical
P152 ENCLOSURE            8/24/2015   8/30/2015   Non-Critical
N6236 has stopped         8/24/2015   8/30/2015   Critical
Run Code Online (Sandbox Code Playgroud)

fal*_*ell 6

您可以使用DataFrame.last_valid_index()查找最后一行的标签,然后减去DateOffset(30, 'D')以返回 30 天:

df[df.last_valid_index()-pandas.DateOffset(30, 'D'):]
Run Code Online (Sandbox Code Playgroud)


jez*_*ael 6

日期lastdayfrom用于DataFrame按功能loc选择最近30天.

lastdayfrom = pd.to_datetime('8/24/2015')
print lastdayfrom
#2015-08-24 00:00:00

print df
#           Error_Description       Date    Weekend          Type
#0     N17739 Limit switch X- 2015-05-01 2015-05-03      Critical
#1     N17739 Limit switch Y- 2015-05-01 2015-05-03      Critical
#2    N938 Key non-functional 2015-05-01 2015-05-03  Non-Critical
#3   P124 Magazine is running 2015-05-01 2015-05-03  Non-Critical
#4     N17738 Limit switch Z+ 2015-02-01 2015-05-03      Critical
#5    N938 Key non-functional 2015-07-25 2015-05-03  Non-Critical
#6        P873 ENCLOSURE DOOR 2015-07-24 2015-08-30  Non-Critical
#7           N3065 Reset M114 2015-07-21 2015-08-21  Non-Critical
#8          N3065 Reset M114, 2015-08-22 2015-08-22  Non-Critical
#9      N2853 Synchronization 2015-08-23 2015-08-30      Critical
#10            P152 ENCLOSURE 2015-08-24 2015-08-30  Non-Critical
#11         N6236 has stopped 2015-08-24 2015-08-30      Critical

print df.dtypes
#Error_Description            object
#Date                 datetime64[ns]
#Weekend              datetime64[ns]
#Type                         object
#dtype: object

#set index from column Date
df = df.set_index('Date')
#if datetimeindex isn't order, order it
df= df.sort_index()

#last 30 days of date lastday
df = df.loc[lastdayfrom - pd.Timedelta(days=30):lastdayfrom].reset_index()
Run Code Online (Sandbox Code Playgroud)
print df
#        Date      Error_Description    Weekend          Type
#0 2015-07-25       N3065 Reset M114 2015-08-21  Non-Critical
#1 2015-08-22      N3065 Reset M114, 2015-08-22  Non-Critical
#2 2015-08-23  N2853 Synchronization 2015-08-30      Critical
#3 2015-08-24         P152 ENCLOSURE 2015-08-30  Non-Critical
#4 2015-08-24      N6236 has stopped 2015-08-30      Critical
Run Code Online (Sandbox Code Playgroud)


小智 6

其他两个答案(当前)假设日期是索引,但至少在 python3 中,您可以通过简单的屏蔽来解决这个问题(.query(..)不起作用)。

df[df["Date"] >= (pd.to_datetime('8/24/2015') - pd.Timedelta(days=30))]