Skip to content

Commit 7f82be9

Browse files
committed
BUG: Fix MultiIndex names handling in pd.concat
This is a fix attempt for issue #15787. The discrepancy between definition and corresponding implementation of so-called non-none names in function _get_consensus_names leads to this bug.
1 parent f35209e commit 7f82be9

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,7 @@ Indexing
11701170
- Bug in creating a ``MultiIndex`` with tuples and not passing a list of names; this will now raise ``ValueError`` (:issue:`15110`)
11711171
- Bug in the HTML display with with a ``MultiIndex`` and truncation (:issue:`14882`)
11721172
- Bug in the display of ``.info()`` where a qualifier (+) would always be displayed with a ``MultiIndex`` that contains only non-strings (:issue:`15245`)
1173+
- 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`)
11731174

11741175
I/O
11751176
^^^

pandas/indexes/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def _get_consensus_names(indexes):
107107
# find the non-none names, need to tupleify to make
108108
# the set hashable, then reverse on return
109109
consensus_names = set([tuple(i.names) for i in indexes
110-
if all(n is not None for n in i.names)])
110+
if any(n is not None for n in i.names)])
111111
if len(consensus_names) == 1:
112112
return list(list(consensus_names)[0])
113113
return [None] * indexes[0].nlevels

pandas/tests/tools/test_concat.py

+18
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,24 @@ def test_concat_multiindex_with_tz(self):
10481048
result = concat([df, df])
10491049
tm.assert_frame_equal(result, expected)
10501050

1051+
def test_concat_multiindex_with_none_in_index_names(self):
1052+
# GH 15787
1053+
from pandas.indexes.frozen import FrozenList
1054+
1055+
index = pd.MultiIndex.from_product([[1], range(5)],
1056+
names=['level1', None])
1057+
df = pd.DataFrame({'col': range(5)}, index=index)
1058+
1059+
result = concat([df, df], keys=[1, 2], names=['level2'])
1060+
result = result.index.names
1061+
expected = FrozenList(['level2', 'level1', None])
1062+
self.assertEqual(result, expected)
1063+
1064+
result = concat([df, df[:2]], keys=[1, 2], names=['level2'])
1065+
result = result.index.names
1066+
expected = FrozenList(['level2', 'level1', None])
1067+
self.assertEqual(result, expected)
1068+
10511069
def test_concat_keys_and_levels(self):
10521070
df = DataFrame(np.random.randn(1, 3))
10531071
df2 = DataFrame(np.random.randn(1, 4))

0 commit comments

Comments
 (0)