在Pandas中,如何以特定的频率创建索引,但只适用于特定的月份?

In Pandas, how to create an index in a specific frequency but only applied to certain months?

我需要按特定频率创建索引,但我只需要在特定月份应用该频率。这是频率:

index_1 = pd.date_range('1/1/2000', '1/1/2016', freq='WOM-3FRI')

我想创建一个与此类似的索引,但只针对一月、三月、九月和十二月。

在 pandas 上是否有 pythonic 方法来执行此操作?

谢谢

好的,我找到了答案,使用 selector:

index_1 = pd.date_range('1/1/2000', '1/1/2016', freq='WOM-3FRI')
data = pd.DataFrame(index=index_1)
month = data.index.month
selector = ((month == 12) | (month == 3) | (month == 6) | (month == 9))
data = data[selector]

您可以从月份构造一个 Pandas 索引并使用 isin 并传递月份值列表来执行选择:

In [370]:

index_1 = pd.date_range('1/1/2000', '1/1/2016', freq='WOM-3FRI')
data = pd.DataFrame(index=index_1)
data[pd.Index(data.index.month).isin([3,6,9,12])]
Out[370]:
Empty DataFrame
Columns: []
Index: [2000-03-17 00:00:00, 2000-06-16 00:00:00, 2000-09-15 00:00:00, 2000-12-15 00:00:00, 2001-03-16 00:00:00, 2001-06-15 00:00:00, 2001-09-21 00:00:00, 2001-12-21 00:00:00, 2002-03-15 00:00:00, 2002-06-21 00:00:00, 2002-09-20 00:00:00, 2002-12-20 00:00:00, 2003-03-21 00:00:00, 2003-06-20 00:00:00, 2003-09-19 00:00:00, 2003-12-19 00:00:00, 2004-03-19 00:00:00, 2004-06-18 00:00:00, 2004-09-17 00:00:00, 2004-12-17 00:00:00, 2005-03-18 00:00:00, 2005-06-17 00:00:00, 2005-09-16 00:00:00, 2005-12-16 00:00:00, 2006-03-17 00:00:00, 2006-06-16 00:00:00, 2006-09-15 00:00:00, 2006-12-15 00:00:00, 2007-03-16 00:00:00, 2007-06-15 00:00:00, 2007-09-21 00:00:00, 2007-12-21 00:00:00, 2008-03-21 00:00:00, 2008-06-20 00:00:00, 2008-09-19 00:00:00, 2008-12-19 00:00:00, 2009-03-20 00:00:00, 2009-06-19 00:00:00, 2009-09-18 00:00:00, 2009-12-18 00:00:00, 2010-03-19 00:00:00, 2010-06-18 00:00:00, 2010-09-17 00:00:00, 2010-12-17 00:00:00, 2011-03-18 00:00:00, 2011-06-17 00:00:00, 2011-09-16 00:00:00, 2011-12-16 00:00:00, 2012-03-16 00:00:00, 2012-06-15 00:00:00, 2012-09-21 00:00:00, 2012-12-21 00:00:00, 2013-03-15 00:00:00, 2013-06-21 00:00:00, 2013-09-20 00:00:00, 2013-12-20 00:00:00, 2014-03-21 00:00:00, 2014-06-20 00:00:00, 2014-09-19 00:00:00, 2014-12-19 00:00:00, 2015-03-20 00:00:00, 2015-06-19 00:00:00, 2015-09-18 00:00:00, 2015-12-18 00:00:00]

[64 rows x 0 columns]