Skip to content

Commit 35f97f4

Browse files
committed
GH6322, Bug on reset_index for a MultiIndex of all NaNs
The reset_index function fails for a MultiIndex if at least one column of the index is all NaN.
1 parent 0b4fdf9 commit 35f97f4

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,4 @@ Bug Fixes
591591
- Bug in ``Series.replace`` and ``DataFrame.replace`` which failed on empty replacement dicts (:issue:`15289`)
592592
- Bug in ``pd.melt()`` where passing a tuple value for ``value_vars`` caused a ``TypeError`` (:issue:`15348`)
593593
- Bug in ``.eval()`` which caused multiline evals to fail with local variables not on the first line (:issue:`15342`)
594+
- 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`)

pandas/core/frame.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2973,7 +2973,13 @@ def _maybe_casted_values(index, labels=None):
29732973
# if we have the labels, extract the values with a mask
29742974
if labels is not None:
29752975
mask = labels == -1
2976-
values = values.take(labels)
2976+
# we can have situations where the whole mask is -1,
2977+
# meaning there is nothing found in labels, so make all nan's
2978+
if mask.all():
2979+
values = (np.nan * mask).values()
2980+
else:
2981+
values = values.take(labels)
2982+
29772983
if mask.any():
29782984
values, changed = _maybe_upcast_putmask(values, mask,
29792985
np.nan)

pandas/tests/frame/test_alter_axes.py

+27
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,33 @@ def test_reset_index_multiindex_col(self):
624624
['a', 'mean', 'median', 'mean']])
625625
assert_frame_equal(rs, xp)
626626

627+
def test_reset_index_multiindex_nan(self):
628+
# GH6322, testing reset_index on MultiIndexes
629+
# when we have a nan or all nan
630+
df = pd.DataFrame({'A' : ['a', 'b', 'c'],
631+
'B' : [0, 1, np.nan],
632+
'C' : np.random.rand(3)})
633+
rs = df.set_index(['A', 'B']).reset_index()
634+
assert_frame_equal(rs, df)
635+
636+
df = pd.DataFrame({'A' : [np.nan, 'b', 'c'],
637+
'B' : [0, 1, 2],
638+
'C' : np.random.rand(3)})
639+
rs = df.set_index(['A', 'B']).reset_index()
640+
assert_frame_equal(rs, df)
641+
642+
df = pd.DataFrame({'A' : ['a', 'b', 'c'],
643+
'B' : [0, 1, 2],
644+
'C' : [np.nan, 1.1, 2.2]})
645+
rs = df.set_index(['A', 'B']).reset_index()
646+
assert_frame_equal(rs, df)
647+
648+
df = pd.DataFrame({'A' : ['a', 'b', 'c'],
649+
'B' : [np.nan, np.nan, np.nan],
650+
'C' : np.random.rand(3)})
651+
rs = df.set_index(['A', 'B']).reset_index()
652+
assert_frame_equal(rs, df)
653+
627654
def test_reset_index_with_datetimeindex_cols(self):
628655
# GH5818
629656
#

0 commit comments

Comments
 (0)