From 584ebd2b43bfdb71cdfde045110fe0d7545b6a83 Mon Sep 17 00:00:00 2001 From: "Brandon M. Burroughs" Date: Mon, 10 Oct 2016 17:15:17 -0400 Subject: [PATCH 1/6] BUG: Allow concat to take string axis names Adding tests for concat string axis names Fixing pep8 related spacing --- doc/source/whatsnew/v0.19.1.txt | 1 + pandas/tests/frame/test_combine_concat.py | 14 ++++++++++++++ pandas/tools/merge.py | 6 +++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.19.1.txt b/doc/source/whatsnew/v0.19.1.txt index 3edb8c1fa9071..d1e83319e1026 100644 --- a/doc/source/whatsnew/v0.19.1.txt +++ b/doc/source/whatsnew/v0.19.1.txt @@ -44,4 +44,5 @@ Bug Fixes - Bug in ``pd.concat`` where names of the ``keys`` were not propagated to the resulting ``MultiIndex`` (:issue:`14252`) +- Bug in ``pd.concat`` where ``axis`` cannot take string parameters ``rows`` or ``columns (:issue:`14369`) - Bug in ``MultiIndex.set_levels`` where illegal level values were still set after raising an error (:issue:`13754`) diff --git a/pandas/tests/frame/test_combine_concat.py b/pandas/tests/frame/test_combine_concat.py index b7cd8a1c01224..c1a84e970feca 100644 --- a/pandas/tests/frame/test_combine_concat.py +++ b/pandas/tests/frame/test_combine_concat.py @@ -347,6 +347,20 @@ def test_concat_named_keys(self): names=[None, None])) assert_frame_equal(concatted_unnamed, expected_unnamed) + def test_concat_axis_parameter(self): + # GH 14369 + df1 = pd.DataFrame({'A': [0.1, 0.2]}, index=range(2)) + df2 = pd.DataFrame({'A': [0.3, 0.4]}, index=range(2)) + expected_row = pd.DataFrame( + {'A': [0.1, 0.2, 0.3, 0.4]}, index=[0, 1, 0, 1]) + concatted_row = pd.concat([df1, df2], axis='rows') + assert_frame_equal(concatted_row, expected_row) + + expected_columns = pd.DataFrame( + [[0.1, 0.3], [0.2, 0.4]], index=[0, 1], columns=['A', 'A']) + concatted_columns = pd.concat([df1, df2], axis='columns') + assert_frame_equal(concatted_columns, expected_columns) + class TestDataFrameCombineFirst(tm.TestCase, TestData): diff --git a/pandas/tools/merge.py b/pandas/tools/merge.py index a8c43195f5552..9dbff77dae237 100644 --- a/pandas/tools/merge.py +++ b/pandas/tools/merge.py @@ -1283,7 +1283,7 @@ def concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised - axis : {0, 1, ...}, default 0 + axis : {0, 1, 'rows', 'columns', ...}, default 0 The axis to concatenate along join : {'inner', 'outer'}, default 'outer' How to handle indexes on other axis(es) @@ -1411,6 +1411,10 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None, sample = objs[0] self.objs = objs + # Check for string axis parameter + if isinstance(axis, str): + axis = objs[0]._get_axis_number(axis) + # Need to flip BlockManager axis in the DataFrame special case self._is_frame = isinstance(sample, DataFrame) if self._is_frame: From a6694b924bc3e32facbd8ff24be345a15ab29d82 Mon Sep 17 00:00:00 2001 From: "Brandon M. Burroughs" Date: Mon, 10 Oct 2016 19:58:26 -0400 Subject: [PATCH 2/6] Updating documentation --- doc/source/whatsnew/v0.19.1.txt | 2 +- pandas/tools/merge.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.19.1.txt b/doc/source/whatsnew/v0.19.1.txt index d1e83319e1026..01e9d2ff4ce7f 100644 --- a/doc/source/whatsnew/v0.19.1.txt +++ b/doc/source/whatsnew/v0.19.1.txt @@ -44,5 +44,5 @@ Bug Fixes - Bug in ``pd.concat`` where names of the ``keys`` were not propagated to the resulting ``MultiIndex`` (:issue:`14252`) -- Bug in ``pd.concat`` where ``axis`` cannot take string parameters ``rows`` or ``columns (:issue:`14369`) +- Bug in ``pd.concat`` where ``axis`` cannot take string parameters ``'rows'`` or ``'columns'`` (:issue:`14369`) - Bug in ``MultiIndex.set_levels`` where illegal level values were still set after raising an error (:issue:`13754`) diff --git a/pandas/tools/merge.py b/pandas/tools/merge.py index 9dbff77dae237..57d257f59bed7 100644 --- a/pandas/tools/merge.py +++ b/pandas/tools/merge.py @@ -1283,7 +1283,7 @@ def concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised - axis : {0, 1, 'rows', 'columns', ...}, default 0 + axis : {0/'index'/'rows', 1/'columns'}, default 0 The axis to concatenate along join : {'inner', 'outer'}, default 'outer' How to handle indexes on other axis(es) From cf3f998d560990dda3d3eac3130ff042b33b429b Mon Sep 17 00:00:00 2001 From: "Brandon M. Burroughs" Date: Mon, 10 Oct 2016 20:10:33 -0400 Subject: [PATCH 3/6] Adding ValueError test for concat Series axis 'columns' --- pandas/tests/frame/test_combine_concat.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/frame/test_combine_concat.py b/pandas/tests/frame/test_combine_concat.py index c1a84e970feca..41b3c6358a239 100644 --- a/pandas/tests/frame/test_combine_concat.py +++ b/pandas/tests/frame/test_combine_concat.py @@ -361,6 +361,18 @@ def test_concat_axis_parameter(self): concatted_columns = pd.concat([df1, df2], axis='columns') assert_frame_equal(concatted_columns, expected_columns) + series1 = pd.Series([0.1, 0.2]) + series2 = pd.Series([0.3, 0.4]) + + expected_row_series = pd.Series( + [0.1, 0.2, 0.3, 0.4], index=[0, 1, 0, 1]) + concatted_row_series = pd.concat([series1, series2], axis='rows') + assert_series_equal(concatted_row_series, expected_row_series) + + # A Series has no 'columns' axis + with assertRaisesRegexp(ValueError, 'No axis named columns'): + pd.concat([series1, series2], axis='columns') + class TestDataFrameCombineFirst(tm.TestCase, TestData): From 3f08b074172760b0f51d0273662621ce451c0f11 Mon Sep 17 00:00:00 2001 From: "Brandon M. Burroughs" Date: Tue, 11 Oct 2016 21:14:58 -0400 Subject: [PATCH 4/6] Adding concat tests for axis 0, 1, and 'index' --- pandas/tests/frame/test_combine_concat.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pandas/tests/frame/test_combine_concat.py b/pandas/tests/frame/test_combine_concat.py index 41b3c6358a239..5432545d48f6e 100644 --- a/pandas/tests/frame/test_combine_concat.py +++ b/pandas/tests/frame/test_combine_concat.py @@ -356,11 +356,26 @@ def test_concat_axis_parameter(self): concatted_row = pd.concat([df1, df2], axis='rows') assert_frame_equal(concatted_row, expected_row) + expected_index = pd.DataFrame( + {'A': [0.1, 0.2, 0.3, 0.4]}, index=[0, 1, 0, 1]) + concatted_index = pd.concat([df1, df2], axis='index') + assert_frame_equal(concatted_index, expected_index) + + expected_0 = pd.DataFrame( + {'A': [0.1, 0.2, 0.3, 0.4]}, index=[0, 1, 0, 1]) + concatted_0 = pd.concat([df1, df2], axis=0) + assert_frame_equal(concatted_0, expected_0) + expected_columns = pd.DataFrame( [[0.1, 0.3], [0.2, 0.4]], index=[0, 1], columns=['A', 'A']) concatted_columns = pd.concat([df1, df2], axis='columns') assert_frame_equal(concatted_columns, expected_columns) + expected_1 = pd.DataFrame( + [[0.1, 0.3], [0.2, 0.4]], index=[0, 1], columns=['A', 'A']) + concatted_1 = pd.concat([df1, df2], axis=1) + assert_frame_equal(concatted_1, expected_1) + series1 = pd.Series([0.1, 0.2]) series2 = pd.Series([0.3, 0.4]) From fdd526085dba7a9f29433322c28c6475c96d2599 Mon Sep 17 00:00:00 2001 From: "Brandon M. Burroughs" Date: Wed, 12 Oct 2016 18:49:56 -0400 Subject: [PATCH 5/6] Removing duplicate expected dfs --- pandas/tests/frame/test_combine_concat.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/pandas/tests/frame/test_combine_concat.py b/pandas/tests/frame/test_combine_concat.py index 5432545d48f6e..2aa0b39051390 100644 --- a/pandas/tests/frame/test_combine_concat.py +++ b/pandas/tests/frame/test_combine_concat.py @@ -351,30 +351,25 @@ def test_concat_axis_parameter(self): # GH 14369 df1 = pd.DataFrame({'A': [0.1, 0.2]}, index=range(2)) df2 = pd.DataFrame({'A': [0.3, 0.4]}, index=range(2)) - expected_row = pd.DataFrame( - {'A': [0.1, 0.2, 0.3, 0.4]}, index=[0, 1, 0, 1]) - concatted_row = pd.concat([df1, df2], axis='rows') - assert_frame_equal(concatted_row, expected_row) expected_index = pd.DataFrame( {'A': [0.1, 0.2, 0.3, 0.4]}, index=[0, 1, 0, 1]) concatted_index = pd.concat([df1, df2], axis='index') assert_frame_equal(concatted_index, expected_index) - expected_0 = pd.DataFrame( - {'A': [0.1, 0.2, 0.3, 0.4]}, index=[0, 1, 0, 1]) + concatted_row = pd.concat([df1, df2], axis='rows') + assert_frame_equal(concatted_row, expected_index) + concatted_0 = pd.concat([df1, df2], axis=0) - assert_frame_equal(concatted_0, expected_0) + assert_frame_equal(concatted_0, expected_index) expected_columns = pd.DataFrame( [[0.1, 0.3], [0.2, 0.4]], index=[0, 1], columns=['A', 'A']) concatted_columns = pd.concat([df1, df2], axis='columns') assert_frame_equal(concatted_columns, expected_columns) - expected_1 = pd.DataFrame( - [[0.1, 0.3], [0.2, 0.4]], index=[0, 1], columns=['A', 'A']) concatted_1 = pd.concat([df1, df2], axis=1) - assert_frame_equal(concatted_1, expected_1) + assert_frame_equal(concatted_1, expected_columns) series1 = pd.Series([0.1, 0.2]) series2 = pd.Series([0.3, 0.4]) From 64702fb740e3a1526dfbdb1a6a0225eb02e4709a Mon Sep 17 00:00:00 2001 From: "Brandon M. Burroughs" Date: Wed, 12 Oct 2016 18:52:33 -0400 Subject: [PATCH 6/6] Updating documentation for concat --- pandas/tools/merge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tools/merge.py b/pandas/tools/merge.py index 57d257f59bed7..798f268ee4549 100644 --- a/pandas/tools/merge.py +++ b/pandas/tools/merge.py @@ -1283,7 +1283,7 @@ def concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised - axis : {0/'index'/'rows', 1/'columns'}, default 0 + axis : {0/'index', 1/'columns'}, default 0 The axis to concatenate along join : {'inner', 'outer'}, default 'outer' How to handle indexes on other axis(es)