From 27852bcb3b24fc6b87de60f710f047bdb0e89eab Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Sat, 9 Jun 2018 01:00:01 +0530 Subject: [PATCH 1/5] Update test_pivot.py BUG: Incorrect values shown by pivot_table() #21378 --- pandas/tests/reshape/test_pivot.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 3ec60d50f2792..7403280099cf2 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -142,6 +142,25 @@ def test_pivot_table_dropna_categoricals(self, dropna): tm.assert_frame_equal(result, expected) + def test_pivot_with_new_patch(self, dropna): + # gh-21133 + df = pd.DataFrame( + {'A': pd.Categorical(['left', 'low', 'high', 'low', 'high'], + categories=['low', 'high', 'left'], + ordered=True), + 'B': range(5)}) + + result = df.pivot_table(index='A', values='B', dropna=dropna) + expected = pd.DataFrame( + {'B': [2, 3, 0]}, + index=pd.Index( + pd.Categorical.from_codes([0, 1, 2], + categories=['low', 'high', 'left'], + ordered=True), + name='A')) + + tm.assert_frame_equal(result, expected) + def test_pivot_with_non_observable_dropna(self, dropna): # gh-21133 df = pd.DataFrame( From 4bab2bd1eed45593c18ff852a7aa1fc9193243ab Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Wed, 13 Jun 2018 16:52:11 +0530 Subject: [PATCH 2/5] Update test_pivot.py Added a new example in pre-existing test --- pandas/tests/reshape/test_pivot.py | 36 +++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 7403280099cf2..2ff6cc6ce9f3c 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -142,43 +142,43 @@ def test_pivot_table_dropna_categoricals(self, dropna): tm.assert_frame_equal(result, expected) - def test_pivot_with_new_patch(self, dropna): + + def test_pivot_with_non_observable_dropna(self, dropna): # gh-21133 df = pd.DataFrame( - {'A': pd.Categorical(['left', 'low', 'high', 'low', 'high'], - categories=['low', 'high', 'left'], + {'A': pd.Categorical([np.nan, 'low', 'high', 'low', 'high'], + categories=['low', 'high'], ordered=True), 'B': range(5)}) result = df.pivot_table(index='A', values='B', dropna=dropna) expected = pd.DataFrame( - {'B': [2, 3, 0]}, + {'B': [2, 3]}, index=pd.Index( - pd.Categorical.from_codes([0, 1, 2], - categories=['low', 'high', 'left'], + pd.Categorical.from_codes([0, 1], + categories=['low', 'high'], ordered=True), name='A')) tm.assert_frame_equal(result, expected) - - def test_pivot_with_non_observable_dropna(self, dropna): - # gh-21133 - df = pd.DataFrame( - {'A': pd.Categorical([np.nan, 'low', 'high', 'low', 'high'], - categories=['low', 'high'], + + # gh-21378 + df2 = pd.DataFrame( + {'A': pd.Categorical(['left', 'low', 'high', 'low', 'high'], + categories=['low', 'high', 'left'], ordered=True), 'B': range(5)}) - result = df.pivot_table(index='A', values='B', dropna=dropna) - expected = pd.DataFrame( - {'B': [2, 3]}, + result2 = df.pivot_table(index='A', values='B', dropna=dropna) + expected2 = pd.DataFrame( + {'B': [2, 3, 0]}, index=pd.Index( - pd.Categorical.from_codes([0, 1], - categories=['low', 'high'], + pd.Categorical.from_codes([0, 1, 2], + categories=['low', 'high', 'left'], ordered=True), name='A')) - tm.assert_frame_equal(result, expected) + tm.assert_frame_equal(result2, expected2) def test_pass_array(self): result = self.data.pivot_table( From d85fc15be23248fe117da58400c76cca5622a3fe Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Wed, 13 Jun 2018 16:53:47 +0530 Subject: [PATCH 3/5] Update test_pivot.py --- pandas/tests/reshape/test_pivot.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 2ff6cc6ce9f3c..7ba03e87d9601 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -142,7 +142,6 @@ def test_pivot_table_dropna_categoricals(self, dropna): tm.assert_frame_equal(result, expected) - def test_pivot_with_non_observable_dropna(self, dropna): # gh-21133 df = pd.DataFrame( @@ -161,7 +160,7 @@ def test_pivot_with_non_observable_dropna(self, dropna): name='A')) tm.assert_frame_equal(result, expected) - + # gh-21378 df2 = pd.DataFrame( {'A': pd.Categorical(['left', 'low', 'high', 'low', 'high'], From 550bfd8523b46f46bfa56a48cb7f1b597aa181d0 Mon Sep 17 00:00:00 2001 From: Uddeshya Singh Date: Fri, 15 Jun 2018 02:41:21 +0530 Subject: [PATCH 4/5] Update test_pivot.py --- pandas/tests/reshape/test_pivot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 7ba03e87d9601..f95ce28ba5c8b 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -168,7 +168,7 @@ def test_pivot_with_non_observable_dropna(self, dropna): ordered=True), 'B': range(5)}) - result2 = df.pivot_table(index='A', values='B', dropna=dropna) + result2 = df2.pivot_table(index='A', values='B', dropna=dropna) expected2 = pd.DataFrame( {'B': [2, 3, 0]}, index=pd.Index( From 355aea3df1f352d344c866195a96ed8ecdc71b06 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Fri, 15 Jun 2018 13:32:18 -0400 Subject: [PATCH 5/5] rename tests --- 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 f95ce28ba5c8b..ca95dde1a20c9 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -162,14 +162,14 @@ def test_pivot_with_non_observable_dropna(self, dropna): tm.assert_frame_equal(result, expected) # gh-21378 - df2 = pd.DataFrame( + df = pd.DataFrame( {'A': pd.Categorical(['left', 'low', 'high', 'low', 'high'], categories=['low', 'high', 'left'], ordered=True), 'B': range(5)}) - result2 = df2.pivot_table(index='A', values='B', dropna=dropna) - expected2 = pd.DataFrame( + result = df.pivot_table(index='A', values='B', dropna=dropna) + expected = pd.DataFrame( {'B': [2, 3, 0]}, index=pd.Index( pd.Categorical.from_codes([0, 1, 2], @@ -177,7 +177,7 @@ def test_pivot_with_non_observable_dropna(self, dropna): ordered=True), name='A')) - tm.assert_frame_equal(result2, expected2) + tm.assert_frame_equal(result, expected) def test_pass_array(self): result = self.data.pivot_table(