Skip to content

Commit 4b066bb

Browse files
authored
CLN: remove ndarray cases from maybe_promote (#39736)
1 parent 602502e commit 4b066bb

File tree

2 files changed

+7
-48
lines changed

2 files changed

+7
-48
lines changed

pandas/core/dtypes/cast.py

+7-20
Original file line numberDiff line numberDiff line change
@@ -520,28 +520,15 @@ def maybe_promote(dtype: DtypeObj, fill_value=np.nan):
520520
ValueError
521521
If fill_value is a non-scalar and dtype is not object.
522522
"""
523-
if not is_scalar(fill_value) and not is_object_dtype(dtype):
523+
if not is_scalar(fill_value):
524524
# with object dtype there is nothing to promote, and the user can
525525
# pass pretty much any weird fill_value they like
526-
raise ValueError("fill_value must be a scalar")
527-
528-
# if we passed an array here, determine the fill value by dtype
529-
if isinstance(fill_value, np.ndarray):
530-
if issubclass(fill_value.dtype.type, (np.datetime64, np.timedelta64)):
531-
fill_value = fill_value.dtype.type("NaT", "ns")
532-
else:
533-
534-
# we need to change to object type as our
535-
# fill_value is of object type
536-
if fill_value.dtype == np.object_:
537-
dtype = np.dtype(np.object_)
538-
fill_value = np.nan
539-
540-
if dtype == np.object_ or dtype.kind in ["U", "S"]:
541-
# We treat string-like dtypes as object, and _always_ fill
542-
# with np.nan
543-
fill_value = np.nan
544-
dtype = np.dtype(np.object_)
526+
if not is_object_dtype(dtype):
527+
# with object dtype there is nothing to promote, and the user can
528+
# pass pretty much any weird fill_value they like
529+
raise ValueError("fill_value must be a scalar")
530+
dtype = np.dtype(object)
531+
return dtype, fill_value
545532

546533
# returns tuple of (dtype, fill_value)
547534
if issubclass(dtype.type, np.datetime64):

pandas/tests/dtypes/cast/test_promote.py

-28
Original file line numberDiff line numberDiff line change
@@ -605,31 +605,3 @@ def test_maybe_promote_any_numpy_dtype_with_na(any_numpy_dtype_reduced, nulls_fi
605605
exp_val_for_scalar = np.nan
606606

607607
_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)
608-
609-
610-
@pytest.mark.parametrize("dim", [0, 2, 3])
611-
def test_maybe_promote_dimensions(any_numpy_dtype_reduced, dim):
612-
dtype = np.dtype(any_numpy_dtype_reduced)
613-
614-
# create 0-dim array of given dtype; casts "1" to correct dtype
615-
fill_array = np.array(1, dtype=dtype)
616-
617-
# expand to desired dimension:
618-
for _ in range(dim):
619-
fill_array = np.expand_dims(fill_array, 0)
620-
621-
if dtype != object:
622-
# test against 1-dimensional case
623-
with pytest.raises(ValueError, match="fill_value must be a scalar"):
624-
maybe_promote(dtype, np.array([1], dtype=dtype))
625-
626-
with pytest.raises(ValueError, match="fill_value must be a scalar"):
627-
maybe_promote(dtype, fill_array)
628-
629-
else:
630-
expected_dtype, expected_missing_value = maybe_promote(
631-
dtype, np.array([1], dtype=dtype)
632-
)
633-
result_dtype, result_missing_value = maybe_promote(dtype, fill_array)
634-
assert result_dtype == expected_dtype
635-
_assert_match(result_missing_value, expected_missing_value)

0 commit comments

Comments
 (0)