Skip to content

Commit c992fd7

Browse files
ms7463gfyoung
authored andcommitted
BUG - pd.concat with all Series on axis=1 ignores the names argument (#23499)
Closes gh-23490.
1 parent 6bf6cd2 commit c992fd7

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,7 @@ Reshaping
13161316
^^^^^^^^^
13171317

13181318
- Bug in :func:`pandas.concat` when joining resampled DataFrames with timezone aware index (:issue:`13783`)
1319+
- Bug in :func:`pandas.concat` when joining only `Series` the `names` argument of `concat` is no longer ignored (:issue:`23490`)
13191320
- Bug in :meth:`Series.combine_first` with ``datetime64[ns, tz]`` dtype which would return tz-naive result (:issue:`21469`)
13201321
- Bug in :meth:`Series.where` and :meth:`DataFrame.where` with ``datetime64[ns, tz]`` dtype (:issue:`21546`)
13211322
- Bug in :meth:`DataFrame.where` with an empty DataFrame and empty ``cond`` having non-bool dtype (:issue:`21947`)

pandas/core/reshape/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ def _get_concat_axis(self):
502502
else:
503503
return ibase.default_index(len(self.objs))
504504
else:
505-
return ensure_index(self.keys)
505+
return ensure_index(self.keys).set_names(self.names)
506506
else:
507507
indexes = [x._data.axes[self.axis] for x in self.objs]
508508

pandas/tests/reshape/test_concat.py

+17
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,23 @@ def test_concat_series_axis1(self, sort=sort):
16181618
expected = DataFrame({'A': s, 'B': s2})
16191619
assert_frame_equal(result, expected)
16201620

1621+
def test_concat_series_axis1_names_applied(self):
1622+
# ensure names argument is not ignored on axis=1, #23490
1623+
s = Series([1, 2, 3])
1624+
s2 = Series([4, 5, 6])
1625+
result = concat([s, s2], axis=1, keys=['a', 'b'], names=['A'])
1626+
expected = DataFrame([[1, 4], [2, 5], [3, 6]],
1627+
columns=pd.Index(['a', 'b'], name='A'))
1628+
assert_frame_equal(result, expected)
1629+
1630+
result = concat([s, s2], axis=1, keys=[('a', 1), ('b', 2)],
1631+
names=['A', 'B'])
1632+
expected = DataFrame([[1, 4], [2, 5], [3, 6]],
1633+
columns=MultiIndex.from_tuples([('a', 1),
1634+
('b', 2)],
1635+
names=['A', 'B']))
1636+
assert_frame_equal(result, expected)
1637+
16211638
def test_concat_single_with_key(self):
16221639
df = DataFrame(np.random.randn(10, 4))
16231640

0 commit comments

Comments
 (0)