Skip to content

Commit

Permalink
REF: use np_can_hold_element pattern in Block.shift (#45792)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Feb 3, 2022
1 parent fc73758 commit 5c757e9
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,14 +1138,16 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> list[Blo

fill_value = self._standardize_fill_value(fill_value)

if not self._can_hold_element(fill_value):
try:
casted = np_can_hold_element(self.dtype, fill_value)
except LossySetitemError:
nb = self.coerce_to_target_dtype(fill_value)
return nb.shift(periods, axis=axis, fill_value=fill_value)

values = cast(np.ndarray, self.values)
new_values = shift(values, periods, axis, fill_value)

return [self.make_block(new_values)]
else:
values = cast(np.ndarray, self.values)
new_values = shift(values, periods, axis, casted)
return [self.make_block(new_values)]

def where(self, other, cond) -> list[Block]:
"""
Expand Down

0 comments on commit 5c757e9

Please sign in to comment.