Skip to content

Commit 5c757e9

Browse files
authored
REF: use np_can_hold_element pattern in Block.shift (#45792)
1 parent fc73758 commit 5c757e9

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

pandas/core/internals/blocks.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1138,14 +1138,16 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> list[Blo
11381138

11391139
fill_value = self._standardize_fill_value(fill_value)
11401140

1141-
if not self._can_hold_element(fill_value):
1141+
try:
1142+
casted = np_can_hold_element(self.dtype, fill_value)
1143+
except LossySetitemError:
11421144
nb = self.coerce_to_target_dtype(fill_value)
11431145
return nb.shift(periods, axis=axis, fill_value=fill_value)
11441146

1145-
values = cast(np.ndarray, self.values)
1146-
new_values = shift(values, periods, axis, fill_value)
1147-
1148-
return [self.make_block(new_values)]
1147+
else:
1148+
values = cast(np.ndarray, self.values)
1149+
new_values = shift(values, periods, axis, casted)
1150+
return [self.make_block(new_values)]
11491151

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

0 commit comments

Comments
 (0)