hernanavella
hernanavella

Reputation: 5552

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

I need to create an index on a specific frequency, however I need to apply that frequency only for certain months. This is the frequency:

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

I want to create an index just like this one but only for the months of January, March, September and December.

Is there a pythonic way to do this on pandas ?

Thanks

Upvotes: 3

Views: 188

Answers (2)

EdChum
EdChum

Reputation: 394021

You could construct a Pandas Index from the months and use isin and pass a list of month values to perform the selection:

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]

Upvotes: 2

hernanavella
hernanavella

Reputation: 5552

Ok, I found an answer, using 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]

Upvotes: 3

Related Questions