Skip to content

Calling shift on a DatetimeIndex of length 0 returns an Index instead of a DatetimeIndex #9903

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
ssanderson opened this issue Apr 15, 2015 · 10 comments
Labels
Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@ssanderson
Copy link
Contributor

Minimal repro:

In [5]: pandas.__version__
Out[5]: '0.16.0-151-gcef3c85'

In [7]: DatetimeIndex([]).shift(3, 'H')
Out[7]: Index([], dtype='object')
@ssanderson
Copy link
Contributor Author

(This happens because DatetimeIndex delegates to Index.shift, which then calls the Index.new with an empty list, and it can't infer that the output should be a DatetimeIndex.)

ssanderson added a commit to ssanderson/pandas that referenced this issue Apr 15, 2015
Fixes a bug where calling shift on an empty DatetimeIndex would result
in the returned value being an Index rather than a DatetimeIndex.
@ssanderson
Copy link
Contributor Author

This is fixed by #9904.

@jreback jreback added Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves labels Apr 17, 2015
@jreback jreback added this to the 0.16.1 milestone Apr 17, 2015
ssanderson added a commit to ssanderson/pandas that referenced this issue Apr 20, 2015
Fixes a bug where calling shift on an empty DatetimeIndex would result
in the returned value being an Index rather than a DatetimeIndex.
ssanderson added a commit to ssanderson/pandas that referenced this issue Apr 20, 2015
Fixes a bug where calling shift on an empty DatetimeIndex would result
in the returned value being an Index rather than a DatetimeIndex.
@jreback jreback modified the milestones: 0.17.0, 0.16.1 Apr 28, 2015
@jreback jreback modified the milestones: Next Major Release, 0.17.0 Aug 15, 2015
@sinhrks
Copy link
Member

sinhrks commented Apr 29, 2016

This has been already fixed in current master (maybe by #11211?). Adding tests then close.

@jreback
Copy link
Contributor

jreback commented Apr 29, 2016

@sinhrks looks good.

In [1]: DatetimeIndex([]).shift(3, 'H')
Out[1]: DatetimeIndex([], dtype='datetime64[ns]', freq=None)

I since we have 0 len we don't infer the freq, but in this case we know the freq (as we are shifting by it), or is that too much carry-over?

@sinhrks
Copy link
Member

sinhrks commented Apr 29, 2016

DatetimeIndex.freq is common frequencies between elem, and it can be different from shifting freq. Thus it sounds making an exception in length=0 case. Thoughts?

s = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
s.shift(1, 'H')
# DatetimeIndex(['2011-01-01 01:00:00', '2011-01-02 01:00:00'], dtype='datetime64[ns]', freq=None)

@jreback
Copy link
Contributor

jreback commented Apr 29, 2016

cc @ssanderson
cc @llllllllll

@jreback
Copy link
Contributor

jreback commented Apr 29, 2016

@sinhrks no, I was more thinking that If I shift an index, then I want to have it set the freq (as opposed to infer it which won't work if its < 3 elements). not really strong here opinion here, except that this was from a user action.

@ssanderson
Copy link
Contributor Author

@jreback I don't think that shifting an index by a specific frequency should be taken to imply that the index's data has that frequency. If I have an hourly-frequency index, and I shift it forward by 5 minutes, it's still hourly frequency. That doesn't change if my index is empty.

@ssanderson
Copy link
Contributor Author

#13026 looks good to me btw. Thanks @sinhrks.

@jreback
Copy link
Contributor

jreback commented Apr 29, 2016

@ssanderson hmm, that is right. ok then!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
3 participants