Skip to content

API: let DatetimeIndex date/time components return a new Index instead of array #15022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jorisvandenbossche opened this issue Dec 31, 2016 · 1 comment
Labels
API Design Datetime Datetime data dtype
Milestone

Comments

@jorisvandenbossche
Copy link
Member

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

@jorisvandenbossche jorisvandenbossche added this to the Next Major Release milestone Dec 31, 2016
@srinivasreddy
Copy link
Contributor

want to take this.

@jreback jreback modified the milestones: 0.20.0, Next Major Release Mar 22, 2017
mattip pushed a commit to mattip/pandas that referenced this issue Apr 3, 2017
… (GH15022)

closes pandas-dev#15022

Author: Joris Van den Bossche <[email protected]>

Closes pandas-dev#15589 from jorisvandenbossche/api-dt-fields-index and squashes the following commits:

ffacd38 [Joris Van den Bossche] doc fixes
41728a9 [Joris Van den Bossche] FIX: boolean fields should still return array
6317b6b [Joris Van den Bossche] Add whatsnew
96ed069 [Joris Van den Bossche] Preserve name for PeriodIndex field accessors
cdf6cae [Joris Van den Bossche] Preserve name for DatetimeIndex field accessors
f2831e2 [Joris Van den Bossche] Update timedelta accessors
52f9008 [Joris Van den Bossche] Fix tests
41008c7 [Joris Van den Bossche] API: return Index instead of array from datetime field accessors (GH15022)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Datetime Datetime data dtype
Projects
None yet
3 participants