@@ -8978,7 +8978,7 @@ def slice_shift(self: FrameOrSeries, periods: int = 1, axis=0) -> FrameOrSeries:
8978
8978
return new_obj .__finalize__ (self )
8979
8979
8980
8980
def tshift (
8981
- self : FrameOrSeries , periods : int = 1 , freq = None , axis = 0
8981
+ self : FrameOrSeries , periods : int = 1 , freq = None , axis : Axis = 0
8982
8982
) -> FrameOrSeries :
8983
8983
"""
8984
8984
Shift the time index, using the index's frequency if available.
@@ -9020,22 +9020,22 @@ def tshift(
9020
9020
if isinstance (freq , str ):
9021
9021
freq = to_offset (freq )
9022
9022
9023
- block_axis = self ._get_block_manager_axis (axis )
9023
+ axis = self ._get_axis_number (axis )
9024
9024
if isinstance (index , PeriodIndex ):
9025
9025
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
9030
9028
raise ValueError (
9031
9029
f"Given freq { freq .rule_code } does not match "
9032
9030
f"PeriodIndex freq { orig_freq .rule_code } "
9033
9031
)
9032
+ new_ax = index .shift (periods )
9034
9033
else :
9035
- new_data = self ._data .copy ()
9036
- new_data .axes [block_axis ] = index .shift (periods , freq )
9034
+ new_ax = index .shift (periods , freq )
9037
9035
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 )
9039
9039
9040
9040
def truncate (
9041
9041
self : FrameOrSeries , before = None , after = None , axis = None , copy : bool_t = True
0 commit comments