diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 700bf4ddc3a37..b23a42c6ac2c7 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -933,6 +933,7 @@ Reshaping ^^^^^^^^^ - Bug in :func:`pandas.concat` when joining resampled DataFrames with timezone aware index (:issue:`13783`) +- Bug in :func:`pandas.concat` when joining only `Series` the `names` argument of `concat` is no longer ignored (:issue:`23490`) - Bug in :meth:`Series.combine_first` with ``datetime64[ns, tz]`` dtype which would return tz-naive result (:issue:`21469`) - Bug in :meth:`Series.where` and :meth:`DataFrame.where` with ``datetime64[ns, tz]`` dtype (:issue:`21546`) - Bug in :meth:`Series.mask` and :meth:`DataFrame.mask` with ``list`` conditionals (:issue:`21891`) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index 1c602a0af1ec1..bc869f76dcfa2 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -500,7 +500,7 @@ def _get_concat_axis(self): else: return ibase.default_index(len(self.objs)) else: - return ensure_index(self.keys) + return ensure_index(self.keys).set_names(self.names) else: indexes = [x._data.axes[self.axis] for x in self.objs] diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 2aaa04d571e69..59fbd356008a7 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -1618,6 +1618,23 @@ def test_concat_series_axis1(self, sort=sort): expected = DataFrame({'A': s, 'B': s2}) assert_frame_equal(result, expected) + def test_concat_series_axis1_names_applied(self): + # ensure names argument is not ignored on axis=1, #23490 + s = Series([1, 2, 3]) + s2 = Series([4, 5, 6]) + result = concat([s, s2], axis=1, keys=['a', 'b'], names=['A']) + expected = DataFrame([[1, 4], [2, 5], [3, 6]], + columns=pd.Index(['a', 'b'], name='A')) + assert_frame_equal(result, expected) + + result = concat([s, s2], axis=1, keys=[('a', 1), ('b', 2)], + names=['A', 'B']) + expected = DataFrame([[1, 4], [2, 5], [3, 6]], + columns=MultiIndex.from_tuples([('a', 1), + ('b', 2)], + names=['A', 'B'])) + assert_frame_equal(result, expected) + def test_concat_single_with_key(self): df = DataFrame(np.random.randn(10, 4))