Skip to content

Commit ef6db68

Browse files
author
Ben Kandel
committed
BUG: Propagate key names in concat.
Fixes a bug where `pd.concat` didn't propagate the names of keys used to create a hierarchical index.
1 parent daba8e5 commit ef6db68

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1563,3 +1563,4 @@ Bug Fixes
15631563
- ``PeridIndex`` can now accept ``list`` and ``array`` which contains ``pd.NaT`` (:issue:`13430`)
15641564
- Bug in ``df.groupby`` where ``.median()`` returns arbitrary values if grouped dataframe contains empty bins (:issue:`13629`)
15651565
- Bug in ``Index.copy()`` where ``name`` parameter was ignored (:issue:`14302`)
1566+
- Bug in ``concat`` where names of keys were not propagated to the resulting MultiIndex (:issue:`14252`)

pandas/tests/frame/test_combine_concat.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44

55
from datetime import datetime
66

7-
from numpy import nan
87
import numpy as np
8+
from numpy import nan
99

10-
from pandas.compat import lrange
11-
from pandas import DataFrame, Series, Index, Timestamp
1210
import pandas as pd
1311

14-
from pandas.util.testing import (assert_series_equal,
15-
assert_frame_equal,
16-
assertRaisesRegexp)
12+
from pandas import DataFrame, Index, Series, Timestamp
13+
from pandas.compat import lrange
1714

18-
import pandas.util.testing as tm
15+
from pandas.core.base import FrozenList
1916

2017
from pandas.tests.frame.common import TestData
2118

19+
import pandas.util.testing as tm
20+
from pandas.util.testing import (assertRaisesRegexp,
21+
assert_equal,
22+
assert_frame_equal,
23+
assert_series_equal)
24+
2225

2326
class TestDataFrameConcatCommon(tm.TestCase, TestData):
2427

@@ -324,6 +327,14 @@ def test_join_multiindex_leftright(self):
324327
assert_frame_equal(df2.join(df1, how='left'),
325328
exp[['value2', 'value1']])
326329

330+
def test_concat_named_keys(self):
331+
# GH 14252
332+
df = DataFrame({'foo': [1, 2, 3, 4],
333+
'bar': [0.1, 0.2, 0.3, 0.4]})
334+
index = Index(['a', 'b'], name='baz')
335+
concatted = pd.concat([df, df], keys=index)
336+
assert_equal(concatted.index.names, FrozenList(['baz', None]))
337+
327338

328339
class TestDataFrameCombineFirst(tm.TestCase, TestData):
329340

pandas/tools/merge.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1361,15 +1361,18 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
13611361
objs = [obj for obj in objs if obj is not None]
13621362
else:
13631363
# #1649
1364-
clean_keys = []
1364+
clean_keys_list = []
13651365
clean_objs = []
13661366
for k, v in zip(keys, objs):
13671367
if v is None:
13681368
continue
1369-
clean_keys.append(k)
1369+
clean_keys_list.append(k)
13701370
clean_objs.append(v)
13711371
objs = clean_objs
1372-
keys = clean_keys
1372+
clean_keys_index = Index(clean_keys_list)
1373+
if isinstance(keys, Index):
1374+
clean_keys_index.name = keys.name
1375+
keys = clean_keys_index
13731376

13741377
if len(objs) == 0:
13751378
raise ValueError('All objects passed were None')
@@ -1454,7 +1457,10 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
14541457
self.axis = axis
14551458
self.join_axes = join_axes
14561459
self.keys = keys
1457-
self.names = names
1460+
if hasattr(keys, 'names'):
1461+
self.names = names or keys.names
1462+
else:
1463+
self.names = names
14581464
self.levels = levels
14591465

14601466
self.ignore_index = ignore_index

0 commit comments

Comments
 (0)