简体   繁体   English

如何在 Pandas 中 select '本月的最后一个工作日'?

[英]How can I select 'last business day of the month' in Pandas?

I'm trying to subset a DataFrame on the condition that is the last of the month.我正在尝试在本月的最后一个条件下对 DataFrame 进行子集化。 I used:我用了:

df['Month_End'] = df.index.is_month_end
sample = df[df['Month_End'] == 1]

This works, but I'm working with stock market data, so I'm missing all the instances where the actual end of the month is during the weekend, I need a way to select the "last business day of the month".这行得通,但我正在处理股市数据,所以我错过了所有实际月底在周末的情况,我需要一种方法来 select “本月的最后一个工作日”。

You can generate a time series with the last business day of each month by passing in freq='BM' . 您可以通过传递freq='BM'来生成每月最后一个工作日的时间序列

For example, to create a series of the last business days of 2014: 例如,要创建2014年最后一个工作日的系列:

>>> pd.date_range('1/1/2014', periods=12, freq='BM')
[2014-01-31 00:00:00, ..., 2014-12-31 00:00:00]
Length: 12, Freq: BM, Timezone: None

You could then use this timeseries to subset/reindex your DataFrame. 然后,您可以使用此时间序列对DataFrame进行子集/重新索引。

pd.Instead of generating the series, you can also parse the business month end from your datetime index as this: pd。除了生成系列之外,您还可以从日期时间索引中解析业务月末,如下所示:

df['BMonthEnd'] = (df.index + pd.offsets.BMonthEnd(1)).day

Though note this currently throws a harmless warning - see http://pandas.pydata.org/pandas-docs/stable/timeseries.html#using-offsets-with-series-datetimeindex 虽然注意到这当前会引发无害的警告 - 请参阅http://pandas.pydata.org/pandas-docs/stable/timeseries.html#using-offsets-with-series-datetimeindex

This is to filter the last business day of each month from your DataFrame object given the index is of datetime type.这是从您的 DataFrame object 中过滤每个月的最后一个工作日,因为索引是日期时间类型。
df.resample('BM').mean()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM