|
4 | 4 |
|
5 | 5 | from datetime import datetime
|
6 | 6 |
|
7 |
| -from numpy import nan |
8 | 7 | import numpy as np
|
| 8 | +from numpy import nan |
9 | 9 |
|
10 |
| -from pandas.compat import lrange |
11 |
| -from pandas import DataFrame, Series, Index, Timestamp |
12 | 10 | import pandas as pd
|
13 | 11 |
|
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 |
19 | 14 |
|
20 | 15 | from pandas.tests.frame.common import TestData
|
21 | 16 |
|
| 17 | +import pandas.util.testing as tm |
| 18 | +from pandas.util.testing import (assertRaisesRegexp, |
| 19 | + assert_frame_equal, |
| 20 | + assert_series_equal) |
| 21 | + |
22 | 22 |
|
23 | 23 | class TestDataFrameConcatCommon(tm.TestCase, TestData):
|
24 | 24 |
|
@@ -324,6 +324,29 @@ def test_join_multiindex_leftright(self):
|
324 | 324 | assert_frame_equal(df2.join(df1, how='left'),
|
325 | 325 | exp[['value2', 'value1']])
|
326 | 326 |
|
| 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 | + |
327 | 350 |
|
328 | 351 | class TestDataFrameCombineFirst(tm.TestCase, TestData):
|
329 | 352 |
|
|
0 commit comments