Skip to content

Commit 8720013

Browse files
Ben KandelPiotr Chromiec
Ben Kandel
authored and
Piotr Chromiec
committed
BUG: Fix concat key name
closes pandas-dev#14252 Fixes a bug where `pd.concat` didn't propagate the names of keys used to create a hierarchical index. Author: Ben Kandel <[email protected]> Closes pandas-dev#14292 from bkandel/fix_concat_key_name and squashes the following commits: cdc76f6 [Ben Kandel] take out _ensure_index 4a301f8 [Ben Kandel] put back in Index coercion d8e2c17 [Ben Kandel] remove coercion to Index before _ensure_index 44932cc [Ben Kandel] changed whatsnew entry to 0.19.1 c51df19 [Ben Kandel] _ensure_index b54b081 [Ben Kandel] simplified logic 3256119 [Ben Kandel] typo 789ecd4 [Ben Kandel] use _ensure_index 350e724 [Ben Kandel] simplified logic 9615a69 [Ben Kandel] extra tests 5c0108b [Ben Kandel] comments dd3c4cc [Ben Kandel] comments 5cd8392 [Ben Kandel] added test for names bc5f1fb [Ben Kandel] cleanup ef6db68 [Ben Kandel] BUG: Propagate key names in concat.
1 parent 6b5e1f6 commit 8720013

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

doc/source/whatsnew/v0.19.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ Performance Improvements
3030

3131
Bug Fixes
3232
~~~~~~~~~
33+
- Bug in ``concat`` where names of keys were not propagated to the resulting MultiIndex (:issue:`14252`)

pandas/tests/frame/test_combine_concat.py

+31-8
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
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)
17-
18-
import pandas.util.testing as tm
12+
from pandas import DataFrame, Index, Series, Timestamp
13+
from pandas.compat import lrange
1914

2015
from pandas.tests.frame.common import TestData
2116

17+
import pandas.util.testing as tm
18+
from pandas.util.testing import (assertRaisesRegexp,
19+
assert_frame_equal,
20+
assert_series_equal)
21+
2222

2323
class TestDataFrameConcatCommon(tm.TestCase, TestData):
2424

@@ -324,6 +324,29 @@ def test_join_multiindex_leftright(self):
324324
assert_frame_equal(df2.join(df1, how='left'),
325325
exp[['value2', 'value1']])
326326

327+
def test_concat_named_keys(self):
328+
# GH 14252
329+
df = pd.DataFrame({'foo': [1, 2], 'bar': [0.1, 0.2]})
330+
index = Index(['a', 'b'], name='baz')
331+
concatted_named_from_keys = pd.concat([df, df], keys=index)
332+
expected_named = pd.DataFrame(
333+
{'foo': [1, 2, 1, 2], 'bar': [0.1, 0.2, 0.1, 0.2]},
334+
index=pd.MultiIndex.from_product((['a', 'b'], [0, 1]),
335+
names=['baz', None]))
336+
assert_frame_equal(concatted_named_from_keys, expected_named)
337+
338+
index_no_name = Index(['a', 'b'], name=None)
339+
concatted_named_from_names = pd.concat(
340+
[df, df], keys=index_no_name, names=['baz'])
341+
assert_frame_equal(concatted_named_from_names, expected_named)
342+
343+
concatted_unnamed = pd.concat([df, df], keys=index_no_name)
344+
expected_unnamed = pd.DataFrame(
345+
{'foo': [1, 2, 1, 2], 'bar': [0.1, 0.2, 0.1, 0.2]},
346+
index=pd.MultiIndex.from_product((['a', 'b'], [0, 1]),
347+
names=[None, None]))
348+
assert_frame_equal(concatted_unnamed, expected_unnamed)
349+
327350

328351
class TestDataFrameCombineFirst(tm.TestCase, TestData):
329352

pandas/tools/merge.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,9 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
13691369
clean_keys.append(k)
13701370
clean_objs.append(v)
13711371
objs = clean_objs
1372-
keys = clean_keys
1372+
name = getattr(keys, 'name', None)
1373+
keys = Index(clean_keys)
1374+
keys.name = name
13731375

13741376
if len(objs) == 0:
13751377
raise ValueError('All objects passed were None')
@@ -1454,7 +1456,7 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
14541456
self.axis = axis
14551457
self.join_axes = join_axes
14561458
self.keys = keys
1457-
self.names = names
1459+
self.names = names or getattr(keys, 'names', None)
14581460
self.levels = levels
14591461

14601462
self.ignore_index = ignore_index

0 commit comments

Comments
 (0)