@@ -924,45 +924,31 @@ def maybe_infer_dtype_type(element):
924
924
925
925
926
926
def maybe_upcast (
927
- values : ArrayLike ,
927
+ values : np . ndarray ,
928
928
fill_value : Scalar = np .nan ,
929
- dtype : Dtype = None ,
930
929
copy : bool = False ,
931
- ) -> Tuple [ArrayLike , Scalar ]:
930
+ ) -> Tuple [np . ndarray , Scalar ]:
932
931
"""
933
932
Provide explicit type promotion and coercion.
934
933
935
934
Parameters
936
935
----------
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.
939
938
fill_value : what we want to fill with
940
- dtype : if None, then use the dtype of the values, else coerce to this type
941
939
copy : bool, default True
942
940
If True always make a copy even if no upcast is required.
943
941
944
942
Returns
945
943
-------
946
- values: ndarray or ExtensionArray
944
+ values: np. ndarray
947
945
the original array, possibly upcast
948
946
fill_value:
949
947
the fill value, possibly upcast
950
948
"""
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 )
966
952
967
953
return values , fill_value
968
954
0 commit comments