Skip to content

Commit 2683e81

Browse files
[Backport ##14416] BUG: Concat with axis rows
closes #14369 Author: Brandon M. Burroughs <[email protected]> Closes #14416 from brandonmburroughs/concat_with_axis_rows and squashes the following commits: 9aa260d [Brandon M. Burroughs] Changing axis handling to depend on object passed 49442be [Brandon M. Burroughs] Using dataframe _get_axis_number instance method 64702fb [Brandon M. Burroughs] Updating documentation for concat fdd5260 [Brandon M. Burroughs] Removing duplicate expected dfs 3f08b07 [Brandon M. Burroughs] Adding concat tests for axis 0, 1, and 'index' cf3f998 [Brandon M. Burroughs] Adding ValueError test for concat Series axis 'columns' a6694b9 [Brandon M. Burroughs] Updating documentation 584ebd2 [Brandon M. Burroughs] BUG: Allow concat to take string axis names (cherry picked from commit fd3be00)
1 parent 6a3755b commit 2683e81

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

doc/source/whatsnew/v0.19.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ 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`)
4849
- Bug in ``DataFrame.to_json`` where ``lines=True`` and a value contained a ``}`` character (:issue:`14391`)
4950
- Bug in ``df.groupby`` causing an ``AttributeError`` when grouping a single index frame by a column and the index level (:issue`14327`)

pandas/tests/frame/test_combine_concat.py

+59
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,65 @@ 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+
355+
# Index/row/0 DataFrame
356+
expected_index = pd.DataFrame(
357+
{'A': [0.1, 0.2, 0.3, 0.4]}, index=[0, 1, 0, 1])
358+
359+
concatted_index = pd.concat([df1, df2], axis='index')
360+
assert_frame_equal(concatted_index, expected_index)
361+
362+
concatted_row = pd.concat([df1, df2], axis='rows')
363+
assert_frame_equal(concatted_row, expected_index)
364+
365+
concatted_0 = pd.concat([df1, df2], axis=0)
366+
assert_frame_equal(concatted_0, expected_index)
367+
368+
# Columns/1 DataFrame
369+
expected_columns = pd.DataFrame(
370+
[[0.1, 0.3], [0.2, 0.4]], index=[0, 1], columns=['A', 'A'])
371+
372+
concatted_columns = pd.concat([df1, df2], axis='columns')
373+
assert_frame_equal(concatted_columns, expected_columns)
374+
375+
concatted_1 = pd.concat([df1, df2], axis=1)
376+
assert_frame_equal(concatted_1, expected_columns)
377+
378+
series1 = pd.Series([0.1, 0.2])
379+
series2 = pd.Series([0.3, 0.4])
380+
381+
# Index/row/0 Series
382+
expected_index_series = pd.Series(
383+
[0.1, 0.2, 0.3, 0.4], index=[0, 1, 0, 1])
384+
385+
concatted_index_series = pd.concat([series1, series2], axis='index')
386+
assert_series_equal(concatted_index_series, expected_index_series)
387+
388+
concatted_row_series = pd.concat([series1, series2], axis='rows')
389+
assert_series_equal(concatted_row_series, expected_index_series)
390+
391+
concatted_0_series = pd.concat([series1, series2], axis=0)
392+
assert_series_equal(concatted_0_series, expected_index_series)
393+
394+
# Columns/1 Series
395+
expected_columns_series = pd.DataFrame(
396+
[[0.1, 0.3], [0.2, 0.4]], index=[0, 1], columns=[0, 1])
397+
398+
concatted_columns_series = pd.concat(
399+
[series1, series2], axis='columns')
400+
assert_frame_equal(concatted_columns_series, expected_columns_series)
401+
402+
concatted_1_series = pd.concat([series1, series2], axis=1)
403+
assert_frame_equal(concatted_1_series, expected_columns_series)
404+
405+
# Testing ValueError
406+
with assertRaisesRegexp(ValueError, 'No axis named'):
407+
pd.concat([series1, series2], axis='something')
408+
350409

351410
class TestDataFrameCombineFirst(tm.TestCase, TestData):
352411

pandas/tools/merge.py

+7-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/'index', 1/'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,12 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
14111411
sample = objs[0]
14121412
self.objs = objs
14131413

1414+
# Standardize axis parameter to int
1415+
if isinstance(sample, Series):
1416+
axis = DataFrame()._get_axis_number(axis)
1417+
else:
1418+
axis = sample._get_axis_number(axis)
1419+
14141420
# Need to flip BlockManager axis in the DataFrame special case
14151421
self._is_frame = isinstance(sample, DataFrame)
14161422
if self._is_frame:

0 commit comments

Comments
 (0)