本文介绍了不重新采样的时间序列的 pandas 子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个熊猫数据系列,该数据系列具有一系列的累积日收益率
I have a pandas data series with cumulative daily returns for a series:
Date CumReturn
3/31/2017 1
4/3/2017 .99
4/4/2017 .992
... ...
4/28/2017 1.012
5/1/2017 1.011
... ...
5/31/2017 1.022
... ...
6/30/2017 1.033
... ...
我只想要月末值.
Date CumReturn
4/28/2017 1.012
5/31/2017 1.022
6/30/2017 1.033
因为我只需要月末值,所以重新采样无法进行,因为它会汇总临时值.
Because I want only the month-end values, resampling doesn't work as it aggregates the interim values.
仅获取原始数据框中显示的月末值的最简单方法是什么?
What is the easiest way to get only the month end values as they appear in the original dataframe?
推荐答案
使用 is_month_end
组件" rel = "nofollow noreferrer"> .dt
日期访问器:
Use the is_month_end
component of the .dt
date accessor:
# Ensure the date column is a Timestamp
df['Date'] = pd.to_datetime(df['Date'])
# Filter to end of the month only
df = df[df['Date'].dt.is_month_end]
将此应用于您提供的数据:
Applying this to the data you provided:
Date CumReturn
0 2017-03-31 1.000
5 2017-05-31 1.022
6 2017-06-30 1.033
编辑
要获得营业月底,请使用BMonthEnd(0)
进行比较:
To get business month end, compare using BMonthEnd(0)
:
from pandas.tseries.offsets import BMonthEnd
# Ensure the date column is a Timestamp
df['Date'] = pd.to_datetime(df['Date'])
# Filter to end of the month only
df = df[df['Date'] == df['Date'] + BMonthEnd(0)]
将此应用于您提供的数据:
Applying this to the data you provided:
Date CumReturn
0 2017-03-31 1.000
3 2017-04-28 1.012
5 2017-05-31 1.022
6 2017-06-30 1.033
这篇关于不重新采样的时间序列的 pandas 子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!