Skip to content

Use ._tshift internally for datetimelike ops #22949

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

Merged
merged 3 commits into from
Oct 5, 2018

Conversation

TomAugspurger
Copy link
Contributor

In preperation for PeriodArray / DatetimeArray / TimedeltaArray.

Index.shift has a different meaning from ExtensionArray.shift.

  • Index.shift pointwise shifts each element by some amount
  • ExtensionArray.shift shits the position of each value in the array
    padding the end with NA

This is going to get confusing. This PR tries to avoid some of that by
internally using a new _tshift method (time-shift) when we want to do pointwise
shifting of each value. Places that know they want that behavior (like in the
datetimelike ops) should use that.

In preperation for PeriodArray / DatetimeArray / TimedeltaArray.

Index.shift has a different meaning from ExtensionArray.shift.

- Index.shift pointwise shifts each element by some amount
- ExtensionArray.shift shits the *position* of each value in the array
  padding the end with NA

This is going to get confusing. This PR tries to avoid some of that by
internally using a new `_tshift` method (time-shift) when we want to do pointwise
shifting of each value. Places that know they want that behavior (like in the
datetimelike ops) should use that.
@TomAugspurger TomAugspurger added Refactor Internal refactoring of code Datetime Datetime data dtype Period Period data type labels Oct 2, 2018
@TomAugspurger TomAugspurger added this to the 0.24.0 milestone Oct 2, 2018
@pep8speaks
Copy link

Hello @TomAugspurger! Thanks for submitting the PR.

@codecov
Copy link

codecov bot commented Oct 2, 2018

Codecov Report

Merging #22949 into master will increase coverage by <.01%.
The diff coverage is 88.88%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #22949      +/-   ##
==========================================
+ Coverage   92.18%   92.18%   +<.01%     
==========================================
  Files         169      169              
  Lines       50823    50827       +4     
==========================================
+ Hits        46853    46857       +4     
  Misses       3970     3970
Flag Coverage Δ
#multiple 90.6% <88.88%> (ø) ⬆️
#single 42.36% <22.22%> (ø) ⬆️
Impacted Files Coverage Δ
pandas/core/arrays/datetimelike.py 95.56% <100%> (+0.02%) ⬆️
pandas/core/arrays/period.py 92.04% <80%> (+0.06%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ee80803...67faabc. Read the comment docs.

@@ -553,6 +553,23 @@ def shift(self, periods, freq=None):
--------
Index.shift : Shift values of Index.
"""
return self._tshift(periods=periods, freq=freq)

def _tshift(self, periods, freq=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we call this ._time_shift or ._period_shift

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you are actually going to change this, then this should be much more consistent across the code base, e.g. does internals need changing, what about the Index classes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose _tshift to match Series.tshift, which has the same behavior, but for the Series' index.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a note to deprecate tshift entirely. I don't think this is a good name at all.

"""
Shift each value by `periods`.

Note this is different from ExtensionArray.shift, which
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this relevant here? this is simply a re-definition of what shift means. I understand the desire to make things clear, but this has always been the case, for example in comparing a DTI to an Index.

@@ -553,6 +553,23 @@ def shift(self, periods, freq=None):
--------
Index.shift : Shift values of Index.
"""
return self._tshift(periods=periods, freq=freq)

def _tshift(self, periods, freq=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you are actually going to change this, then this should be much more consistent across the code base, e.g. does internals need changing, what about the Index classes?

@TomAugspurger
Copy link
Contributor Author

These changes cover the places known to hit PeriodIndex.shift, the places I needed to change on the PeriodArray refactor to get tests passing.

I don't see any uses in Internals, and no other Index classes I'm aware of actually implement .shift. Just the datetimelike ones I've updated here.

@TomAugspurger
Copy link
Contributor Author

Renamed to _time_shift.

if you are actually going to change this, then this should be much more consistent across the code base, e.g. does internals need changing, what about the Index classes?

Do you have particular places in mind?

@TomAugspurger
Copy link
Contributor Author

All green.

@jreback jreback merged commit ee27fab into pandas-dev:master Oct 5, 2018
@jreback
Copy link
Contributor

jreback commented Oct 5, 2018

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Period Period data type Refactor Internal refactoring of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants