-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Wrong dtype when resetting a multiindex with missing values. (#1… #27370
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
c41b85f
4d2b2e8
cbf7499
07f8161
865a6eb
94f3a28
2af935a
425acee
9e56281
7569ba8
0cf50d0
e2288a1
f1d110a
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 |
---|---|---|
|
@@ -1391,3 +1391,41 @@ def maybe_cast_to_integer_array(arr, dtype, copy=False): | |
|
||
if is_integer_dtype(dtype) and (is_float_dtype(arr) or is_object_dtype(arr)): | ||
raise ValueError("Trying to coerce float values to integers") | ||
|
||
|
||
def maybe_casted_values(index, codes=None): | ||
""" | ||
Convert an index, given directly or as a pair (level, codes), to a 1D array. | ||
|
||
Parameters | ||
---------- | ||
index : Index | ||
codes : sequence of integers (optional) | ||
|
||
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 labels, extract the values with a mask | ||
if codes is not None: | ||
if isinstance(values, np.ndarray): | ||
mask = codes == -1 | ||
# we can have situations where the whole mask is -1, | ||
# meaning there is nothing found in labels, so make all nan's | ||
if mask.all(): | ||
values = np.empty(len(mask), dtype=values.dtype) | ||
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. Is it possible to get here with an extension dtype? Like 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. I didn't have time to really get into it, but after a quick look I would say that it's possible. I'll have to do something about it when i come back. |
||
values.fill(np.nan) | ||
else: | ||
values = values.take(codes) | ||
if mask.any(): | ||
values, _ = maybe_upcast_putmask(values, mask, np.nan) | ||
else: | ||
values = values.take(codes, allow_fill=True) | ||
return values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think mark this as private with a leading underscore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure ? It's not really private since it's used in frame.py, and @jreback already asked me to remove the underscore when moving it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe wait to hear from @jreback but
.core.*
is supposed to be private, but people still use methods from there. We plan to deprecate it, but until then it's probably best to prefix methods with underscores.