API/CLN: add in common operations to Series/Index, refactored as a OpsMixin by jreback · Pull Request #6380 · pandas-dev/pandas (original) (raw)

closes #4551
closes #4056
closes #5519

allow a Series to utilize index methods for its index type, e.g. Series.year is now defined
for a Series with a DatetimeIndex or a PeriodIndex; trying this on an Index type will
now raise a TypeError. The following properties are affected:
date,time,year,month,day,hour,minute,second,weekofyear
week,dayofweek,dayofyear,quarter,microsecond,nanosecond,qyear
and methods: min(),max(),

In [1]: s = Series(np.random.randn(5),index=tm.makeDateIndex(5))

In [2]: s
Out[2]: 
2000-01-03   -0.301523
2000-01-04    1.104868
2000-01-05    0.321592
2000-01-06   -0.565114
2000-01-07    0.888334
Freq: B, dtype: float64

In [3]: s.year
Out[3]: 
2000-01-03    2000
2000-01-04    2000
2000-01-05    2000
2000-01-06    2000
2000-01-07    2000
Freq: B, dtype: int64

In [4]: s.index.year
Out[4]: Int64Index([2000, 2000, 2000, 2000, 2000], dtype='int64')

In [5]: Series(np.random.randn(5)).year
TypeError: cannot perform an year operations on this type <class 'pandas.core.index.Int64Index'>