如何从 pandas 日期时间索引中过滤特定月份 [英] How to filter specifc months from Pandas datetime index

查看:89
本文介绍了如何从 pandas 日期时间索引中过滤特定月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个每天的数据集,范围是2000-2010.我已经通过

I have a daily dataset which ranges from 2000-2010. I already set the column 'GregDate' via

df.set_index(pd.DatetimeIndex(df['GregDate'])) 

作为索引.现在,我只想调查从十一月到三月的十年(整整十年).

as index. Now I only want to investigate the months from November till March (for all the ten years).

我的数据框如下:

                Sigma        Lat        Lon
GregDate                                   
2000-01-01  -5.874062  79.913437 -74.583125
2000-01-02  -6.794000  79.904000 -74.604000
2000-01-03  -5.826061  79.923939 -74.548485
2000-01-04  -5.702439  79.916829 -74.641707
...
2009-07-11 -10.727381  79.925952 -74.660714
2009-07-12 -10.648000  79.923667 -74.557333
2009-07-13 -11.123095  79.908810 -74.596190

[3482 rows x 3 columns]

我已经看过了这个

I already had a look at this question, but I still am not able to solve my problem.

推荐答案

我认为您需要 boolean indexing DatetimeIndex.month Index.isin :

I think you need boolean indexing with DatetimeIndex.month and Index.isin:

df = df[df.index.month.isin([11,12,1,2,3])]
print (df)
               Sigma        Lat        Lon
GregDate                                  
2000-01-01 -5.874062  79.913437 -74.583125
2000-01-02 -6.794000  79.904000 -74.604000
2000-01-03 -5.826061  79.923939 -74.548485
2000-01-04 -5.702439  79.916829 -74.641707

这篇关于如何从 pandas 日期时间索引中过滤特定月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆