Skip to content

Commit 43245c9

Browse files
authored
REF: use standard coerce pattern in Block.shift (#45353)
1 parent 09d693f commit 43245c9

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

pandas/core/internals/blocks.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
infer_dtype_from,
4343
maybe_downcast_numeric,
4444
maybe_downcast_to_dtype,
45-
maybe_upcast,
4645
soft_convert_objects,
4746
)
4847
from pandas.core.dtypes.common import (
@@ -1149,11 +1148,25 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> list[Blo
11491148
# convert integer to float if necessary. need to do a lot more than
11501149
# that, handle boolean etc also
11511150

1152-
values = cast(np.ndarray, self.values)
1151+
# Note: periods is never 0 here, as that is handled at the top of
1152+
# NDFrame.shift. If that ever changes, we can do a check for periods=0
1153+
# and possibly avoid coercing.
1154+
1155+
if not lib.is_scalar(fill_value) and self.dtype != _dtype_obj:
1156+
# with object dtype there is nothing to promote, and the user can
1157+
# pass pretty much any weird fill_value they like
1158+
# see test_shift_object_non_scalar_fill
1159+
raise ValueError("fill_value must be a scalar")
11531160

1154-
new_values, fill_value = maybe_upcast(values, fill_value)
1161+
if is_valid_na_for_dtype(fill_value, self.dtype) and self.dtype != _dtype_obj:
1162+
fill_value = self.fill_value
11551163

1156-
new_values = shift(new_values, periods, axis, fill_value)
1164+
if not self._can_hold_element(fill_value):
1165+
nb = self.coerce_to_target_dtype(fill_value)
1166+
return nb.shift(periods, axis=axis, fill_value=fill_value)
1167+
1168+
values = cast(np.ndarray, self.values)
1169+
new_values = shift(values, periods, axis, fill_value)
11571170

11581171
return [self.make_block(new_values)]
11591172

0 commit comments

Comments
 (0)