Skip to content

Commit 584ebd2

Browse files
BUG: Allow concat to take string axis names
Adding tests for concat string axis names Fixing pep8 related spacing
1 parent d98e982 commit 584ebd2

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v0.19.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ Bug Fixes
4444

4545

4646
- Bug in ``pd.concat`` where names of the ``keys`` were not propagated to the resulting ``MultiIndex`` (:issue:`14252`)
47+
- Bug in ``pd.concat`` where ``axis`` cannot take string parameters ``rows`` or ``columns (:issue:`14369`)
4748
- Bug in ``MultiIndex.set_levels`` where illegal level values were still set after raising an error (:issue:`13754`)

pandas/tests/frame/test_combine_concat.py

+14
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,20 @@ def test_concat_named_keys(self):
347347
names=[None, None]))
348348
assert_frame_equal(concatted_unnamed, expected_unnamed)
349349

350+
def test_concat_axis_parameter(self):
351+
# GH 14369
352+
df1 = pd.DataFrame({'A': [0.1, 0.2]}, index=range(2))
353+
df2 = pd.DataFrame({'A': [0.3, 0.4]}, index=range(2))
354+
expected_row = pd.DataFrame(
355+
{'A': [0.1, 0.2, 0.3, 0.4]}, index=[0, 1, 0, 1])
356+
concatted_row = pd.concat([df1, df2], axis='rows')
357+
assert_frame_equal(concatted_row, expected_row)
358+
359+
expected_columns = pd.DataFrame(
360+
[[0.1, 0.3], [0.2, 0.4]], index=[0, 1], columns=['A', 'A'])
361+
concatted_columns = pd.concat([df1, df2], axis='columns')
362+
assert_frame_equal(concatted_columns, expected_columns)
363+
350364

351365
class TestDataFrameCombineFirst(tm.TestCase, TestData):
352366

pandas/tools/merge.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ def concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,
12831283
argument, unless it is passed, in which case the values will be
12841284
selected (see below). Any None objects will be dropped silently unless
12851285
they are all None in which case a ValueError will be raised
1286-
axis : {0, 1, ...}, default 0
1286+
axis : {0, 1, 'rows', 'columns', ...}, default 0
12871287
The axis to concatenate along
12881288
join : {'inner', 'outer'}, default 'outer'
12891289
How to handle indexes on other axis(es)
@@ -1411,6 +1411,10 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
14111411
sample = objs[0]
14121412
self.objs = objs
14131413

1414+
# Check for string axis parameter
1415+
if isinstance(axis, str):
1416+
axis = objs[0]._get_axis_number(axis)
1417+
14141418
# Need to flip BlockManager axis in the DataFrame special case
14151419
self._is_frame = isinstance(sample, DataFrame)
14161420
if self._is_frame:

0 commit comments

Comments
 (0)