-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
REG: dt64 shift with integer fill_value #32591
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 all commits
87ff829
a7fce3b
8bc5cb1
56f0f4b
5417e49
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 |
---|---|---|
|
@@ -745,6 +745,57 @@ def _from_factorized(cls, values, original): | |
def _values_for_argsort(self): | ||
return self._data | ||
|
||
@Appender(ExtensionArray.shift.__doc__) | ||
def shift(self, periods=1, fill_value=None, axis=0): | ||
if not self.size or periods == 0: | ||
return self.copy() | ||
|
||
if is_valid_nat_for_dtype(fill_value, self.dtype): | ||
fill_value = NaT | ||
elif not isinstance(fill_value, self._recognized_scalars): | ||
# only warn if we're not going to raise | ||
if self._scalar_type is Period and lib.is_integer(fill_value): | ||
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. isinstance? 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. "is" is on purpose here |
||
# kludge for #31971 since Period(integer) tries to cast to str | ||
new_fill = Period._from_ordinal(fill_value, freq=self.freq) | ||
else: | ||
new_fill = self._scalar_type(fill_value) | ||
|
||
# stacklevel here is chosen to be correct when called from | ||
# DataFrame.shift or Series.shift | ||
warnings.warn( | ||
f"Passing {type(fill_value)} to shift is deprecated and " | ||
"will raise in a future version, pass " | ||
f"{self._scalar_type.__name__} instead.", | ||
FutureWarning, | ||
stacklevel=7, | ||
) | ||
fill_value = new_fill | ||
|
||
fill_value = self._unbox_scalar(fill_value) | ||
|
||
new_values = self._data | ||
|
||
# make sure array sent to np.roll is c_contiguous | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
f_ordered = new_values.flags.f_contiguous | ||
if f_ordered: | ||
new_values = new_values.T | ||
axis = new_values.ndim - axis - 1 | ||
|
||
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. this is similar to the shift we have in block values yes? this should at some point be a routine in missing.py 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. ideally doing it in this PR but if too hard to backport ok 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. Yah, i was thinking core.algos directory for algorithms-like functions that are specifically only ndarray/EA |
||
new_values = np.roll(new_values, periods, axis=axis) | ||
|
||
axis_indexer = [slice(None)] * self.ndim | ||
if periods > 0: | ||
axis_indexer[axis] = slice(None, periods) | ||
else: | ||
axis_indexer[axis] = slice(periods, None) | ||
new_values[tuple(axis_indexer)] = fill_value | ||
|
||
# restore original order | ||
if f_ordered: | ||
new_values = new_values.T | ||
|
||
return type(self)._simple_new(new_values, dtype=self.dtype) | ||
|
||
# ------------------------------------------------------------------ | ||
# Additional array methods | ||
# These are not part of the EA API, but we implement them because | ||
|
Uh oh!
There was an error while loading. Please reload this page.