From ded4775d05bbd807b6a32d9f4cc7dfa89f0ba319 Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Fri, 8 Jun 2018 16:51:29 +0530 Subject: [PATCH 1/7] Update test_pivot.py TST: add additional test cases for pivot_table with categorical data #21370 --- pandas/tests/reshape/test_pivot.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 3ec60d50f2792..61bd06a6aba9a 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -142,6 +142,23 @@ def test_pivot_table_dropna_categoricals(self, dropna): tm.assert_frame_equal(result, expected) + def test_pivot_with_non_observable_dropna_civ(self, dropna): + # gh-21370 + df=pd.DataFrame( + {"In":pd.Categorical([np.nan,'low','high','low','high','high',np.nan], + categories=['low','high'],ordered=True), + "Col":["A","B","C","C","C","A","B"], + "Val":range(7)}) + result=df.pivot_table(index="In",columns="Col",values="Val",dropna=dropna) + expected=pd.DataFrame( + {'A':[np.nan,5],'B':[1,np.nan],'C':[3.0,3.0]}, + index=pd.Index( + pd.Categorical.from_codes([0,1], + categories=['low','high'], + ordered=True), + name='In')) + tm.assert_frame_equal(result,expected) + def test_pivot_with_non_observable_dropna(self, dropna): # gh-21133 df = pd.DataFrame( From 7dbaa95ff4769158cdd9f899e9462db485f3d15b Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Fri, 8 Jun 2018 17:08:16 +0530 Subject: [PATCH 2/7] Update test_pivot.py Fixed PEP-8 issues --- pandas/tests/reshape/test_pivot.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 61bd06a6aba9a..bf22baa2250d4 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -143,21 +143,23 @@ def test_pivot_table_dropna_categoricals(self, dropna): tm.assert_frame_equal(result, expected) def test_pivot_with_non_observable_dropna_civ(self, dropna): - # gh-21370 - df=pd.DataFrame( - {"In":pd.Categorical([np.nan,'low','high','low','high','high',np.nan], - categories=['low','high'],ordered=True), - "Col":["A","B","C","C","C","A","B"], - "Val":range(7)}) - result=df.pivot_table(index="In",columns="Col",values="Val",dropna=dropna) - expected=pd.DataFrame( - {'A':[np.nan,5],'B':[1,np.nan],'C':[3.0,3.0]}, + # gh-21370 + arr = [np.nan, 'low', 'high', 'low', 'high', 'high', np.nan] + df = pd.DataFrame( + {"In": pd.Categorical(arr, + categories=['low', 'high'], ordered=True), + "Col": ["A", "B", "C", "C", "C", "A", "B"], + "Val": range(7)}) + result = df.pivot_table(index="In", columns="Col", values="Val", + dropna=dropna) + expected = pd.DataFrame( + {'A': [np.nan, 5], 'B': [1, np.nan], 'C': [3.0, 3.0]}, index=pd.Index( pd.Categorical.from_codes([0,1], - categories=['low','high'], + categories=['low', 'high'], ordered=True), name='In')) - tm.assert_frame_equal(result,expected) + tm.assert_frame_equal(result,expected) def test_pivot_with_non_observable_dropna(self, dropna): # gh-21133 From 4dbb21ab4a1f870ef3814681362d5d4b4f7483e3 Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Fri, 8 Jun 2018 17:46:26 +0530 Subject: [PATCH 3/7] Update test_pivot.py Fixed linting --- pandas/tests/reshape/test_pivot.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index bf22baa2250d4..dfdc226f3f431 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -145,22 +145,22 @@ def test_pivot_table_dropna_categoricals(self, dropna): def test_pivot_with_non_observable_dropna_civ(self, dropna): # gh-21370 arr = [np.nan, 'low', 'high', 'low', 'high', 'high', np.nan] - df = pd.DataFrame( - {"In": pd.Categorical(arr, - categories=['low', 'high'], ordered=True), - "Col": ["A", "B", "C", "C", "C", "A", "B"], - "Val": range(7)}) + df = pd.DataFrame({"In": pd.Categorical(arr, + categories=['low', 'high'], + ordered=True), "Col": ["A", + "B", "C", "C", "C", "A", "B"], + "Val": range(7)}) result = df.pivot_table(index="In", columns="Col", values="Val", - dropna=dropna) + dropna=dropna) expected = pd.DataFrame( {'A': [np.nan, 5], 'B': [1, np.nan], 'C': [3.0, 3.0]}, index=pd.Index( - pd.Categorical.from_codes([0,1], + pd.Categorical.from_codes([0, 1], categories=['low', 'high'], ordered=True), name='In')) - tm.assert_frame_equal(result,expected) - + tm.assert_frame_equal(result, expected) + def test_pivot_with_non_observable_dropna(self, dropna): # gh-21133 df = pd.DataFrame( From 15c1ef0e31e2fddfb80436dabc2682751d743c31 Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Fri, 8 Jun 2018 17:52:18 +0530 Subject: [PATCH 4/7] Update test_pivot.py --- pandas/tests/reshape/test_pivot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index dfdc226f3f431..b5467ff4858f7 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -155,10 +155,10 @@ def test_pivot_with_non_observable_dropna_civ(self, dropna): expected = pd.DataFrame( {'A': [np.nan, 5], 'B': [1, np.nan], 'C': [3.0, 3.0]}, index=pd.Index( - pd.Categorical.from_codes([0, 1], - categories=['low', 'high'], - ordered=True), - name='In')) + pd.Categorical.from_codes([0, 1], + categories=['low', 'high'], + ordered=True), + name='In')) tm.assert_frame_equal(result, expected) def test_pivot_with_non_observable_dropna(self, dropna): From c6196aaf038e3dc23e5925286c270309af48c923 Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Sat, 9 Jun 2018 00:26:56 +0530 Subject: [PATCH 5/7] Update test_pivot.py fixed linting --- pandas/tests/reshape/test_pivot.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index b5467ff4858f7..4244777b89471 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -153,12 +153,12 @@ def test_pivot_with_non_observable_dropna_civ(self, dropna): result = df.pivot_table(index="In", columns="Col", values="Val", dropna=dropna) expected = pd.DataFrame( - {'A': [np.nan, 5], 'B': [1, np.nan], 'C': [3.0, 3.0]}, - index=pd.Index( - pd.Categorical.from_codes([0, 1], - categories=['low', 'high'], - ordered=True), - name='In')) + {'A': [np.nan, 5], 'B': [1, np.nan], 'C': [3.0, 3.0]}, + index=pd.Index( + pd.Categorical.from_codes([0, 1], + categories=['low', 'high'], + ordered=True), + name='In')) tm.assert_frame_equal(result, expected) def test_pivot_with_non_observable_dropna(self, dropna): From 23be90a0b0914a596060408b2c4600fc328287d4 Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Sun, 10 Jun 2018 16:12:37 +0530 Subject: [PATCH 6/7] Update test_pivot.py Attempt on making the code readable --- pandas/tests/reshape/test_pivot.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 4244777b89471..b7afd84915bf6 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -144,16 +144,20 @@ def test_pivot_table_dropna_categoricals(self, dropna): def test_pivot_with_non_observable_dropna_civ(self, dropna): # gh-21370 - arr = [np.nan, 'low', 'high', 'low', 'high', 'high', np.nan] - df = pd.DataFrame({"In": pd.Categorical(arr, - categories=['low', 'high'], - ordered=True), "Col": ["A", - "B", "C", "C", "C", "A", "B"], - "Val": range(7)}) + arr = [np.nan, 'low', 'high', 'low', np.nan] + df = pd.DataFrame( + {"In": pd.Categorical(arr, + categories=['low', 'high'], + ordered=True), + "Col": ["A", "B", "C", "A", "B"], + "Val": range(1, 6)}) result = df.pivot_table(index="In", columns="Col", values="Val", dropna=dropna) expected = pd.DataFrame( - {'A': [np.nan, 5], 'B': [1, np.nan], 'C': [3.0, 3.0]}, + { + 'A': [4.0, np.nan], + 'B': [2.0, np.nan], + 'C': [np.nan, 3.0]}, index=pd.Index( pd.Categorical.from_codes([0, 1], categories=['low', 'high'], From ee6bf25351d38a3be0fbfe7620e7a6f26f48f366 Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Mon, 11 Jun 2018 02:18:34 +0530 Subject: [PATCH 7/7] Update test_pivot.py --- pandas/tests/reshape/test_pivot.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index b7afd84915bf6..e7a9f5551b2df 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -145,23 +145,24 @@ def test_pivot_table_dropna_categoricals(self, dropna): def test_pivot_with_non_observable_dropna_civ(self, dropna): # gh-21370 arr = [np.nan, 'low', 'high', 'low', np.nan] - df = pd.DataFrame( - {"In": pd.Categorical(arr, - categories=['low', 'high'], - ordered=True), - "Col": ["A", "B", "C", "A", "B"], - "Val": range(1, 6)}) + df = pd.DataFrame({ + "In": pd.Categorical(arr, + categories=['low', 'high'], + ordered=True), + "Col": ["A", "B", "C", "A", "B"], + "Val": range(1, 6) + }) result = df.pivot_table(index="In", columns="Col", values="Val", dropna=dropna) - expected = pd.DataFrame( - { - 'A': [4.0, np.nan], - 'B': [2.0, np.nan], - 'C': [np.nan, 3.0]}, + expected = pd.DataFrame({ + 'A': [4.0, np.nan], + 'B': [2.0, np.nan], + 'C': [np.nan, 3.0]}, index=pd.Index( - pd.Categorical.from_codes([0, 1], - categories=['low', 'high'], - ordered=True), + pd.Categorical.from_codes( + [0, 1], + categories=['low', 'high'], + ordered=True), name='In')) tm.assert_frame_equal(result, expected)