-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: reset_index with NaN in MultiIndex #6322
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
Comments
can U give an example of your use case |
This comes out of a database query of mixed fields of dates and values. And I want to use the combination of dates as index so I can easily join the values from multiple queries. however, sometimes all dates in one column are all null. NaN in MultiIndex is already supported. That 3 lines I suggested would make it work with a level with all NaN. We can test case other functionalities. But after I made that change in my local copy, I haven't experienced other issues with all NaN MultiIndex yet. |
go ahead and do a PR (with a test case) and i'll take a look |
I can confirm I am also experiencing precisely this issue with an all-NaN level in my multi-index. Looks to me like those three lines should work if included. |
The code below returns the error:
This is the desired result:
|
@jreback I have the same issue and was playing around with a solution. The
see here. The mask variable is all
Please advise. More than happy to create a pull request and create all appropriate test cases. |
@tzinckgraf you can just do the take if sure why don't you put up a PR with a test (you can use the above example). |
closes pandas-dev#6322 Author: tzinckgraf <[email protected]> Closes pandas-dev#15466 from tzinckgraf/GH6322 and squashes the following commits: 35f97f4 [tzinckgraf] GH6322, Bug on reset_index for a MultiIndex of all NaNs
This is related to the following commit.
jreback@625ee94
When one level in the MultiIndex is all NaN, line 2819 in DataFrame.reset_index() and subfunction _maybe_cast, will have a problem, because values is empty.
2819: values = values.take(labels)
IndexError: cannot do a non-empty take from an empty axes
A possible fix is to add the following lines before 2819:
if mask.all():
values = labels * np.nan
return values
The text was updated successfully, but these errors were encountered: