From d37efb9585c4c3c6519bf879680552a877a1e613 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 2 Feb 2022 18:13:18 -0800 Subject: [PATCH] REF: use np_can_hold_element pattern in Block.shift --- pandas/core/internals/blocks.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 2c1724adf0300..158bcd5d60397 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1139,14 +1139,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]: """