Skip to content

Commit ed6ad03

Browse files
committed
move core/frame.py -> core/dtype/cast.py
1 parent 653f694 commit ed6ad03

File tree

2 files changed

+62
-47
lines changed

2 files changed

+62
-47
lines changed

pandas/core/dtypes/cast.py

+59-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@
7373
ABCSeries,
7474
)
7575
from pandas.core.dtypes.inference import is_list_like
76-
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, notna
76+
from pandas.core.dtypes.missing import (
77+
is_valid_nat_for_dtype,
78+
isna,
79+
na_value_for_dtype,
80+
notna,
81+
)
7782

7883
if TYPE_CHECKING:
7984
from pandas import Series
@@ -439,6 +444,59 @@ def changeit():
439444
return result, False
440445

441446

447+
def maybe_casted_values(index, codes=None):
448+
"""
449+
Convert an index, given directly or as a pair (level, code), to a 1D array.
450+
Parameters
451+
----------
452+
index : Index
453+
codes : sequence of integers (optional)
454+
Returns
455+
-------
456+
ExtensionArray or ndarray
457+
If codes is `None`, the values of `index`.
458+
If codes is passed, an array obtained by taking from `index` the indices
459+
contained in `codes`.
460+
"""
461+
462+
values = index._values
463+
if not isinstance(index, (ABCPeriodIndex, ABCDatetimeIndex)):
464+
if values.dtype == np.object_:
465+
values = lib.maybe_convert_objects(values)
466+
467+
# if we have the codes, extract the values with a mask
468+
if codes is not None:
469+
mask = codes == -1
470+
471+
# we can have situations where the whole mask is -1,
472+
# meaning there is nothing found in codes, so make all nan's
473+
if mask.size > 0 and mask.all():
474+
dtype = index.dtype
475+
fill_value = na_value_for_dtype(dtype)
476+
values = construct_1d_arraylike_from_scalar(fill_value, len(mask), dtype)
477+
else:
478+
values = values.take(codes)
479+
480+
# TODO(https://github.com/pandas-dev/pandas/issues/24206)
481+
# Push this into maybe_upcast_putmask?
482+
# We can't pass EAs there right now. Looks a bit
483+
# complicated.
484+
# So we unbox the ndarray_values, op, re-box.
485+
values_type = type(values)
486+
values_dtype = values.dtype
487+
488+
if issubclass(values_type, ABCDatetimeArray):
489+
values = values._data # TODO: can we de-kludge yet?
490+
491+
if mask.any():
492+
values, _ = maybe_upcast_putmask(values, mask, np.nan)
493+
494+
if issubclass(values_type, ABCDatetimeArray):
495+
values = values_type(values, dtype=values_dtype)
496+
497+
return values
498+
499+
442500
def maybe_promote(dtype, fill_value=np.nan):
443501
"""
444502
Find the minimal dtype that can hold both the given dtype and fill_value.

pandas/core/frame.py

+3-46
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@
8383
infer_dtype_from_scalar,
8484
invalidate_string_dtypes,
8585
maybe_cast_to_datetime,
86+
maybe_casted_values,
8687
maybe_convert_platform,
8788
maybe_downcast_to_dtype,
8889
maybe_infer_to_datetimelike,
8990
maybe_upcast,
90-
maybe_upcast_putmask,
9191
validate_numeric_casting,
9292
)
9393
from pandas.core.dtypes.common import (
@@ -114,21 +114,18 @@
114114
needs_i8_conversion,
115115
pandas_dtype,
116116
)
117-
from pandas.core.dtypes.missing import isna, na_value_for_dtype, notna
117+
from pandas.core.dtypes.missing import isna, notna
118118

119119
from pandas.core import algorithms, common as com, nanops, ops
120120
from pandas.core.accessor import CachedAccessor
121121
from pandas.core.aggregation import reconstruct_func, relabel_result, transform
122122
from pandas.core.arrays import Categorical, ExtensionArray
123-
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin as DatetimeLikeArray
124123
from pandas.core.arrays.sparse import SparseFrameAccessor
125124
from pandas.core.construction import extract_array
126125
from pandas.core.generic import NDFrame, _shared_docs
127126
from pandas.core.indexes import base as ibase
128127
from pandas.core.indexes.api import Index, ensure_index, ensure_index_from_sequences
129-
from pandas.core.indexes.datetimes import DatetimeIndex
130128
from pandas.core.indexes.multi import MultiIndex, maybe_droplevels
131-
from pandas.core.indexes.period import PeriodIndex
132129
from pandas.core.indexing import check_bool_indexer, convert_to_index_sliceable
133130
from pandas.core.internals import BlockManager
134131
from pandas.core.internals.construction import (
@@ -4842,46 +4839,6 @@ class max type
48424839
else:
48434840
new_obj = self.copy()
48444841

4845-
def _maybe_casted_values(index, labels=None):
4846-
values = index._values
4847-
if not isinstance(index, (PeriodIndex, DatetimeIndex)):
4848-
if values.dtype == np.object_:
4849-
values = lib.maybe_convert_objects(values)
4850-
4851-
# if we have the labels, extract the values with a mask
4852-
if labels is not None:
4853-
mask = labels == -1
4854-
4855-
# we can have situations where the whole mask is -1,
4856-
# meaning there is nothing found in labels, so make all nan's
4857-
if mask.size > 0 and mask.all():
4858-
dtype = index.dtype
4859-
fill_value = na_value_for_dtype(dtype)
4860-
values = construct_1d_arraylike_from_scalar(
4861-
fill_value, len(mask), dtype
4862-
)
4863-
else:
4864-
values = values.take(labels)
4865-
4866-
# TODO(https://github.com/pandas-dev/pandas/issues/24206)
4867-
# Push this into maybe_upcast_putmask?
4868-
# We can't pass EAs there right now. Looks a bit
4869-
# complicated.
4870-
# So we unbox the ndarray_values, op, re-box.
4871-
values_type = type(values)
4872-
values_dtype = values.dtype
4873-
4874-
if issubclass(values_type, DatetimeLikeArray):
4875-
values = values._data # TODO: can we de-kludge yet?
4876-
4877-
if mask.any():
4878-
values, _ = maybe_upcast_putmask(values, mask, np.nan)
4879-
4880-
if issubclass(values_type, DatetimeLikeArray):
4881-
values = values_type(values, dtype=values_dtype)
4882-
4883-
return values
4884-
48854842
new_index = ibase.default_index(len(new_obj))
48864843
if level is not None:
48874844
if not isinstance(level, (tuple, list)):
@@ -4924,7 +4881,7 @@ def _maybe_casted_values(index, labels=None):
49244881
name_lst += [col_fill] * missing
49254882
name = tuple(name_lst)
49264883
# to ndarray and maybe infer different dtype
4927-
level_values = _maybe_casted_values(lev, lab)
4884+
level_values = maybe_casted_values(lev, lab)
49284885
new_obj.insert(0, name, level_values)
49294886

49304887
new_obj.index = new_index

0 commit comments

Comments
 (0)