如何在熊猫时间序列中选择特定季度
问题描述:
从时间序列中选择特定单行的最佳方式是什么?如何在熊猫时间序列中选择特定季度
我已经得到了一些数据:
indexed = df.set_index("Month").resample("Q-MAR", how="sum")['count']
indexed
Month
2010-12-31 6942
2011-03-31 23677
2011-06-30 24131
2011-09-30 23144
2011-12-31 22249
2012-03-31 24216
2012-06-30 22938
2012-09-30 25468
2012-12-31 21733
2013-03-31 21385
2013-06-30 23093
2013-09-30 26206
2013-12-31 22248
2014-03-31 20737
2014-06-30 23384
2014-09-30 25285
2014-12-31 22210
2015-03-31 22627
2015-06-30 25185
2015-09-30 27038
2015-12-31 25352
2016-03-31 16694
Freq: Q-MAR, Name: count, dtype: int64
我可以切片出各个条目,如:
indexed.ix["2012-09-30"]
25468
indexed.ix["2015-09-30"]
27038
但是我怎么选择这两个?
我想:
indexed.ix[["2012-09-30", "2015-09-30"],:]
它返回太多的索引错误...
虽然
indexed.ix[["2012-09-30", "2015-09-30"]]
带回
Month
2012-09-30 NaN
2015-09-30 NaN
而且我不明白为什么
indexed.ix["2012-09-30", "2015-09-30"]
回报
25468
可能有人请解释给我,好吗?
答
工作,如果你指定了正确的dtypes如预期索引访问:
In [13]: df.loc[[pd.to_datetime('2012-09-30'),pd.to_datetime('2015-09-30')]]
Out[13]:
Count
Month
2012-09-30 25468
2015-09-30 27038
In [14]: df.loc[['2012-09-30','2015-09-30']]
Out[14]:
Count
Month
2012-09-30 NaN
2015-09-30 NaN
UPDATE:
从Pand开始为0.20.1 the .ix indexer is deprecated, in favor of the more strict .iloc and .loc indexers。
答
我发现索引这个答案有用: pandas, python - how to select specific times in timeseries
indexed[(pd.Index(indexed.index.quarter).isin([3])) & (pd.Index(indexed.index.year).isin([2012,2015]))]
Month
2012-09-30 25468
2015-09-30 27038