Skip to content

Commit bdb8783

Browse files
jbrockmendelproost
authored andcommitted
CLN: Assorted cleanups (pandas-dev#30260)
1 parent 45e0dd7 commit bdb8783

File tree

12 files changed

+137
-104
lines changed

12 files changed

+137
-104
lines changed

pandas/core/dtypes/cast.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,13 @@ def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray, other):
244244
necessary.
245245
mask : boolean ndarray
246246
other : scalar
247-
The source value
247+
The source value.
248248
249249
Returns
250250
-------
251251
result : ndarray
252-
changed : boolean
253-
Set to true if the result array was upcasted
252+
changed : bool
253+
Set to true if the result array was upcasted.
254254
255255
Examples
256256
--------
@@ -337,6 +337,21 @@ def changeit():
337337

338338

339339
def maybe_promote(dtype, fill_value=np.nan):
340+
"""
341+
Find the minimal dtype that can hold both the given dtype and fill_value.
342+
343+
Parameters
344+
----------
345+
dtype : np.dtype or ExtensionDtype
346+
fill_value : scalar, default np.nan
347+
348+
Returns
349+
-------
350+
dtype
351+
Upcasted from dtype argument if necessary.
352+
fill_value
353+
Upcasted from fill_value argument if necessary.
354+
"""
340355
if not is_scalar(fill_value) and not is_object_dtype(dtype):
341356
# with object dtype there is nothing to promote, and the user can
342357
# pass pretty much any weird fill_value they like
@@ -592,11 +607,11 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
592607

593608
def infer_dtype_from_array(arr, pandas_dtype: bool = False):
594609
"""
595-
Infer the dtype from a scalar or array.
610+
Infer the dtype from an array.
596611
597612
Parameters
598613
----------
599-
arr : scalar or array
614+
arr : array
600615
pandas_dtype : bool, default False
601616
whether to infer dtype including pandas extension types.
602617
If False, array belongs to pandas extension types
@@ -622,7 +637,6 @@ def infer_dtype_from_array(arr, pandas_dtype: bool = False):
622637
623638
>>> infer_dtype_from_array([1, '1'])
624639
(numpy.object_, [1, '1'])
625-
626640
"""
627641

628642
if isinstance(arr, np.ndarray):
@@ -686,10 +700,12 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
686700
687701
Parameters
688702
----------
689-
values : the ndarray that we want to maybe upcast
703+
values : ndarray or ExtensionArray
704+
The array that we want to maybe upcast.
690705
fill_value : what we want to fill with
691706
dtype : if None, then use the dtype of the values, else coerce to this type
692-
copy : if True always make a copy even if no upcast is required
707+
copy : bool, default True
708+
If True always make a copy even if no upcast is required.
693709
"""
694710
if not is_scalar(fill_value) and not is_object_dtype(values.dtype):
695711
# We allow arbitrary fill values for object dtype

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4354,7 +4354,7 @@ def _maybe_casted_values(index, labels=None):
43544354
values = values._data
43554355

43564356
if mask.any():
4357-
values, changed = maybe_upcast_putmask(values, mask, np.nan)
4357+
values, _ = maybe_upcast_putmask(values, mask, np.nan)
43584358

43594359
if issubclass(values_type, DatetimeLikeArray):
43604360
values = values_type(values, dtype=values_dtype)

pandas/core/indexes/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,12 @@ def __new__(
331331

332332
# extension dtype
333333
elif is_extension_array_dtype(data) or is_extension_array_dtype(dtype):
334-
data = np.asarray(data)
335334
if not (dtype is None or is_object_dtype(dtype)):
336335
# coerce to the provided dtype
337336
ea_cls = dtype.construct_array_type()
338337
data = ea_cls._from_sequence(data, dtype=dtype, copy=False)
338+
else:
339+
data = np.asarray(data, dtype=object)
339340

340341
# coerce to the object dtype
341342
data = data.astype(object)

pandas/core/nanops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def _get_values(
314314

315315
# promote if needed
316316
else:
317-
values, changed = maybe_upcast_putmask(values, mask, fill_value)
317+
values, _ = maybe_upcast_putmask(values, mask, fill_value)
318318

319319
# return a platform independent precision dtype
320320
dtype_max = dtype

pandas/core/ops/array_ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def masked_arith_op(x, y, op):
110110
with np.errstate(all="ignore"):
111111
result[mask] = op(xrav[mask], y)
112112

113-
result, changed = maybe_upcast_putmask(result, ~mask, np.nan)
113+
result, _ = maybe_upcast_putmask(result, ~mask, np.nan)
114114
result = result.reshape(x.shape) # 2D compat
115115
return result
116116

pandas/core/strings.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ def cat_core(list_of_columns: List, sep: str):
7474
"""
7575
if sep == "":
7676
# no need to interleave sep if it is empty
77-
return np.sum(list_of_columns, axis=0)
77+
arr_of_cols = np.asarray(list_of_columns, dtype=object)
78+
return np.sum(arr_of_cols, axis=0)
7879
list_with_sep = [sep] * (2 * len(list_of_columns) - 1)
7980
list_with_sep[::2] = list_of_columns
80-
return np.sum(list_with_sep, axis=0)
81+
arr_with_sep = np.asarray(list_with_sep)
82+
return np.sum(arr_with_sep, axis=0)
8183

8284

8385
def cat_safe(list_of_columns: List, sep: str):

0 commit comments

Comments
 (0)