python - How to filter a pandas series with a datetime index on the quarter and year -


i have series, called 'scores', datetime index.

i wish subset quarter , year
pseudocode: series.loc['q2 of 2013']

attempts far:
s.dt.quarter

attributeerror: can use .dt accessor datetimelike values

s.index.dt.quarter

attributeerror: 'datetimeindex' object has no attribute 'dt'

this works (inspired this answer), can't believe right way in pandas:

d = pd.dataframe(s)
d['date'] = pd.to_datetime(d.index)
d.loc[(d['date'].dt.quarter == 2) & (d['date'].dt.year == 2013)]['scores']

i expect there way without transforming dataset, forcing index datetime, , getting series it.

what missing, , elegant way on pandas series?

import numpy np import pandas pd  index = pd.date_range('2013-01-01', freq='m', periods=12) s = pd.series(np.random.rand(12), index=index) print(s)  # 2013-01-31    0.820672 # 2013-02-28    0.994890 # 2013-03-31    0.928376 # 2013-04-30    0.848532 # 2013-05-31    0.122263 # 2013-06-30    0.305741 # 2013-07-31    0.088432 # 2013-08-31    0.647288 # 2013-09-30    0.640308 # 2013-10-31    0.737139 # 2013-11-30    0.233656 # 2013-12-31    0.245214 # freq: m, dtype: float64  d = pd.series(s.index, index=s.index) quarter = d.dt.quarter.astype(str) + 'q' + d.dt.year.astype(str) print(quarter)  # 2013-01-31    1q2013 # 2013-02-28    1q2013 # 2013-03-31    1q2013 # 2013-04-30    2q2013 # 2013-05-31    2q2013 # 2013-06-30    2q2013 # 2013-07-31    3q2013 # 2013-08-31    3q2013 # 2013-09-30    3q2013 # 2013-10-31    4q2013 # 2013-11-30    4q2013 # 2013-12-31    4q2013 # freq: m, dtype: object  print(s[quarter == '1q2013'])  # 2013-01-31    0.124398 # 2013-02-28    0.052828 # 2013-03-31    0.126374 # freq: m, dtype: float64 

if don't want create new series holds label each quarter (e.g., if subsetting once), do

print(s[(s.index.quarter == 1) & (s.index.year == 2013)])  # 2013-01-31    0.124398 # 2013-02-28    0.052828 # 2013-03-31    0.126374 # freq: m, dtype: float64 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -