-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
API/DEPR: 'periods' argument instead of 'n' for DatetimeIndex.shift() #22697
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
Changes from 8 commits
3e3c5a0
39141c0
1df9a9f
5b592f6
f684245
240aa7e
11169fa
6f627ac
d0d8b83
ff335a5
de54cce
d9bbfe8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
| from pandas.core.algorithms import checked_add_with_arr | ||
|
|
||
| from .base import ExtensionOpsMixin | ||
| from pandas.util._decorators import deprecate_kwarg | ||
|
|
||
|
|
||
| def _make_comparison_op(op, cls): | ||
|
|
@@ -521,40 +522,51 @@ def _addsub_offset_array(self, other, op): | |
| kwargs['freq'] = 'infer' | ||
| return type(self)(res_values, **kwargs) | ||
|
|
||
| def shift(self, n, freq=None): | ||
| @deprecate_kwarg(old_arg_name='n', new_arg_name='periods') | ||
| def shift(self, periods, freq=None): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll need to deprecate periods. Your change would break people doing This could be somewhat tricky to do... I think you may have to make the signature
lmk if you need help.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @TomAugspurger Thanks. I started parsing manually but couldn't make some tests pass. Then I realized there is a decorator in |
||
| """ | ||
| Specialized shift which produces a Datetime/Timedelta Array/Index | ||
| Shift index by desired number of time frequency increments. | ||
|
|
||
| This method is for shifting the values of datetime-like indexes | ||
| by a specified time increment a given number of times. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| n : int | ||
| Periods to shift by | ||
| freq : DateOffset or timedelta-like, optional | ||
| periods : int | ||
| Number of periods (or increments) to shift by, | ||
| can be positive or negative. | ||
arminv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| freq : pandas.DateOffset, pandas.Timedelta or string, optional | ||
| Frequency increment to shift by. | ||
| If None, the index is shifted by its own `freq` attribute. | ||
| Offset aliases are valid strings, e.g., 'D', 'W', 'M' etc. | ||
|
|
||
| Returns | ||
| ------- | ||
| shifted : same type as self | ||
| pandas.DatetimeIndex | ||
| Shifted index. | ||
|
|
||
| See Also | ||
| -------- | ||
arminv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Index.shift : Shift values of Index. | ||
| """ | ||
| if freq is not None and freq != self.freq: | ||
| if isinstance(freq, compat.string_types): | ||
| freq = frequencies.to_offset(freq) | ||
| offset = n * freq | ||
| offset = periods * freq | ||
| result = self + offset | ||
|
|
||
| if hasattr(self, 'tz'): | ||
| result._tz = self.tz | ||
|
|
||
| return result | ||
|
|
||
| if n == 0: | ||
| if periods == 0: | ||
| # immutable so OK | ||
| return self.copy() | ||
|
|
||
| if self.freq is None: | ||
| raise NullFrequencyError("Cannot shift with no freq") | ||
|
|
||
| start = self[0] + n * self.freq | ||
| end = self[-1] + n * self.freq | ||
| start = self[0] + periods * self.freq | ||
| end = self[-1] + periods * self.freq | ||
| attribs = self._get_attributes_dict() | ||
| return self._generate_range(start=start, end=end, periods=None, | ||
| **attribs) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -540,6 +540,16 @@ def test_shift(self): | |
| shifted = rng.shift(1, freq=CDay()) | ||
| assert shifted[0] == rng[0] + CDay() | ||
|
|
||
| def test_shift_periods(self): | ||
| # GH #22458 : | ||
arminv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| idx = pd.DatetimeIndex(start=START, end=END, | ||
| periods=3) | ||
| tm.assert_index_equal(idx.shift(periods=0), idx) | ||
| tm.assert_index_equal(idx.shift(0), idx) | ||
| with tm.assert_produces_warning(FutureWarning, | ||
| check_stacklevel=False): | ||
|
||
| tm.assert_index_equal(idx.shift(n=0), idx) | ||
|
|
||
| def test_pickle_unpickle(self): | ||
| unpickled = tm.round_trip_pickle(self.rng) | ||
| assert unpickled.freq is not None | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.