在索引列中我有一个日期列表:
DatetimeIndex(['2010-12-31', '2011-01-02', '2011-01-03', '2011-01-29',
'2011-02-26', '2011-02-28', '2011-03-26', '2011-03-31',
'2011-04-01', '2011-04-03',
...
'2016-02-27', '2016-02-29', '2016-03-26', '2016-03-31',
'2016-04-01', '2016-04-03', '2016-04-30', '2016-05-31',
'2016-06-30', '2016-07-02'],
dtype='datetime64[ns]', length=123, freq=None)
但是我想过滤掉所有月和日等于 12/31、3/31、6/30、9/30 的值,以获得季度末的值。
有解决这个问题的好方法吗?
最佳答案
您可以使用 is_quarter_end
过滤行标签:
In [151]:
df = pd.DataFrame(np.random.randn(400,1), index= pd.date_range(start=dt.datetime(2016,1,1), periods=400))
df.loc[df.index.is_quarter_end]
Out[151]:
0
2016-03-31 -0.474125
2016-06-30 0.931780
2016-09-30 -0.281271
2016-12-31 0.325521
关于python - Pandas 按季度结束日期过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39598618/