Skip to content

Commit 90cddee

Browse files
committed
BUG: use Series name attributes for colnames in concat with axis=1. close #2489
1 parent 0a7ba6f commit 90cddee

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ pandas 0.10.0
235235
- DataFrame.combine_first will always result in the union of the index and
236236
columns, even if one DataFrame is length-zero (GH2525_)
237237
- Fix several DataFrame.icol/irow with duplicate indices issues (GH2228_, GH2259_)
238+
- Use Series names for column names when using concat with axis=1 (GH2489_)
238239

239240
.. _GH407: https://github.com/pydata/pandas/issues/407
240241
.. _GH821: https://github.com/pydata/pandas/issues/821
@@ -353,6 +354,7 @@ pandas 0.10.0
353354
.. _GH2525: https://github.com/pydata/pandas/issues/2525
354355
.. _GH2228: https://github.com/pydata/pandas/issues/2228
355356
.. _GH2259: https://github.com/pydata/pandas/issues/2259
357+
.. _GH2489: https://github.com/pydata/pandas/issues/2489
356358

357359

358360
pandas 0.9.1

pandas/tools/merge.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,13 @@ def _get_concat_axis(self):
11401140
if self.axis == 0:
11411141
indexes = [x.index for x in self.objs]
11421142
elif self.keys is None:
1143-
return Index(np.arange(len(self.objs)))
1143+
names = []
1144+
for x in self.objs:
1145+
if x.name is not None:
1146+
names.append(x.name)
1147+
else:
1148+
return Index(np.arange(len(self.objs)))
1149+
return Index(names)
11441150
else:
11451151
return _ensure_index(self.keys)
11461152
else:

pandas/tools/tests/test_merge.py

+12
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,18 @@ def test_concat_series_axis1(self):
14971497
expected = DataFrame(pieces, index=['A', 'B', 'C']).T
14981498
assert_frame_equal(result, expected)
14991499

1500+
# preserve series names, #2489
1501+
s = Series(randn(5), name='A')
1502+
s2 = Series(randn(5), name='B')
1503+
1504+
result = concat([s, s2], axis=1)
1505+
expected = DataFrame({'A': s, 'B': s2})
1506+
assert_frame_equal(result, expected)
1507+
1508+
s2.name = None
1509+
result = concat([s, s2], axis=1)
1510+
self.assertTrue(np.array_equal(result.columns, range(2)))
1511+
15001512
def test_concat_single_with_key(self):
15011513
df = DataFrame(np.random.randn(10, 4))
15021514

0 commit comments

Comments
 (0)