Skip to content

REF: avoid internals in tshift #33056

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 2 commits into from
Mar 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8978,7 +8978,7 @@ def slice_shift(self: FrameOrSeries, periods: int = 1, axis=0) -> FrameOrSeries:
return new_obj.__finalize__(self)

def tshift(
self: FrameOrSeries, periods: int = 1, freq=None, axis=0
self: FrameOrSeries, periods: int = 1, freq=None, axis: Axis = 0
) -> FrameOrSeries:
"""
Shift the time index, using the index's frequency if available.
Expand Down Expand Up @@ -9020,22 +9020,22 @@ def tshift(
if isinstance(freq, str):
freq = to_offset(freq)

block_axis = self._get_block_manager_axis(axis)
axis = self._get_axis_number(axis)
if isinstance(index, PeriodIndex):
orig_freq = to_offset(index.freq)
if freq == orig_freq:
new_data = self._data.copy()
new_data.axes[block_axis] = index.shift(periods)
elif orig_freq is not None:
if freq != orig_freq:
assert orig_freq is not None # for mypy
raise ValueError(
f"Given freq {freq.rule_code} does not match "
f"PeriodIndex freq {orig_freq.rule_code}"
)
new_ax = index.shift(periods)
else:
new_data = self._data.copy()
new_data.axes[block_axis] = index.shift(periods, freq)
new_ax = index.shift(periods, freq)

return self._constructor(new_data).__finalize__(self)
result = self.copy()
Copy link
Contributor

Choose a reason for hiding this comment

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

we are very inconsistent on how calling .set_axis in generic.py, e.g. whether using inplace True/False & also _reset_cached_item.

ok here, but think this would be good to be consistent about.

Copy link
Member Author

Choose a reason for hiding this comment

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

yah, #31784 has got me looking at the caching thing now. is this just to avoid the perf overhead of creating Series objects?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, it may be possible to rip this out as it causes a number of bugs, but would have to see perf impacts.

result.set_axis(new_ax, axis, inplace=True)
return result.__finalize__(self)

def truncate(
self: FrameOrSeries, before=None, after=None, axis=None, copy: bool_t = True
Expand Down