python - 在最后 n 个日期过滤 Pandas DataFrame

标签 python python-2.7 pandas

我有一个看起来像这样的 Pandas DF:

df

我想使用本地定义的整数参数“days”过滤 DF。例如当 days = 10 时,我过滤的 DF 只有最后 10 个可用日期的数据。

到目前为止,我已经尝试了以下方法:

days=10    
cutoff_date = df["SeriesDate"][-1:] - datetime.timedelta(days=days)

但是,然后尝试使用以下方法输出过滤后的 DF:

df[df['SeriesDate'] > cutoff_date] 

我得到以下错误:

ValueError: Can only compare identically-labeled Series objects

我仍在学习 Python,因此我将不胜感激能从中获得的任何帮助。

最佳答案

我认为您需要通过 iloc 选择列 SeriesDate 的最后一个值:

start = pd.to_datetime('2015-02-24')
rng = pd.date_range(start, periods=15, freq='20H')
df = pd.DataFrame({'SeriesDate': rng, 'Value_1': np.random.random(15)})  
print (df)
            SeriesDate   Value_1
0  2015-02-24 00:00:00  0.849160
1  2015-02-24 20:00:00  0.332487
2  2015-02-25 16:00:00  0.687638
3  2015-02-26 12:00:00  0.310326
4  2015-02-27 08:00:00  0.660795
5  2015-02-28 04:00:00  0.354475
6  2015-03-01 00:00:00  0.061312
7  2015-03-01 20:00:00  0.443908
8  2015-03-02 16:00:00  0.708326
9  2015-03-03 12:00:00  0.257419
10 2015-03-04 08:00:00  0.618363
11 2015-03-05 04:00:00  0.121625
12 2015-03-06 00:00:00  0.637324
13 2015-03-06 20:00:00  0.058292
14 2015-03-07 16:00:00  0.047624
days=10    
cutoff_date = df["SeriesDate"].iloc[-1] - pd.Timedelta(days=days)
print (cutoff_date)
2015-02-25 16:00:00

df1 = df[df['SeriesDate'] > cutoff_date] 
print (df1)
            SeriesDate   Value_1
3  2015-02-26 12:00:00  0.310326
4  2015-02-27 08:00:00  0.660795
5  2015-02-28 04:00:00  0.354475
6  2015-03-01 00:00:00  0.061312
7  2015-03-01 20:00:00  0.443908
8  2015-03-02 16:00:00  0.708326
9  2015-03-03 12:00:00  0.257419
10 2015-03-04 08:00:00  0.618363
11 2015-03-05 04:00:00  0.121625
12 2015-03-06 00:00:00  0.637324
13 2015-03-06 20:00:00  0.058292
14 2015-03-07 16:00:00  0.047624

另一种选择是使用max,谢谢Pocin :

cutoff_date = df["SeriesDate"].max() - pd.Timedelta(days=days)
print (cutoff_date)
2015-02-25 16:00:00

如果你只想按日期过滤:

days=10    
cutoff_date = df["SeriesDate"].dt.date.iloc[-1] - pd.Timedelta(days=days)
print (cutoff_date)
2015-02-25

编辑:

您可以使用 dayofweek 筛选出周末日期然后使用 isin

start = pd.to_datetime('2015-02-24')
rng = pd.date_range(start, periods=15)
df = pd.DataFrame({'SeriesDate': rng, 'Value_1': np.random.random(15)})  
print (df)
   SeriesDate   Value_1
0  2015-02-24  0.498387
1  2015-02-25  0.435767
2  2015-02-26  0.299233
3  2015-02-27  0.489286
4  2015-02-28  0.892167
5  2015-03-01  0.507436
6  2015-03-02  0.360427
7  2015-03-03  0.903886
8  2015-03-04  0.718148
9  2015-03-05  0.645489
10 2015-03-06  0.251285
11 2015-03-07  0.139275
12 2015-03-08  0.756845
13 2015-03-09  0.565863
14 2015-03-10  0.148077
days=10    
last_day = df["SeriesDate"].dt.date.iloc[-1]
cutoff_date = last_day - pd.Timedelta(days=days)
rng = pd.date_range(cutoff_date, last_day)

rng = rng[(rng.dayofweek != 0) & (rng.dayofweek != 6)]
print (rng)
DatetimeIndex(['2015-02-28', '2015-03-03', '2015-03-04', '2015-03-05',
               '2015-03-06', '2015-03-07', '2015-03-10'],
              dtype='datetime64[ns]', freq=None)

df1 = df[df['SeriesDate'].isin(rng)]
print (df1)
   SeriesDate   Value_1
4  2015-02-28  0.892167
7  2015-03-03  0.903886
8  2015-03-04  0.718148
9  2015-03-05  0.645489
10 2015-03-06  0.251285
11 2015-03-07  0.139275
14 2015-03-10  0.148077

关于python - 在最后 n 个日期过滤 Pandas DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42506788/

相关文章:

python 、 Pandas : join dataframes on timestamp and offset

python - 如何在 python 上打印彩色的特定单词?

python - 将列表转换为字典的最佳方法,其中键是每个对象的值?

python-2.7 - 使用requests_oauthlib访问OAuth2网页

python - Selenium Python 绑定(bind)无法将变量传递到函数中

python - 使用 Numpy 屏蔽填充给定值的单元格

python - 如何根据阈值对多列进行分组并在Python中创建新列

Python - 数据框转置和改变结构

python - 对多索引 panda 的级别 2 进行过滤

python - 如何将 pandas 列中的浮点值离散为 [1, 10]