|
38 | 38 | from pandas.core.algorithms import checked_add_with_arr
|
39 | 39 |
|
40 | 40 | from .base import ExtensionOpsMixin
|
| 41 | +from pandas.util._decorators import deprecate_kwarg |
41 | 42 |
|
42 | 43 |
|
43 | 44 | def _make_comparison_op(op, cls):
|
@@ -522,40 +523,54 @@ def _addsub_offset_array(self, other, op):
|
522 | 523 | kwargs['freq'] = 'infer'
|
523 | 524 | return type(self)(res_values, **kwargs)
|
524 | 525 |
|
525 |
| - def shift(self, n, freq=None): |
| 526 | + @deprecate_kwarg(old_arg_name='n', new_arg_name='periods') |
| 527 | + def shift(self, periods, freq=None): |
526 | 528 | """
|
527 |
| - Specialized shift which produces a Datetime/Timedelta Array/Index |
| 529 | + Shift index by desired number of time frequency increments. |
| 530 | +
|
| 531 | + This method is for shifting the values of datetime-like indexes |
| 532 | + by a specified time increment a given number of times. |
528 | 533 |
|
529 | 534 | Parameters
|
530 | 535 | ----------
|
531 |
| - n : int |
532 |
| - Periods to shift by |
533 |
| - freq : DateOffset or timedelta-like, optional |
| 536 | + periods : int |
| 537 | + Number of periods (or increments) to shift by, |
| 538 | + can be positive or negative. |
| 539 | +
|
| 540 | + .. versionchanged:: 0.24.0 |
| 541 | +
|
| 542 | + freq : pandas.DateOffset, pandas.Timedelta or string, optional |
| 543 | + Frequency increment to shift by. |
| 544 | + If None, the index is shifted by its own `freq` attribute. |
| 545 | + Offset aliases are valid strings, e.g., 'D', 'W', 'M' etc. |
534 | 546 |
|
535 | 547 | Returns
|
536 | 548 | -------
|
537 |
| - shifted : same type as self |
| 549 | + pandas.DatetimeIndex |
| 550 | + Shifted index. |
| 551 | +
|
| 552 | + See Also |
| 553 | + -------- |
| 554 | + Index.shift : Shift values of Index. |
538 | 555 | """
|
539 | 556 | if freq is not None and freq != self.freq:
|
540 | 557 | if isinstance(freq, compat.string_types):
|
541 | 558 | freq = frequencies.to_offset(freq)
|
542 |
| - offset = n * freq |
| 559 | + offset = periods * freq |
543 | 560 | result = self + offset
|
544 |
| - |
545 | 561 | if hasattr(self, 'tz'):
|
546 | 562 | result._tz = self.tz
|
547 |
| - |
548 | 563 | return result
|
549 | 564 |
|
550 |
| - if n == 0: |
| 565 | + if periods == 0: |
551 | 566 | # immutable so OK
|
552 | 567 | return self.copy()
|
553 | 568 |
|
554 | 569 | if self.freq is None:
|
555 | 570 | raise NullFrequencyError("Cannot shift with no freq")
|
556 | 571 |
|
557 |
| - start = self[0] + n * self.freq |
558 |
| - end = self[-1] + n * self.freq |
| 572 | + start = self[0] + periods * self.freq |
| 573 | + end = self[-1] + periods * self.freq |
559 | 574 | attribs = self._get_attributes_dict()
|
560 | 575 | return self._generate_range(start=start, end=end, periods=None,
|
561 | 576 | **attribs)
|
|
0 commit comments