From 13f00dfe96cab9f8ec6f58ffece90e4fc69c115b Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sat, 19 May 2018 18:46:43 +0530 Subject: [PATCH 01/10] GH21015 Does not raise error in concatting Series with numpy scalar and tuple names --- pandas/core/common.py | 6 +++++- pandas/tests/reshape/test_concat.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index b9182bfd2cbe2..58f72ada47293 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -55,7 +55,11 @@ def flatten(l): def _consensus_name_attr(objs): name = objs[0].name for obj in objs[1:]: - if obj.name != name: + name_check = (obj.name != name) + if isinstance(name_check, np.ndarray): + # 'If' test can also be a negative for 'np.bool_' or 'bool' + return None + elif name_check: return None return name diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index f5e58fa70e1c4..80083356ef88e 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -2350,6 +2350,20 @@ def test_concat_datetime_timezone(self): tm.assert_frame_equal(result, expected) + def test_concat_series_name_npscalar_tuple(self): + # GH21015 + s1 = pd.Series({'a': 1.5}, name=np.int64(190)) + s2 = pd.Series([], name=(43, 0)) + result = pd.concat([s1, s2]) + expected = pd.Series({'a': 1.5}) + tm.assert_series_equal(result, expected) + + df1 = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'], index=[0, 1]) + df2 = pd.DataFrame([[5, 6], [7, 8]], columns=['c', 'd'], + index=pd.MultiIndex.from_tuples([(0, 0), (1, 1)])) + result = pd.concat([df1.iloc[0], df2.iloc[0]]) + expected = pd.Series({'a':1, 'b':2, 'c':5, 'd':6}) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize('pdt', [pd.Series, pd.DataFrame, pd.Panel]) @pytest.mark.parametrize('dt', np.sctypes['float']) From 354eb2d7a7744a1dc7cfaa8ae80c99830460883c Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sat, 19 May 2018 18:48:51 +0530 Subject: [PATCH 02/10] GH21015 Does not raise error in concatting Series with numpy scalar and tuple names --- pandas/tests/reshape/test_concat.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 80083356ef88e..f577b67476298 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -2362,9 +2362,10 @@ def test_concat_series_name_npscalar_tuple(self): df2 = pd.DataFrame([[5, 6], [7, 8]], columns=['c', 'd'], index=pd.MultiIndex.from_tuples([(0, 0), (1, 1)])) result = pd.concat([df1.iloc[0], df2.iloc[0]]) - expected = pd.Series({'a':1, 'b':2, 'c':5, 'd':6}) + expected = pd.Series({'a': 1, 'b': 2, 'c': 5, 'd': 6}) tm.assert_series_equal(result, expected) + @pytest.mark.parametrize('pdt', [pd.Series, pd.DataFrame, pd.Panel]) @pytest.mark.parametrize('dt', np.sctypes['float']) def test_concat_no_unnecessary_upcast(dt, pdt): From 613ab4e99b93cde3bce0e662cd1d50a17527974b Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sat, 19 May 2018 19:46:32 +0530 Subject: [PATCH 03/10] GH21015 Changed to negative test for bool or np.bool_ --- pandas/core/common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index 58f72ada47293..473b2f5874846 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -56,8 +56,8 @@ def _consensus_name_attr(objs): name = objs[0].name for obj in objs[1:]: name_check = (obj.name != name) - if isinstance(name_check, np.ndarray): - # 'If' test can also be a negative for 'np.bool_' or 'bool' + if not isinstance(name_check, (bool, np.bool_)): + # 'If' test can also be a positive for 'np.ndarray' return None elif name_check: return None From 3e84f9ca829205bd30c28499dc7010531f2d0528 Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sat, 19 May 2018 23:11:42 +0530 Subject: [PATCH 04/10] GH21015 Changed to try except block, instead of type introspection --- doc/source/whatsnew/v0.23.1.txt | 2 +- pandas/core/common.py | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v0.23.1.txt b/doc/source/whatsnew/v0.23.1.txt index 9382d74f95295..1a5239663c305 100644 --- a/doc/source/whatsnew/v0.23.1.txt +++ b/doc/source/whatsnew/v0.23.1.txt @@ -82,7 +82,7 @@ Plotting Reshaping ^^^^^^^^^ -- +- Bug in :func:`concat` where error was raised in concatenating Series with numpy scalar and tuple names (:issue:`21015`) - Categorical diff --git a/pandas/core/common.py b/pandas/core/common.py index 473b2f5874846..213bb3cc12d83 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -55,11 +55,10 @@ def flatten(l): def _consensus_name_attr(objs): name = objs[0].name for obj in objs[1:]: - name_check = (obj.name != name) - if not isinstance(name_check, (bool, np.bool_)): - # 'If' test can also be a positive for 'np.ndarray' - return None - elif name_check: + try: + if obj.name != name: + return None + except ValueError: return None return name From 35b664f136d49f7faf7c3f3c3b0ec9924bed29b2 Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sun, 20 May 2018 00:58:25 +0530 Subject: [PATCH 05/10] GH21015 made changes suggested by @jreback on 19May --- doc/source/whatsnew/v0.23.1.txt | 2 +- pandas/core/common.py | 4 ++-- pandas/tests/reshape/test_concat.py | 26 +++++++++++--------------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/doc/source/whatsnew/v0.23.1.txt b/doc/source/whatsnew/v0.23.1.txt index 1a5239663c305..fb02a19ec4718 100644 --- a/doc/source/whatsnew/v0.23.1.txt +++ b/doc/source/whatsnew/v0.23.1.txt @@ -82,7 +82,7 @@ Plotting Reshaping ^^^^^^^^^ -- Bug in :func:`concat` where error was raised in concatenating Series with numpy scalar and tuple names (:issue:`21015`) +- Bug in :func:`concat` where error was raised in concatenating ``Series`` with numpy scalar and tuple names (:issue:`21015`) - Categorical diff --git a/pandas/core/common.py b/pandas/core/common.py index 213bb3cc12d83..1de8269c9a0c6 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -57,9 +57,9 @@ def _consensus_name_attr(objs): for obj in objs[1:]: try: if obj.name != name: - return None + name = None except ValueError: - return None + name = None return name diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index f577b67476298..6afd825709f86 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -2350,21 +2350,6 @@ def test_concat_datetime_timezone(self): tm.assert_frame_equal(result, expected) - def test_concat_series_name_npscalar_tuple(self): - # GH21015 - s1 = pd.Series({'a': 1.5}, name=np.int64(190)) - s2 = pd.Series([], name=(43, 0)) - result = pd.concat([s1, s2]) - expected = pd.Series({'a': 1.5}) - tm.assert_series_equal(result, expected) - - df1 = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'], index=[0, 1]) - df2 = pd.DataFrame([[5, 6], [7, 8]], columns=['c', 'd'], - index=pd.MultiIndex.from_tuples([(0, 0), (1, 1)])) - result = pd.concat([df1.iloc[0], df2.iloc[0]]) - expected = pd.Series({'a': 1, 'b': 2, 'c': 5, 'd': 6}) - tm.assert_series_equal(result, expected) - @pytest.mark.parametrize('pdt', [pd.Series, pd.DataFrame, pd.Panel]) @pytest.mark.parametrize('dt', np.sctypes['float']) @@ -2502,3 +2487,14 @@ def test_concat_aligned_sort_does_not_raise(): columns=[1, 'a']) result = pd.concat([df, df], ignore_index=True, sort=True) tm.assert_frame_equal(result, expected) + + +@pytest.mark.parametrize("s1name, s2name", [(np.int64(190), (43, 0)), + (190, (43, 0))]) +def test_concat_series_name_npscalar_tuple(s1name, s2name): + # GH21015 + s1 = pd.Series({'a': 1.5}, name=s1name) + s2 = pd.Series([], name=s2name) + result = pd.concat([s1, s2]) + expected = pd.Series({'a': 1.5}) + tm.assert_series_equal(result, expected) From 7ec0135e17b9758f9ae64f76e018afb4cf378797 Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sun, 20 May 2018 01:08:38 +0530 Subject: [PATCH 06/10] GH21015 made changes suggested by @jreback on 19May --- pandas/tests/reshape/test_concat.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 6afd825709f86..2cf8232b483e2 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -2493,8 +2493,8 @@ def test_concat_aligned_sort_does_not_raise(): (190, (43, 0))]) def test_concat_series_name_npscalar_tuple(s1name, s2name): # GH21015 - s1 = pd.Series({'a': 1.5}, name=s1name) - s2 = pd.Series([], name=s2name) + s1 = pd.Series({'a': 1, 'b': 2}, name=s1name) + s2 = pd.Series({'c': 5, 'd': 6}, name=s2name) result = pd.concat([s1, s2]) - expected = pd.Series({'a': 1.5}) + expected = pd.Series({'a': 1, 'b': 2, 'c': 5, 'd': 6}) tm.assert_series_equal(result, expected) From 345c084f22e61abffb1e870b35ca6f5a6086a03a Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sun, 20 May 2018 07:47:45 +0530 Subject: [PATCH 07/10] GH21015 made minor style changes to test file suggested by @WillAyd --- pandas/tests/reshape/test_concat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 2cf8232b483e2..f5d5515a01ca8 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -2489,8 +2489,8 @@ def test_concat_aligned_sort_does_not_raise(): tm.assert_frame_equal(result, expected) -@pytest.mark.parametrize("s1name, s2name", [(np.int64(190), (43, 0)), - (190, (43, 0))]) +@pytest.mark.parametrize("s1name,s2name", + [(np.int64(190), (43, 0)), (190, (43, 0))]) def test_concat_series_name_npscalar_tuple(s1name, s2name): # GH21015 s1 = pd.Series({'a': 1, 'b': 2}, name=s1name) From 21d834e52a4bc97d539466ec01ee4f5627892250 Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sun, 20 May 2018 08:15:21 +0530 Subject: [PATCH 08/10] GH21015 made minor style changes to test file suggested by @WillAyd --- pandas/tests/reshape/test_concat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index f5d5515a01ca8..a0ffbaef2b685 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -2489,8 +2489,8 @@ def test_concat_aligned_sort_does_not_raise(): tm.assert_frame_equal(result, expected) -@pytest.mark.parametrize("s1name,s2name", - [(np.int64(190), (43, 0)), (190, (43, 0))]) +@pytest.mark.parametrize("s1name,s2name", [ + (np.int64(190), (43, 0)), (190, (43, 0))]) def test_concat_series_name_npscalar_tuple(s1name, s2name): # GH21015 s1 = pd.Series({'a': 1, 'b': 2}, name=s1name) From ba8abba1e1d27e91dae89391620e641c81de982f Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sun, 20 May 2018 08:25:49 +0530 Subject: [PATCH 09/10] GH21015 made minor style changes to whatsnew file suggested by @WillAyd --- doc/source/whatsnew/v0.23.1.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.1.txt b/doc/source/whatsnew/v0.23.1.txt index fb02a19ec4718..5973ad2ebf43f 100644 --- a/doc/source/whatsnew/v0.23.1.txt +++ b/doc/source/whatsnew/v0.23.1.txt @@ -82,7 +82,7 @@ Plotting Reshaping ^^^^^^^^^ -- Bug in :func:`concat` where error was raised in concatenating ``Series`` with numpy scalar and tuple names (:issue:`21015`) +- Bug in :func:`concat` where error was raised in concatenating :class:`Series` with numpy scalar and tuple names (:issue:`21015`) - Categorical From 8aa7e054ca53f9896ecb6c22730e403c95842bee Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sun, 20 May 2018 08:29:52 +0530 Subject: [PATCH 10/10] GH21015 made minor style changes to test file suggested by @WillAyd --- pandas/tests/reshape/test_concat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index a0ffbaef2b685..dea305d4b3fee 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -2490,7 +2490,7 @@ def test_concat_aligned_sort_does_not_raise(): @pytest.mark.parametrize("s1name,s2name", [ - (np.int64(190), (43, 0)), (190, (43, 0))]) + (np.int64(190), (43, 0)), (190, (43, 0))]) def test_concat_series_name_npscalar_tuple(s1name, s2name): # GH21015 s1 = pd.Series({'a': 1, 'b': 2}, name=s1name)