From d605251fb0566a957c5278eb05f18114491e480a Mon Sep 17 00:00:00 2001 From: Nicholas Ver Halen Date: Wed, 22 Feb 2017 18:03:06 -0600 Subject: [PATCH 1/4] Made sure pivot_table propped na columns --- pandas/tools/pivot.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/tools/pivot.py b/pandas/tools/pivot.py index 41fc705691a96..fc0b92de9316d 100644 --- a/pandas/tools/pivot.py +++ b/pandas/tools/pivot.py @@ -175,6 +175,9 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', if len(index) == 0 and len(columns) > 0: table = table.T + if isinstance(table, DataFrame) and dropna: + table = table.dropna(how='all', axis=1) + return table From a6432677d7249ee93141e05987f03d97b553ac18 Mon Sep 17 00:00:00 2001 From: Nicholas Ver Halen Date: Thu, 23 Feb 2017 21:53:22 -0600 Subject: [PATCH 2/4] Added test for issue 15193 --- pandas/tests/tools/test_pivot.py | 33 ++++++++++++++++++++++++++++++++ pandas/tools/pivot.py | 1 + 2 files changed, 34 insertions(+) diff --git a/pandas/tests/tools/test_pivot.py b/pandas/tests/tools/test_pivot.py index f5d91d0088306..62863372dbd02 100644 --- a/pandas/tests/tools/test_pivot.py +++ b/pandas/tests/tools/test_pivot.py @@ -86,6 +86,39 @@ def test_pivot_table_dropna(self): tm.assert_index_equal(pv_col.columns, m) tm.assert_index_equal(pv_ind.index, m) + def test_pivot_table_dropna_categoricals(self): + # GH 15193 + categories = ['a', 'b', 'c', 'd'] + + df = DataFrame({'A': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'], + 'B': [1, 2, 3, 1, 2, 3, 1, 2, 3], + 'C': range(0, 9)}) + + df['A'] = df['A'].astype('category', ordered=False, + categories=categories) + result_true = df.pivot_table(index='B', columns='A', values='C', + dropna=True) + expected_columns = Series(['a', 'b', 'c'], name='A') + expected_columns = expected_columns.astype('category', ordered=False, + categories=categories) + expected_index = Series([1, 2, 3], name='B') + expected_true = DataFrame([[0.0, 3.0, 6.0], + [1.0, 4.0, 7.0], + [2.0, 5.0, 8.0]], + index=expected_index, + columns=expected_columns,) + tm.assert_frame_equal(expected_true, result_true) + + result_false = df.pivot_table(index='B', columns='A', values='C', + dropna=False) + expected_columns = Series(['a', 'b', 'c', 'd'], name='A') + expected_false = DataFrame([[0.0, 3.0, 6.0, np.NaN], + [1.0, 4.0, 7.0, np.NaN], + [2.0, 5.0, 8.0, np.NaN]], + index=expected_index, + columns=expected_columns,) + tm.assert_frame_equal(expected_false, result_false) + def test_pass_array(self): result = self.data.pivot_table( 'D', index=self.data.A, columns=self.data.C) diff --git a/pandas/tools/pivot.py b/pandas/tools/pivot.py index fc0b92de9316d..6dabb4d94b3a5 100644 --- a/pandas/tools/pivot.py +++ b/pandas/tools/pivot.py @@ -175,6 +175,7 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', if len(index) == 0 and len(columns) > 0: table = table.T + # GH 15193 if isinstance(table, DataFrame) and dropna: table = table.dropna(how='all', axis=1) From adf861689df6b6b9c8e3e1145a798b75f028bede Mon Sep 17 00:00:00 2001 From: Nicholas Ver Halen Date: Thu, 23 Feb 2017 22:15:55 -0600 Subject: [PATCH 3/4] Added whatsnew for issue 15193 --- doc/source/whatsnew/v0.20.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 123fc346441cb..5e38fd3e38d96 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -632,3 +632,4 @@ Bug Fixes - Bug in ``pd.melt()`` where passing a tuple value for ``value_vars`` caused a ``TypeError`` (:issue:`15348`) - Bug in ``.eval()`` which caused multiline evals to fail with local variables not on the first line (:issue:`15342`) - Bug in ``pd.read_msgpack`` which did not allow to load dataframe with an index of type ``CategoricalIndex`` (:issue:`15487`) +- Bug in ``DataFrame.pivot_table`` where ``dropna=True`` would not drop columns from a categorical series to columns (:issue:`15193`) From bf0fdeb2285d778964bf979c5a5c220dd46ee1b3 Mon Sep 17 00:00:00 2001 From: Nicholas Ver Halen Date: Mon, 27 Feb 2017 16:40:34 -0600 Subject: [PATCH 4/4] Added description to code change. --- pandas/tools/pivot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tools/pivot.py b/pandas/tools/pivot.py index 6dabb4d94b3a5..e23beb8332fd4 100644 --- a/pandas/tools/pivot.py +++ b/pandas/tools/pivot.py @@ -175,7 +175,7 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', if len(index) == 0 and len(columns) > 0: table = table.T - # GH 15193 + # GH 15193 Makse sure empty columns are removed if dropna=True if isinstance(table, DataFrame) and dropna: table = table.dropna(how='all', axis=1)