Skip to content

Commit 112b6c5

Browse files
committed
move core/frame.py -> core/dtype/cast.py
1 parent 9787744 commit 112b6c5

File tree

2 files changed

+62
-47
lines changed

2 files changed

+62
-47
lines changed

pandas/core/dtypes/cast.py

Lines changed: 59 additions & 1 deletion
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

Lines changed: 3 additions & 46 deletions
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,7 +114,7 @@
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
@@ -125,15 +125,12 @@
125125
transform,
126126
)
127127
from pandas.core.arrays import Categorical, ExtensionArray
128-
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin as DatetimeLikeArray
129128
from pandas.core.arrays.sparse import SparseFrameAccessor
130129
from pandas.core.construction import extract_array
131130
from pandas.core.generic import NDFrame, _shared_docs
132131
from pandas.core.indexes import base as ibase
133132
from pandas.core.indexes.api import Index, ensure_index, ensure_index_from_sequences
134-
from pandas.core.indexes.datetimes import DatetimeIndex
135133
from pandas.core.indexes.multi import MultiIndex, maybe_droplevels
136-
from pandas.core.indexes.period import PeriodIndex
137134
from pandas.core.indexing import check_bool_indexer, convert_to_index_sliceable
138135
from pandas.core.internals import BlockManager
139136
from pandas.core.internals.construction import (
@@ -4847,46 +4844,6 @@ class max type
48474844
else:
48484845
new_obj = self.copy()
48494846

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

49354892
new_obj.index = new_index

0 commit comments

Comments
 (0)