From 2349940268b1aad055fe7a3039ee6dec2c16bb73 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 11 Dec 2020 18:13:20 -0800 Subject: [PATCH] REF: simplify maybe_casted_values --- pandas/core/dtypes/cast.py | 55 +------------------------------------- pandas/core/frame.py | 13 +++++++-- 2 files changed, 12 insertions(+), 56 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index fafa1f51c823e..fcd4a24140c9f 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -88,17 +88,11 @@ ABCSeries, ) from pandas.core.dtypes.inference import is_list_like -from pandas.core.dtypes.missing import ( - is_valid_nat_for_dtype, - isna, - na_value_for_dtype, - notna, -) +from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, notna if TYPE_CHECKING: from pandas import Series from pandas.core.arrays import ExtensionArray - from pandas.core.indexes.base import Index _int8_max = np.iinfo(np.int8).max _int16_max = np.iinfo(np.int16).max @@ -488,53 +482,6 @@ def changeit(): return result, False -def maybe_casted_values( - index: "Index", codes: Optional[np.ndarray] = None -) -> ArrayLike: - """ - Convert an index, given directly or as a pair (level, code), to a 1D array. - - Parameters - ---------- - index : Index - codes : np.ndarray[intp] or None, default None - - Returns - ------- - ExtensionArray or ndarray - If codes is `None`, the values of `index`. - If codes is passed, an array obtained by taking from `index` the indices - contained in `codes`. - """ - - values = index._values - if values.dtype == np.object_: - values = lib.maybe_convert_objects(values) - - # if we have the codes, extract the values with a mask - if codes is not None: - mask: np.ndarray = codes == -1 - - if mask.size > 0 and mask.all(): - # we can have situations where the whole mask is -1, - # meaning there is nothing found in codes, so make all nan's - - dtype = index.dtype - fill_value = na_value_for_dtype(dtype) - values = construct_1d_arraylike_from_scalar(fill_value, len(mask), dtype) - - else: - values = values.take(codes) - - if mask.any(): - if isinstance(values, np.ndarray): - values, _ = maybe_upcast_putmask(values, mask, np.nan) - else: - values[mask] = np.nan - - return values - - def maybe_promote(dtype, fill_value=np.nan): """ Find the minimal dtype that can hold both the given dtype and fill_value. diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 66399f2b9a5e4..bed89bbf0dcca 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -83,7 +83,6 @@ infer_dtype_from_scalar, invalidate_string_dtypes, maybe_box_datetimelike, - maybe_casted_values, maybe_convert_platform, maybe_downcast_to_dtype, maybe_infer_to_datetimelike, @@ -5021,8 +5020,18 @@ class max type missing = self.columns.nlevels - len(name_lst) name_lst += [col_fill] * missing name = tuple(name_lst) + # to ndarray and maybe infer different dtype - level_values = maybe_casted_values(lev, lab) + level_values = lev._values + if level_values.dtype == np.object_: + level_values = lib.maybe_convert_objects(level_values) + + if lab is not None: + # if we have the codes, extract the values with a mask + level_values = algorithms.take( + level_values, lab, allow_fill=True, fill_value=lev._na_value + ) + new_obj.insert(0, name, level_values) new_obj.index = new_index