@@ -182,7 +182,7 @@ class NDFrame(PandasObject, SelectionMixin, indexing.IndexingMixin):
182
182
]
183
183
_internal_names_set : Set [str ] = set (_internal_names )
184
184
_accessors : Set [str ] = set ()
185
- _deprecations : FrozenSet [str ] = frozenset (["get_values" ])
185
+ _deprecations : FrozenSet [str ] = frozenset (["get_values" , "tshift" ])
186
186
_metadata : List [str ] = []
187
187
_is_copy = None
188
188
_mgr : BlockManager
@@ -9162,7 +9162,9 @@ def shift(
9162
9162
When `freq` is not passed, shift the index without realigning the data.
9163
9163
If `freq` is passed (in this case, the index must be date or datetime,
9164
9164
or it will raise a `NotImplementedError`), the index will be
9165
- increased using the periods and the `freq`.
9165
+ increased using the periods and the `freq`. `freq` can be inferred
9166
+ when specified as "infer" as long as either freq or inferred_freq
9167
+ attribute is set in the index.
9166
9168
9167
9169
Parameters
9168
9170
----------
@@ -9173,6 +9175,9 @@ def shift(
9173
9175
If `freq` is specified then the index values are shifted but the
9174
9176
data is not realigned. That is, use `freq` if you would like to
9175
9177
extend the index when shifting and preserve the original data.
9178
+ If `freq` is specified as "infer" then it will be inferred from
9179
+ the freq or inferred_freq attributes of the index. If neither of
9180
+ those attributes exist, a ValueError is thrown
9176
9181
axis : {{0 or 'index', 1 or 'columns', None}}, default None
9177
9182
Shift direction.
9178
9183
fill_value : object, optional
@@ -9182,7 +9187,7 @@ def shift(
9182
9187
For datetime, timedelta, or period data, etc. :attr:`NaT` is used.
9183
9188
For extension dtypes, ``self.dtype.na_value`` is used.
9184
9189
9185
- .. versionchanged:: 0.24 .0
9190
+ .. versionchanged:: 1.1 .0
9186
9191
9187
9192
Returns
9188
9193
-------
@@ -9199,46 +9204,99 @@ def shift(
9199
9204
9200
9205
Examples
9201
9206
--------
9202
- >>> df = pd.DataFrame({{'Col1': [10, 20, 15, 30, 45],
9203
- ... 'Col2': [13, 23, 18, 33, 48],
9204
- ... 'Col3': [17, 27, 22, 37, 52]}})
9207
+ >>> df = pd.DataFrame({{"Col1": [10, 20, 15, 30, 45],
9208
+ ... "Col2": [13, 23, 18, 33, 48],
9209
+ ... "Col3": [17, 27, 22, 37, 52]}},
9210
+ ... index=pd.date_range("2020-01-01", "2020-01-05"))
9211
+ >>> df
9212
+ Col1 Col2 Col3
9213
+ 2020-01-01 10 13 17
9214
+ 2020-01-02 20 23 27
9215
+ 2020-01-03 15 18 22
9216
+ 2020-01-04 30 33 37
9217
+ 2020-01-05 45 48 52
9205
9218
9206
9219
>>> df.shift(periods=3)
9207
- Col1 Col2 Col3
9208
- 0 NaN NaN NaN
9209
- 1 NaN NaN NaN
9210
- 2 NaN NaN NaN
9211
- 3 10.0 13.0 17.0
9212
- 4 20.0 23.0 27.0
9213
-
9214
- >>> df.shift(periods=1, axis=' columns' )
9215
- Col1 Col2 Col3
9216
- 0 NaN 10.0 13.0
9217
- 1 NaN 20.0 23.0
9218
- 2 NaN 15.0 18.0
9219
- 3 NaN 30.0 33.0
9220
- 4 NaN 45.0 48.0
9220
+ Col1 Col2 Col3
9221
+ 2020-01-01 NaN NaN NaN
9222
+ 2020-01-02 NaN NaN NaN
9223
+ 2020-01-03 NaN NaN NaN
9224
+ 2020-01-04 10.0 13.0 17.0
9225
+ 2020-01-05 20.0 23.0 27.0
9226
+
9227
+ >>> df.shift(periods=1, axis=" columns" )
9228
+ Col1 Col2 Col3
9229
+ 2020-01-01 NaN 10.0 13.0
9230
+ 2020-01-02 NaN 20.0 23.0
9231
+ 2020-01-03 NaN 15.0 18.0
9232
+ 2020-01-04 NaN 30.0 33.0
9233
+ 2020-01-05 NaN 45.0 48.0
9221
9234
9222
9235
>>> df.shift(periods=3, fill_value=0)
9223
- Col1 Col2 Col3
9224
- 0 0 0 0
9225
- 1 0 0 0
9226
- 2 0 0 0
9227
- 3 10 13 17
9228
- 4 20 23 27
9236
+ Col1 Col2 Col3
9237
+ 2020-01-01 0 0 0
9238
+ 2020-01-02 0 0 0
9239
+ 2020-01-03 0 0 0
9240
+ 2020-01-04 10 13 17
9241
+ 2020-01-05 20 23 27
9242
+
9243
+ >>> df.shift(periods=3, freq="D")
9244
+ Col1 Col2 Col3
9245
+ 2020-01-04 10 13 17
9246
+ 2020-01-05 20 23 27
9247
+ 2020-01-06 15 18 22
9248
+ 2020-01-07 30 33 37
9249
+ 2020-01-08 45 48 52
9250
+
9251
+ >>> df.shift(periods=3, freq="infer")
9252
+ Col1 Col2 Col3
9253
+ 2020-01-04 10 13 17
9254
+ 2020-01-05 20 23 27
9255
+ 2020-01-06 15 18 22
9256
+ 2020-01-07 30 33 37
9257
+ 2020-01-08 45 48 52
9229
9258
"""
9230
9259
if periods == 0 :
9231
9260
return self .copy ()
9232
9261
9233
- block_axis = self ._get_block_manager_axis (axis )
9234
9262
if freq is None :
9263
+ # when freq is None, data is shifted, index is not
9264
+ block_axis = self ._get_block_manager_axis (axis )
9235
9265
new_data = self ._mgr .shift (
9236
9266
periods = periods , axis = block_axis , fill_value = fill_value
9237
9267
)
9268
+ return self ._constructor (new_data ).__finalize__ (self , method = "shift" )
9269
+
9270
+ # when freq is given, index is shifted, data is not
9271
+ index = self ._get_axis (axis )
9272
+
9273
+ if freq == "infer" :
9274
+ freq = getattr (index , "freq" , None )
9275
+
9276
+ if freq is None :
9277
+ freq = getattr (index , "inferred_freq" , None )
9278
+
9279
+ if freq is None :
9280
+ msg = "Freq was not set in the index hence cannot be inferred"
9281
+ raise ValueError (msg )
9282
+
9283
+ elif isinstance (freq , str ):
9284
+ freq = to_offset (freq )
9285
+
9286
+ if isinstance (index , PeriodIndex ):
9287
+ orig_freq = to_offset (index .freq )
9288
+ if freq != orig_freq :
9289
+ assert orig_freq is not None # for mypy
9290
+ raise ValueError (
9291
+ f"Given freq { freq .rule_code } does not match "
9292
+ f"PeriodIndex freq { orig_freq .rule_code } "
9293
+ )
9294
+ new_ax = index .shift (periods )
9238
9295
else :
9239
- return self . tshift (periods , freq )
9296
+ new_ax = index . shift (periods , freq )
9240
9297
9241
- return self ._constructor (new_data ).__finalize__ (self , method = "shift" )
9298
+ result = self .set_axis (new_ax , axis )
9299
+ return result .__finalize__ (self , method = "shift" )
9242
9300
9243
9301
def slice_shift (self : FrameOrSeries , periods : int = 1 , axis = 0 ) -> FrameOrSeries :
9244
9302
"""
@@ -9283,6 +9341,9 @@ def tshift(
9283
9341
"""
9284
9342
Shift the time index, using the index's frequency if available.
9285
9343
9344
+ .. deprecated:: 1.1.0
9345
+ Use `shift` instead.
9346
+
9286
9347
Parameters
9287
9348
----------
9288
9349
periods : int
@@ -9303,39 +9364,19 @@ def tshift(
9303
9364
attributes of the index. If neither of those attributes exist, a
9304
9365
ValueError is thrown
9305
9366
"""
9306
- index = self ._get_axis (axis )
9307
- if freq is None :
9308
- freq = getattr (index , "freq" , None )
9309
-
9310
- if freq is None :
9311
- freq = getattr (index , "inferred_freq" , None )
9367
+ warnings .warn (
9368
+ (
9369
+ "tshift is deprecated and will be removed in a future version. "
9370
+ "Please use shift instead."
9371
+ ),
9372
+ FutureWarning ,
9373
+ stacklevel = 2 ,
9374
+ )
9312
9375
9313
9376
if freq is None :
9314
- msg = "Freq was not given and was not set in the index"
9315
- raise ValueError (msg )
9316
-
9317
- if periods == 0 :
9318
- return self
9319
-
9320
- if isinstance (freq , str ):
9321
- freq = to_offset (freq )
9322
-
9323
- axis = self ._get_axis_number (axis )
9324
- if isinstance (index , PeriodIndex ):
9325
- orig_freq = to_offset (index .freq )
9326
- if freq != orig_freq :
9327
- assert orig_freq is not None # for mypy
9328
- raise ValueError (
9329
- f"Given freq { freq .rule_code } does not match "
9330
- f"PeriodIndex freq { orig_freq .rule_code } "
9331
- )
9332
- new_ax = index .shift (periods )
9333
- else :
9334
- new_ax = index .shift (periods , freq )
9377
+ freq = "infer"
9335
9378
9336
- result = self .copy ()
9337
- result .set_axis (new_ax , axis , inplace = True )
9338
- return result .__finalize__ (self , method = "tshift" )
9379
+ return self .shift (periods , freq , axis )
9339
9380
9340
9381
def truncate (
9341
9382
self : FrameOrSeries , before = None , after = None , axis = None , copy : bool_t = True
0 commit comments