Skip to content

BUG: Fix MultiIndex names handling in pd.concat #15955

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 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -1170,6 +1170,7 @@ Indexing
- Bug in creating a ``MultiIndex`` with tuples and not passing a list of names; this will now raise ``ValueError`` (:issue:`15110`)
- Bug in the HTML display with with a ``MultiIndex`` and truncation (:issue:`14882`)
- Bug in the display of ``.info()`` where a qualifier (+) would always be displayed with a ``MultiIndex`` that contains only non-strings (:issue:`15245`)
- Bug in ``pd.concat()`` where the names of ``MultiIndex`` of resulting ``DataFrame`` are not handled correctly when ``None`` is presented in the names of ``MultiIndex`` of input ``DataFrame`` (:issue:`15787`)

I/O
^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _get_consensus_names(indexes):
# find the non-none names, need to tupleify to make
# the set hashable, then reverse on return
consensus_names = set([tuple(i.names) for i in indexes
if all(n is not None for n in i.names)])
if any(n is not None for n in i.names)])
if len(consensus_names) == 1:
return list(list(consensus_names)[0])
return [None] * indexes[0].nlevels
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/tools/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,24 @@ def test_concat_multiindex_with_tz(self):
result = concat([df, df])
tm.assert_frame_equal(result, expected)

def test_concat_multiindex_with_none_in_index_names(self):
# GH 15787
from pandas.indexes.frozen import FrozenList

index = pd.MultiIndex.from_product([[1], range(5)],
names=['level1', None])
df = pd.DataFrame({'col': range(5)}, index=index)

result = concat([df, df], keys=[1, 2], names=['level2'])
result = result.index.names
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather you actually construct the expected DF here (and use assert_frame_equal

expected = FrozenList(['level2', 'level1', None])
self.assertEqual(result, expected)

result = concat([df, df[:2]], keys=[1, 2], names=['level2'])
result = result.index.names
expected = FrozenList(['level2', 'level1', None])
self.assertEqual(result, expected)

def test_concat_keys_and_levels(self):
df = DataFrame(np.random.randn(1, 3))
df2 = DataFrame(np.random.randn(1, 4))
Expand Down