Skip to content

Commit c796aa4

Browse files
jbrockmendelluckyvs1
authored andcommitted
REF: simplify maybe_upcast (pandas-dev#38428)
1 parent 045f0d0 commit c796aa4

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

pandas/core/dtypes/cast.py

+8-22
Original file line numberDiff line numberDiff line change
@@ -924,45 +924,31 @@ def maybe_infer_dtype_type(element):
924924

925925

926926
def maybe_upcast(
927-
values: ArrayLike,
927+
values: np.ndarray,
928928
fill_value: Scalar = np.nan,
929-
dtype: Dtype = None,
930929
copy: bool = False,
931-
) -> Tuple[ArrayLike, Scalar]:
930+
) -> Tuple[np.ndarray, Scalar]:
932931
"""
933932
Provide explicit type promotion and coercion.
934933
935934
Parameters
936935
----------
937-
values : ndarray or ExtensionArray
938-
The array that we want to maybe upcast.
936+
values : np.ndarray
937+
The array that we may want to upcast.
939938
fill_value : what we want to fill with
940-
dtype : if None, then use the dtype of the values, else coerce to this type
941939
copy : bool, default True
942940
If True always make a copy even if no upcast is required.
943941
944942
Returns
945943
-------
946-
values: ndarray or ExtensionArray
944+
values: np.ndarray
947945
the original array, possibly upcast
948946
fill_value:
949947
the fill value, possibly upcast
950948
"""
951-
if not is_scalar(fill_value) and not is_object_dtype(values.dtype):
952-
# We allow arbitrary fill values for object dtype
953-
raise ValueError("fill_value must be a scalar")
954-
955-
if is_extension_array_dtype(values):
956-
if copy:
957-
values = values.copy()
958-
else:
959-
if dtype is None:
960-
dtype = values.dtype
961-
new_dtype, fill_value = maybe_promote(dtype, fill_value)
962-
if new_dtype != values.dtype:
963-
values = values.astype(new_dtype)
964-
elif copy:
965-
values = values.copy()
949+
new_dtype, fill_value = maybe_promote(values.dtype, fill_value)
950+
# We get a copy in all cases _except_ (values.dtype == new_dtype and not copy)
951+
values = values.astype(new_dtype, copy=copy)
966952

967953
return values, fill_value
968954

0 commit comments

Comments
 (0)