Skip to content

Commit 5742eb6

Browse files
jbrockmendeljreback
authored andcommitted
Disallow non-scalar fill_value in maybe_upcast (#29362)
1 parent 7f6561d commit 5742eb6

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

pandas/core/dtypes/cast.py

+2
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,8 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy=False):
686686
dtype : if None, then use the dtype of the values, else coerce to this type
687687
copy : if True always make a copy even if no upcast is required
688688
"""
689+
if not is_scalar(fill_value):
690+
raise ValueError("fill_value must be a scalar")
689691

690692
if is_extension_type(values):
691693
if copy:

pandas/core/internals/blocks.py

+4
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,10 @@ def diff(self, n: int, axis: int = 1) -> List["Block"]:
12831283
def shift(self, periods, axis=0, fill_value=None):
12841284
""" shift the block by periods, possibly upcast """
12851285

1286+
if not lib.is_scalar(fill_value):
1287+
# We could go further and require e.g. self._can_hold_element(fv)
1288+
raise ValueError("fill_value must be a scalar")
1289+
12861290
# convert integer to float if necessary. need to do a lot more than
12871291
# that, handle boolean etc also
12881292
new_values, fill_value = maybe_upcast(self.values, fill_value)

pandas/core/internals/construction.py

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ def masked_rec_array_to_mgr(data, index, columns, dtype, copy):
9797
# fill if needed
9898
new_arrays = []
9999
for fv, arr, col in zip(fill_value, arrays, arr_columns):
100+
# TODO: numpy docs suggest fv must be scalar, but could it be
101+
# non-scalar for object dtype?
102+
assert lib.is_scalar(fv), fv
100103
mask = ma.getmaskarray(data[col])
101104
if mask.any():
102105
arr, fv = maybe_upcast(arr, fill_value=fv, copy=True)

0 commit comments

Comments
 (0)