使用datetime访问pandas数据帧

时间:2014-05-25 08:33:19

标签: python datetime pandas

我有一个pandas对象,是groupby()操作的结果:

>>> g
Reporting_month
2012-06-01         22.046260
2012-07-01         44.881277
2012-08-01         40.123409
2012-09-01         58.302501
2012-10-01         38.668045
2012-11-01         14.589685
2012-12-01         35.016623
2013-01-01         17.418799
2013-02-01         15.796828
2013-03-01         24.454363
2013-04-01         17.841645
2013-05-01         13.254986
2013-06-01         18.841706
2013-07-01         12.993700
2013-08-01         30.855772
2013-09-01         35.239389
2013-10-01         47.578782
2013-11-01         21.351405
2013-12-01          7.699076
2014-01-01          8.214374
2014-02-01         21.175696
Name: Account_total, dtype: float64

我正试图访问季度数据。

说我试图在2013年获得第2季:

我可以访问感兴趣的年份:

h = h=g[g.index.year==2013]

给我:

Reporting_month
2013-01-01         17.418799
2013-02-01         15.796828
2013-03-01         24.454363
2013-04-01         17.841645
2013-05-01         13.254986
2013-06-01         18.841706
2013-07-01         12.993700
2013-08-01         30.855772
2013-09-01         35.239389
2013-10-01         47.578782
2013-11-01         21.351405
2013-12-01          7.699076
Name: Account_total, dtype: float64

和Q2这样:

h[h.index.month[2:5]]

,并提供:

Reporting_month
2013-04-01         17.841645
2013-05-01         13.254986
2013-06-01         18.841706
Name: Account_total, dtype: float64

但是,我无法像这样访问1月份。

似乎必须有更好的方法。有没有人有任何想法?

1 个答案:

答案 0 :(得分:0)

刚破解它:

h[h.index.quarter==1]