From e2e18eefd93b6e34874736a0ef2e179456b1b179 Mon Sep 17 00:00:00 2001 From: Makarov Andrey Date: Mon, 15 Feb 2016 09:30:33 +0300 Subject: [PATCH 1/8] pivot_table dropna=False drops index level names --- pandas/tools/pivot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tools/pivot.py b/pandas/tools/pivot.py index 7a04847947bf2..bff28a7c86fa3 100644 --- a/pandas/tools/pivot.py +++ b/pandas/tools/pivot.py @@ -126,13 +126,13 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', if not dropna: try: - m = MultiIndex.from_arrays(cartesian_product(table.index.levels)) + m = MultiIndex.from_arrays(cartesian_product(table.index.levels), names=table.index.names) table = table.reindex_axis(m, axis=0) except AttributeError: pass # it's a single level try: - m = MultiIndex.from_arrays(cartesian_product(table.columns.levels)) + m = MultiIndex.from_arrays(cartesian_product(table.columns.levels), names=table.columns.names) table = table.reindex_axis(m, axis=1) except AttributeError: pass # it's a single level or a series From b4c58c6499d890fa860f81b6357f1fad6a26ff92 Mon Sep 17 00:00:00 2001 From: Makarov Andrey Date: Mon, 15 Feb 2016 09:36:14 +0300 Subject: [PATCH 2/8] test level names --- pandas/tools/tests/test_pivot.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pandas/tools/tests/test_pivot.py b/pandas/tools/tests/test_pivot.py index d303f489d9dea..49a17e8660010 100644 --- a/pandas/tools/tests/test_pivot.py +++ b/pandas/tools/tests/test_pivot.py @@ -75,11 +75,10 @@ def test_pivot_table_dropna(self): 'product': {0: 'a', 1: 'b', 2: 'c', 3: 'd'}, 'quantity': {0: 2000000, 1: 500000, 2: 1000000, 3: 1000000}}) - pv_col = df.pivot_table('quantity', 'month', [ - 'customer', 'product'], dropna=False) - pv_ind = df.pivot_table( - 'quantity', ['customer', 'product'], 'month', dropna=False) - + names1, names2 = ['month'], ['customer', 'product'] + pv_col = df.pivot_table('quantity', names1, names2, dropna=False) + pv_ind = df.pivot_table('quantity', names2, names1, dropna=False) + m = MultiIndex.from_tuples([(u('A'), u('a')), (u('A'), u('b')), (u('A'), u('c')), @@ -92,7 +91,11 @@ def test_pivot_table_dropna(self): (u('C'), u('b')), (u('C'), u('c')), (u('C'), u('d'))]) - + + assert_equal(pv_col.index.names, names1) + assert_equal(pv_ind.columns.names, names1) + assert_equal(pv_col.columns.names, names2) + assert_equal(pv_ind.index.names, names2) assert_equal(pv_col.columns.values, m.values) assert_equal(pv_ind.index.values, m.values) From 472cbd8237e71c791e773acdb1ef17bc597e563b Mon Sep 17 00:00:00 2001 From: Makarov Andrey Date: Tue, 16 Feb 2016 10:18:54 +0300 Subject: [PATCH 3/8] Update test_pivot.py --- pandas/tools/tests/test_pivot.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/pandas/tools/tests/test_pivot.py b/pandas/tools/tests/test_pivot.py index 49a17e8660010..30847fc50478f 100644 --- a/pandas/tools/tests/test_pivot.py +++ b/pandas/tools/tests/test_pivot.py @@ -75,10 +75,11 @@ def test_pivot_table_dropna(self): 'product': {0: 'a', 1: 'b', 2: 'c', 3: 'd'}, 'quantity': {0: 2000000, 1: 500000, 2: 1000000, 3: 1000000}}) - names1, names2 = ['month'], ['customer', 'product'] - pv_col = df.pivot_table('quantity', names1, names2, dropna=False) - pv_ind = df.pivot_table('quantity', names2, names1, dropna=False) - + pv_col = df.pivot_table('quantity', 'month', [ + 'customer', 'product'], dropna=False) + pv_ind = df.pivot_table( + 'quantity', ['customer', 'product'], 'month', dropna=False) + m = MultiIndex.from_tuples([(u('A'), u('a')), (u('A'), u('b')), (u('A'), u('c')), @@ -91,14 +92,21 @@ def test_pivot_table_dropna(self): (u('C'), u('b')), (u('C'), u('c')), (u('C'), u('d'))]) - - assert_equal(pv_col.index.names, names1) - assert_equal(pv_ind.columns.names, names1) - assert_equal(pv_col.columns.names, names2) - assert_equal(pv_ind.index.names, names2) + assert_equal(pv_col.columns.values, m.values) assert_equal(pv_ind.index.values, m.values) + idx = pd.MultiIndex.from_tuples([(1000000, 201308), (1000000, 201310)], + names=('quantity', 'month')) + tup = (("B", "c"), ("B", "d"), ("C", "c"), ("C", "d")) + clm = pd.MultiIndex.from_tuples(tup, names=('customer', 'product')) + pv = pd.DataFrame([[50000, np.nan, np.nan, np.nan], + [np.nan, np.nan, np.nan, 30000]], + index=idx, columns=clm) + pv_gen = df[2:].pivot_table('amount', ['quantity', 'month'], + ['customer', 'product'], dropna=False) + tm.assert_frame_equal(pv, pv_gen) + def test_pass_array(self): result = self.data.pivot_table( 'D', index=self.data.A, columns=self.data.C) From 539e74e4547824b3dc6b44fadd49ad1d0534b5a7 Mon Sep 17 00:00:00 2001 From: Makarov Andrey Date: Tue, 16 Feb 2016 10:21:32 +0300 Subject: [PATCH 4/8] pep8: 79 char. --- pandas/tools/pivot.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/tools/pivot.py b/pandas/tools/pivot.py index bff28a7c86fa3..4b76bb5a67fc3 100644 --- a/pandas/tools/pivot.py +++ b/pandas/tools/pivot.py @@ -126,13 +126,15 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', if not dropna: try: - m = MultiIndex.from_arrays(cartesian_product(table.index.levels), names=table.index.names) + m = MultiIndex.from_arrays(cartesian_product(table.index.levels), + names=table.index.names) table = table.reindex_axis(m, axis=0) except AttributeError: - pass # it's a single level + pass # it's a single level try: - m = MultiIndex.from_arrays(cartesian_product(table.columns.levels), names=table.columns.names) + m = MultiIndex.from_arrays(cartesian_product(table.columns.levels), + names=table.columns.names) table = table.reindex_axis(m, axis=1) except AttributeError: pass # it's a single level or a series From 5428d28a4a68576d4fc82c21d0cad49573e44f4c Mon Sep 17 00:00:00 2001 From: Makarov Andrey Date: Tue, 16 Feb 2016 10:31:32 +0300 Subject: [PATCH 5/8] issue 12133 --- doc/source/whatsnew/v0.18.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 75a38544fb8eb..72d2692df01bd 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -540,3 +540,4 @@ of columns didn't match the number of series provided (:issue:`12039`). - Bug in ``.loc`` setitem indexer preventing the use of a TZ-aware DatetimeIndex (:issue:`12050`) - Big in ``.style`` indexes and multi-indexes not appearing (:issue:`11655`) +- Bug in ``pivot_table`` where ``dropna`` argument drops columns and index level names (:issue:`12133`) From 8f9697197d1423571b27d889100db9e5ddd36baa Mon Sep 17 00:00:00 2001 From: Makarov Andrey Date: Tue, 16 Feb 2016 13:53:48 +0300 Subject: [PATCH 6/8] formatting --- 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 4b76bb5a67fc3..0852d0114d697 100644 --- a/pandas/tools/pivot.py +++ b/pandas/tools/pivot.py @@ -130,7 +130,7 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', names=table.index.names) table = table.reindex_axis(m, axis=0) except AttributeError: - pass # it's a single level + pass # it's a single level try: m = MultiIndex.from_arrays(cartesian_product(table.columns.levels), From bff8b21a108f198b82915101783305ecf2c2ce27 Mon Sep 17 00:00:00 2001 From: Makarov Andrey Date: Tue, 16 Feb 2016 18:06:32 +0300 Subject: [PATCH 7/8] Update v0.18.0.txt --- doc/source/whatsnew/v0.18.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 72d2692df01bd..67585d156a090 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -540,4 +540,4 @@ of columns didn't match the number of series provided (:issue:`12039`). - Bug in ``.loc`` setitem indexer preventing the use of a TZ-aware DatetimeIndex (:issue:`12050`) - Big in ``.style`` indexes and multi-indexes not appearing (:issue:`11655`) -- Bug in ``pivot_table`` where ``dropna`` argument drops columns and index level names (:issue:`12133`) +- Bug in ``.pivot_table()`` where ``dropna`` argument drops columns and index level names (:issue:`12133`) From feb35bbe8da09917e25a118ca34521d1ba2e5e3b Mon Sep 17 00:00:00 2001 From: Makarov Andrey Date: Tue, 16 Feb 2016 18:09:25 +0300 Subject: [PATCH 8/8] reference issue --- pandas/tools/tests/test_pivot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tools/tests/test_pivot.py b/pandas/tools/tests/test_pivot.py index 30847fc50478f..93d4802b7d622 100644 --- a/pandas/tools/tests/test_pivot.py +++ b/pandas/tools/tests/test_pivot.py @@ -96,6 +96,7 @@ def test_pivot_table_dropna(self): assert_equal(pv_col.columns.values, m.values) assert_equal(pv_ind.index.values, m.values) + #issue 12133, dropna=False arg drops index level names idx = pd.MultiIndex.from_tuples([(1000000, 201308), (1000000, 201310)], names=('quantity', 'month')) tup = (("B", "c"), ("B", "d"), ("C", "c"), ("C", "d"))