-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CLN: move maybe_casted_values from pandas/core/frame.py to pandas/core/dtype/cast.py #36985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
030b90b
fc28afa
3cce705
87af5e2
340af7a
2b6e15f
b5cd73c
20c2620
d658153
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,12 @@ | |
ABCSeries, | ||
) | ||
from pandas.core.dtypes.inference import is_list_like | ||
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, notna | ||
from pandas.core.dtypes.missing import ( | ||
is_valid_nat_for_dtype, | ||
isna, | ||
na_value_for_dtype, | ||
notna, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from pandas import Series | ||
|
@@ -439,6 +444,65 @@ def changeit(): | |
return result, False | ||
|
||
|
||
def maybe_casted_values(index, codes=None): | ||
""" | ||
Convert an index, given directly or as a pair (level, code), to a 1D array. | ||
|
||
Parameters | ||
---------- | ||
index : Index | ||
codes : sequence of integers (optional) | ||
|
||
Returns | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
------- | ||
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 not isinstance(index, (ABCPeriodIndex, ABCDatetimeIndex)): | ||
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 = codes == -1 | ||
|
||
# we can have situations where the whole mask is -1, | ||
# meaning there is nothing found in codes, so make all nan's | ||
if mask.size > 0 and mask.all(): | ||
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) | ||
|
||
# TODO(https://github.com/pandas-dev/pandas/issues/24206) | ||
# Push this into maybe_upcast_putmask? | ||
# We can't pass EAs there right now. Looks a bit | ||
# complicated. | ||
# So we unbox the ndarray_values, op, re-box. | ||
values_type = type(values) | ||
values_dtype = values.dtype | ||
|
||
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure this is acceptable - will change if not. The issue is we can't just import at the top of the file because of a circular import There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok fine with cleaning this up after, this routine definitely needs it (and also possibly is duplicative of things in this file anyways) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
tried it and it broke some tests. I'll investigate in the follow-on |
||
|
||
if isinstance(values, DatetimeLikeArrayMixin): | ||
values = values._data # TODO: can we de-kludge yet? | ||
|
||
if mask.any(): | ||
values, _ = maybe_upcast_putmask(values, mask, np.nan) | ||
|
||
if issubclass(values_type, DatetimeLikeArrayMixin): | ||
values = values_type( | ||
values, dtype=values_dtype | ||
) # type: ignore[call-arg] | ||
|
||
return values | ||
|
||
|
||
def maybe_promote(dtype, fill_value=np.nan): | ||
""" | ||
Find the minimal dtype that can hold both the given dtype and fill_value. | ||
|
Uh oh!
There was an error while loading. Please reload this page.