Skip to content

Commit 18062dd

Browse files
jbrockmendelluckyvs1
authored andcommitted
REF: simplify maybe_casted_values (pandas-dev#38421)
1 parent c796aa4 commit 18062dd

File tree

2 files changed

+12
-56
lines changed

2 files changed

+12
-56
lines changed

pandas/core/dtypes/cast.py

+1-54
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,11 @@
8888
ABCSeries,
8989
)
9090
from pandas.core.dtypes.inference import is_list_like
91-
from pandas.core.dtypes.missing import (
92-
is_valid_nat_for_dtype,
93-
isna,
94-
na_value_for_dtype,
95-
notna,
96-
)
91+
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, notna
9792

9893
if TYPE_CHECKING:
9994
from pandas import Series
10095
from pandas.core.arrays import ExtensionArray
101-
from pandas.core.indexes.base import Index
10296

10397
_int8_max = np.iinfo(np.int8).max
10498
_int16_max = np.iinfo(np.int16).max
@@ -488,53 +482,6 @@ def changeit():
488482
return result, False
489483

490484

491-
def maybe_casted_values(
492-
index: "Index", codes: Optional[np.ndarray] = None
493-
) -> ArrayLike:
494-
"""
495-
Convert an index, given directly or as a pair (level, code), to a 1D array.
496-
497-
Parameters
498-
----------
499-
index : Index
500-
codes : np.ndarray[intp] or None, default None
501-
502-
Returns
503-
-------
504-
ExtensionArray or ndarray
505-
If codes is `None`, the values of `index`.
506-
If codes is passed, an array obtained by taking from `index` the indices
507-
contained in `codes`.
508-
"""
509-
510-
values = index._values
511-
if values.dtype == np.object_:
512-
values = lib.maybe_convert_objects(values)
513-
514-
# if we have the codes, extract the values with a mask
515-
if codes is not None:
516-
mask: np.ndarray = codes == -1
517-
518-
if mask.size > 0 and mask.all():
519-
# we can have situations where the whole mask is -1,
520-
# meaning there is nothing found in codes, so make all nan's
521-
522-
dtype = index.dtype
523-
fill_value = na_value_for_dtype(dtype)
524-
values = construct_1d_arraylike_from_scalar(fill_value, len(mask), dtype)
525-
526-
else:
527-
values = values.take(codes)
528-
529-
if mask.any():
530-
if isinstance(values, np.ndarray):
531-
values, _ = maybe_upcast_putmask(values, mask, np.nan)
532-
else:
533-
values[mask] = np.nan
534-
535-
return values
536-
537-
538485
def maybe_promote(dtype, fill_value=np.nan):
539486
"""
540487
Find the minimal dtype that can hold both the given dtype and fill_value.

pandas/core/frame.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
infer_dtype_from_scalar,
8484
invalidate_string_dtypes,
8585
maybe_box_datetimelike,
86-
maybe_casted_values,
8786
maybe_convert_platform,
8887
maybe_downcast_to_dtype,
8988
maybe_infer_to_datetimelike,
@@ -5021,8 +5020,18 @@ class max type
50215020
missing = self.columns.nlevels - len(name_lst)
50225021
name_lst += [col_fill] * missing
50235022
name = tuple(name_lst)
5023+
50245024
# to ndarray and maybe infer different dtype
5025-
level_values = maybe_casted_values(lev, lab)
5025+
level_values = lev._values
5026+
if level_values.dtype == np.object_:
5027+
level_values = lib.maybe_convert_objects(level_values)
5028+
5029+
if lab is not None:
5030+
# if we have the codes, extract the values with a mask
5031+
level_values = algorithms.take(
5032+
level_values, lab, allow_fill=True, fill_value=lev._na_value
5033+
)
5034+
50265035
new_obj.insert(0, name, level_values)
50275036

50285037
new_obj.index = new_index

0 commit comments

Comments
 (0)