From f82d9f37209bdffc3ae3256435cf1f811783de06 Mon Sep 17 00:00:00 2001 From: Artin Sarraf Date: Sun, 4 Nov 2018 16:48:13 -0500 Subject: [PATCH 1/6] BUG - account for names when concating series on axis 1 --- pandas/core/reshape/concat.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index 1c602a0af1ec1..4ed2665edb9cf 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -500,7 +500,9 @@ def _get_concat_axis(self): else: return ibase.default_index(len(self.objs)) else: - return ensure_index(self.keys) + new_axis = ensure_index(self.keys) + new_axis.names = self.names + return new_axis else: indexes = [x._data.axes[self.axis] for x in self.objs] From 3346f7fd37b6985027867d110d82b61d8d9cd95c Mon Sep 17 00:00:00 2001 From: Artin Sarraf Date: Sun, 4 Nov 2018 17:04:14 -0500 Subject: [PATCH 2/6] TST - add test to make sure names argument is accounted for when concating series and axis=1 --- pandas/tests/reshape/test_concat.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 2aaa04d571e69..57df4a0b47959 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -1618,6 +1618,22 @@ def test_concat_series_axis1(self, sort=sort): expected = DataFrame({'A': s, 'B': s2}) assert_frame_equal(result, expected) + # ensure names argument is not ignored on axis=1 + s = Series([1, 2, 3]) + s2 = Series([4, 5, 6]) + result = concat([s, s2], axis=1, keys=['a', 'b'], names=['A']) + expected = DataFrame([[1, 4], [2, 5], [3, 6]], + columns=pd.Index(['a', 'b'], name='A')) + assert_frame_equal(result, expected) + + result = concat([s, s2], axis=1, keys=[('a', 1), ('b', 2)], + names=['A', 'B']) + expected = DataFrame([[1, 4], [2, 5], [3, 6]], + columns=MultiIndex.from_tuples([('a', 1), + ('b', 2)], + names=['A', 'B'])) + assert_frame_equal(result, expected) + def test_concat_single_with_key(self): df = DataFrame(np.random.randn(10, 4)) From 8f0d8d86b3d6d9b2180f84c3fdf6144dab3bb372 Mon Sep 17 00:00:00 2001 From: Artin Sarraf Date: Sun, 4 Nov 2018 17:11:04 -0500 Subject: [PATCH 3/6] DOC - update whats new --- doc/source/whatsnew/v0.24.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 700bf4ddc3a37..22939ceb0817b 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -933,6 +933,7 @@ Reshaping ^^^^^^^^^ - Bug in :func:`pandas.concat` when joining resampled DataFrames with timezone aware index (:issue:`13783`) +- Bug in :func:`pandas.concat` when joining only series the `names` argument of `concat` is no longer ignored (:issue:`23490`) - Bug in :meth:`Series.combine_first` with ``datetime64[ns, tz]`` dtype which would return tz-naive result (:issue:`21469`) - Bug in :meth:`Series.where` and :meth:`DataFrame.where` with ``datetime64[ns, tz]`` dtype (:issue:`21546`) - Bug in :meth:`Series.mask` and :meth:`DataFrame.mask` with ``list`` conditionals (:issue:`21891`) From de4be0b7cfde0f56f2f3e360c68cc67e15ecb5f8 Mon Sep 17 00:00:00 2001 From: Artin Sarraf Date: Sun, 4 Nov 2018 18:23:40 -0500 Subject: [PATCH 4/6] DOC - reference issue associated with test --- doc/source/whatsnew/v0.24.0.txt | 2 +- pandas/tests/reshape/test_concat.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 22939ceb0817b..b23a42c6ac2c7 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -933,7 +933,7 @@ Reshaping ^^^^^^^^^ - Bug in :func:`pandas.concat` when joining resampled DataFrames with timezone aware index (:issue:`13783`) -- Bug in :func:`pandas.concat` when joining only series the `names` argument of `concat` is no longer ignored (:issue:`23490`) +- Bug in :func:`pandas.concat` when joining only `Series` the `names` argument of `concat` is no longer ignored (:issue:`23490`) - Bug in :meth:`Series.combine_first` with ``datetime64[ns, tz]`` dtype which would return tz-naive result (:issue:`21469`) - Bug in :meth:`Series.where` and :meth:`DataFrame.where` with ``datetime64[ns, tz]`` dtype (:issue:`21546`) - Bug in :meth:`Series.mask` and :meth:`DataFrame.mask` with ``list`` conditionals (:issue:`21891`) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 57df4a0b47959..8c2f015e75d43 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -1618,7 +1618,7 @@ def test_concat_series_axis1(self, sort=sort): expected = DataFrame({'A': s, 'B': s2}) assert_frame_equal(result, expected) - # ensure names argument is not ignored on axis=1 + # ensure names argument is not ignored on axis=1, #23490 s = Series([1, 2, 3]) s2 = Series([4, 5, 6]) result = concat([s, s2], axis=1, keys=['a', 'b'], names=['A']) From a41a0d3f2b72d350b1403080d9d27d573deb14da Mon Sep 17 00:00:00 2001 From: Artin Sarraf Date: Mon, 5 Nov 2018 22:38:29 -0500 Subject: [PATCH 5/6] BUG - use set_names instead of .names=... to ensure original keys not mutated --- pandas/core/reshape/concat.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index 4ed2665edb9cf..bc869f76dcfa2 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -500,9 +500,7 @@ def _get_concat_axis(self): else: return ibase.default_index(len(self.objs)) else: - new_axis = ensure_index(self.keys) - new_axis.names = self.names - return new_axis + return ensure_index(self.keys).set_names(self.names) else: indexes = [x._data.axes[self.axis] for x in self.objs] From 3e624e899dc53ed24aed6e0599018511bb160f60 Mon Sep 17 00:00:00 2001 From: Artin Sarraf Date: Mon, 5 Nov 2018 22:38:59 -0500 Subject: [PATCH 6/6] TST - break up test into its own testing method --- pandas/tests/reshape/test_concat.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 8c2f015e75d43..59fbd356008a7 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -1618,6 +1618,7 @@ def test_concat_series_axis1(self, sort=sort): expected = DataFrame({'A': s, 'B': s2}) assert_frame_equal(result, expected) + def test_concat_series_axis1_names_applied(self): # ensure names argument is not ignored on axis=1, #23490 s = Series([1, 2, 3]) s2 = Series([4, 5, 6])