python - Pandas 按季度结束日期过滤

标签 python datetime pandas

在索引列中我有一个日期列表:

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/

相关文章:

Python:理解类和实例变量

python - 字典控制 turtle 的运动

python - 在 Snow Leopard 上使用 python 2.5 构建 mod_wsgi

java - 根据自定义时间单位计算天数

python - .FITS 文件/表格转为 csv?

python - Seaborn.despine() 消除将 y 轴移动到图右侧的效果

c# - ParseExact 无法解析 RFC 3339 Internet 日期/时间格式的字符串

android - 如何在 Kotlin 中获取当前的本地日期和时间

python - 如何在单列的 Pandas 数据框上追加新行?

python - 计算pandas面板数据集中并发实体的数量