Skip to content

Commit bb2144a

Browse files
tzinckgrafjreback
authored andcommitted
BUG: Bug on reset_index for a MultiIndex of all NaNs
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
1 parent e1d5407 commit bb2144a

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ Bug Fixes
575575
- Incorrect dtyped ``Series`` was returned by comparison methods (e.g., ``lt``, ``gt``, ...) against a constant for an empty ``DataFrame`` (:issue:`15077`)
576576
- Bug in ``Series.dt.round`` inconsistent behaviour on NAT's with different arguments (:issue:`14940`)
577577
- Bug in ``DataFrame.fillna()`` where the argument ``downcast`` was ignored when fillna value was of type ``dict`` (:issue:`15277`)
578+
- Bug in ``.reset_index()`` when an all ``NaN`` level of a ``MultiIndex`` would fail (:issue:`6322`)
578579

579580

580581

pandas/core/frame.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -2973,10 +2973,17 @@ 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)
2977-
if mask.any():
2978-
values, changed = _maybe_upcast_putmask(values, mask,
2979-
np.nan)
2976+
2977+
# we can have situations where the whole mask is -1,
2978+
# meaning there is nothing found in labels, so make all nan's
2979+
if mask.all():
2980+
values = np.empty(len(mask))
2981+
values.fill(np.nan)
2982+
else:
2983+
values = values.take(labels)
2984+
if mask.any():
2985+
values, changed = _maybe_upcast_putmask(values, mask,
2986+
np.nan)
29802987
return values
29812988

29822989
new_index = _default_index(len(new_obj))

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)