Skip to content

GH6322, Bug on reset_index for a MultiIndex of all NaNs #15466

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,4 @@ Bug Fixes
- Bug in ``Series.replace`` and ``DataFrame.replace`` which failed on empty replacement dicts (:issue:`15289`)
- Bug in ``pd.melt()`` where passing a tuple value for ``value_vars`` caused a ``TypeError`` (:issue:`15348`)
- Bug in ``.eval()`` which caused multiline evals to fail with local variables not on the first line (:issue:`15342`)
- Bug in ``.reset_index()`` which caused ``reset_index`` for a ``MultiIndex`` to fail if one part of the index was all ``NaN``'s (:issue:`6322`)
8 changes: 7 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2973,7 +2973,13 @@ def _maybe_casted_values(index, labels=None):
# if we have the labels, extract the values with a mask
if labels is not None:
mask = labels == -1
values = values.take(labels)
# 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.nan * mask).values()
else:
values = values.take(labels)

if mask.any():
values, changed = _maybe_upcast_putmask(values, mask,
np.nan)
Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,33 @@ def test_reset_index_multiindex_col(self):
['a', 'mean', 'median', 'mean']])
assert_frame_equal(rs, xp)

def test_reset_index_multiindex_nan(self):
# GH6322, testing reset_index on MultiIndexes
# when we have a nan or all nan
df = pd.DataFrame({'A' : ['a', 'b', 'c'],
'B' : [0, 1, np.nan],
'C' : np.random.rand(3)})
rs = df.set_index(['A', 'B']).reset_index()
assert_frame_equal(rs, df)

df = pd.DataFrame({'A' : [np.nan, 'b', 'c'],
'B' : [0, 1, 2],
'C' : np.random.rand(3)})
rs = df.set_index(['A', 'B']).reset_index()
assert_frame_equal(rs, df)

df = pd.DataFrame({'A' : ['a', 'b', 'c'],
'B' : [0, 1, 2],
'C' : [np.nan, 1.1, 2.2]})
rs = df.set_index(['A', 'B']).reset_index()
assert_frame_equal(rs, df)

df = pd.DataFrame({'A' : ['a', 'b', 'c'],
'B' : [np.nan, np.nan, np.nan],
'C' : np.random.rand(3)})
rs = df.set_index(['A', 'B']).reset_index()
assert_frame_equal(rs, df)

def test_reset_index_with_datetimeindex_cols(self):
# GH5818
#
Expand Down