在 Pandas 中,如何创建特定频率但仅适用于特定月份的索引?
Posted
技术标签:
【中文标题】在 Pandas 中,如何创建特定频率但仅适用于特定月份的索引?【英文标题】:In Pandas, how to create an index in a specific frequency but only applied to certain months? 【发布时间】:2015-05-23 20:49:45 【问题描述】:我需要按特定频率创建索引,但我只需要在特定月份应用该频率。这是频率:
index_1 = pd.date_range('1/1/2000', '1/1/2016', freq='WOM-3FRI')
我想创建一个与此类似的索引,但仅限于 1 月、3 月、9 月和 12 月。
在 pandas 上是否有一种 Pythonic 方法可以做到这一点?
谢谢
【问题讨论】:
【参考方案1】:好的,我找到了答案,使用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]
【讨论】:
【参考方案2】:您可以根据月份构建 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]
【讨论】:
以上是关于在 Pandas 中,如何创建特定频率但仅适用于特定月份的索引?的主要内容,如果未能解决你的问题,请参考以下文章