Skip to content

Commit b6cb1a4

Browse files
authored
REF: avoid internals in tshift (#33056)
1 parent 5e3a5f6 commit b6cb1a4

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

pandas/core/generic.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -8978,7 +8978,7 @@ def slice_shift(self: FrameOrSeries, periods: int = 1, axis=0) -> FrameOrSeries:
89788978
return new_obj.__finalize__(self)
89798979

89808980
def tshift(
8981-
self: FrameOrSeries, periods: int = 1, freq=None, axis=0
8981+
self: FrameOrSeries, periods: int = 1, freq=None, axis: Axis = 0
89828982
) -> FrameOrSeries:
89838983
"""
89848984
Shift the time index, using the index's frequency if available.
@@ -9020,22 +9020,22 @@ def tshift(
90209020
if isinstance(freq, str):
90219021
freq = to_offset(freq)
90229022

9023-
block_axis = self._get_block_manager_axis(axis)
9023+
axis = self._get_axis_number(axis)
90249024
if isinstance(index, PeriodIndex):
90259025
orig_freq = to_offset(index.freq)
9026-
if freq == orig_freq:
9027-
new_data = self._data.copy()
9028-
new_data.axes[block_axis] = index.shift(periods)
9029-
elif orig_freq is not None:
9026+
if freq != orig_freq:
9027+
assert orig_freq is not None # for mypy
90309028
raise ValueError(
90319029
f"Given freq {freq.rule_code} does not match "
90329030
f"PeriodIndex freq {orig_freq.rule_code}"
90339031
)
9032+
new_ax = index.shift(periods)
90349033
else:
9035-
new_data = self._data.copy()
9036-
new_data.axes[block_axis] = index.shift(periods, freq)
9034+
new_ax = index.shift(periods, freq)
90379035

9038-
return self._constructor(new_data).__finalize__(self)
9036+
result = self.copy()
9037+
result.set_axis(new_ax, axis, inplace=True)
9038+
return result.__finalize__(self)
90399039

90409040
def truncate(
90419041
self: FrameOrSeries, before=None, after=None, axis=None, copy: bool_t = True

0 commit comments

Comments
 (0)