API: let DatetimeIndex date/time components return a new Index instead of array · Issue #15022 · pandas-dev/pandas (original) (raw)

Problem description

Currently the date and time components on a DatetimeIndex return a numpy array. I would propose to let them return a new Index object (and in this way retaining all the interesting added functionality of an Index object).

Related to #14506 that changed Index.map to return an Index instead of an array.

Code Sample

In [14]: ts = pd.Series(range(10), index=pd.date_range('2016-01-01', periods=10))

In [15]: ts
Out[15]: 
2016-01-01    0
2016-01-02    1
2016-01-03    2
2016-01-04    3
2016-01-05    4
2016-01-06    5
2016-01-07    6
2016-01-08    7
2016-01-09    8
2016-01-10    9
Freq: D, dtype: int64

In [16]: ts.index.dayofweek
Out[16]: array([4, 5, 6, 0, 1, 2, 3, 4, 5, 6], dtype=int32)

When the above would return an index, something that would become possible is this:

ts[ts.index.dayofweek.isin([5,6])]

which is now not possible as an array has no isin method.

Using pandas master, 0.19.0+289.g1bf94c8