From d6a6036d76d425ad87841c31bf8f2de3e29d5382 Mon Sep 17 00:00:00 2001 From: cmazzullo Date: Mon, 13 Jun 2016 19:06:18 -0400 Subject: [PATCH 1/6] BUG: df.pivot_table: margins_name is ignored when there aggfunc is list #13354 --- doc/source/whatsnew/v0.18.1.txt | 1 + pandas/tools/pivot.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.18.1.txt b/doc/source/whatsnew/v0.18.1.txt index 51982c42499ff..574dc8edd2385 100644 --- a/doc/source/whatsnew/v0.18.1.txt +++ b/doc/source/whatsnew/v0.18.1.txt @@ -682,3 +682,4 @@ Bug Fixes - Bug in ``pd.to_numeric()`` with ``Index`` returns ``np.ndarray``, rather than ``Index`` (:issue:`12777`) - Bug in ``pd.to_numeric()`` with datetime-like may raise ``TypeError`` (:issue:`12777`) - Bug in ``pd.to_numeric()`` with scalar raises ``ValueError`` (:issue:`12777`) +- Bug in ``pd.pivot_table()`` where margins_name is ignored when aggfunc is a list (:issue:`13354`) diff --git a/pandas/tools/pivot.py b/pandas/tools/pivot.py index a4e6cc404a457..e1405bc9e6add 100644 --- a/pandas/tools/pivot.py +++ b/pandas/tools/pivot.py @@ -86,7 +86,7 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', table = pivot_table(data, values=values, index=index, columns=columns, fill_value=fill_value, aggfunc=func, - margins=margins) + margins=margins, margins_name=margins_name) pieces.append(table) keys.append(func.__name__) return concat(pieces, keys=keys, axis=1) From 0dcb78f7db32c8bd46964372fbc2d7e9315e33f0 Mon Sep 17 00:00:00 2001 From: cmazzullo Date: Mon, 13 Jun 2016 23:59:09 -0400 Subject: [PATCH 2/6] Switched documentation to v0.18.2 --- doc/source/whatsnew/v0.18.1.txt | 1 - doc/source/whatsnew/v0.18.2.txt | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.18.1.txt b/doc/source/whatsnew/v0.18.1.txt index 574dc8edd2385..51982c42499ff 100644 --- a/doc/source/whatsnew/v0.18.1.txt +++ b/doc/source/whatsnew/v0.18.1.txt @@ -682,4 +682,3 @@ Bug Fixes - Bug in ``pd.to_numeric()`` with ``Index`` returns ``np.ndarray``, rather than ``Index`` (:issue:`12777`) - Bug in ``pd.to_numeric()`` with datetime-like may raise ``TypeError`` (:issue:`12777`) - Bug in ``pd.to_numeric()`` with scalar raises ``ValueError`` (:issue:`12777`) -- Bug in ``pd.pivot_table()`` where margins_name is ignored when aggfunc is a list (:issue:`13354`) diff --git a/doc/source/whatsnew/v0.18.2.txt b/doc/source/whatsnew/v0.18.2.txt index 105194e504f45..29c872670d779 100644 --- a/doc/source/whatsnew/v0.18.2.txt +++ b/doc/source/whatsnew/v0.18.2.txt @@ -383,3 +383,4 @@ Bug Fixes - Bug in ``Categorical.remove_unused_categories()`` changes ``.codes`` dtype to platform int (:issue:`13261`) +- Bug in ``pd.pivot_table()`` where margins_name is ignored when aggfunc is a list (:issue:`13354`) From 669931f1b4f4d5bd8f7e0fee0c8511d7fa7208a2 Mon Sep 17 00:00:00 2001 From: cmazzullo Date: Wed, 15 Jun 2016 18:20:50 -0400 Subject: [PATCH 3/6] Added unit test for pivot_table margins_name when aggfunc is a list --- pandas/tools/tests/test_pivot.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pandas/tools/tests/test_pivot.py b/pandas/tools/tests/test_pivot.py index 82feaae13f771..055627c52c78b 100644 --- a/pandas/tools/tests/test_pivot.py +++ b/pandas/tools/tests/test_pivot.py @@ -779,6 +779,13 @@ def test_pivot_table_with_iterator_values(self): ) tm.assert_frame_equal(pivot_values_gen, pivot_values_list) + def test_pivot_table_margins_name_with_aggfunc_list(self): + # GH 13354 + margins_name = 'Weekly' + table = self.data.pivot_table(index='A', columns='B', margins=True, + margins_name=margins_name, aggfunc=[np.mean, max]) + self.assertEqual(table[:][:].index[2], margins_name) + class TestCrosstab(tm.TestCase): From 98ab8be541923c74ba9c4f032cd579a36fe0ce48 Mon Sep 17 00:00:00 2001 From: cmazzullo Date: Fri, 17 Jun 2016 20:08:03 -0400 Subject: [PATCH 4/6] Added doublequotes to variable names --- doc/source/whatsnew/v0.18.2.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.18.2.txt b/doc/source/whatsnew/v0.18.2.txt index 29c872670d779..c4670b8c03e83 100644 --- a/doc/source/whatsnew/v0.18.2.txt +++ b/doc/source/whatsnew/v0.18.2.txt @@ -383,4 +383,4 @@ Bug Fixes - Bug in ``Categorical.remove_unused_categories()`` changes ``.codes`` dtype to platform int (:issue:`13261`) -- Bug in ``pd.pivot_table()`` where margins_name is ignored when aggfunc is a list (:issue:`13354`) +- Bug in ``pd.pivot_table()`` where ``margins_name`` is ignored when ``aggfunc`` is a list (:issue:`13354`) From b4b09bfcb025bb1b6794cc33886eeee9bd6348c7 Mon Sep 17 00:00:00 2001 From: cmazzullo Date: Fri, 17 Jun 2016 20:08:33 -0400 Subject: [PATCH 5/6] Compared whole dataframes in test_pivot_table_margins_name_with_aggfunc_list --- pandas/tools/tests/test_pivot.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pandas/tools/tests/test_pivot.py b/pandas/tools/tests/test_pivot.py index 055627c52c78b..3e8a86f03acc7 100644 --- a/pandas/tools/tests/test_pivot.py +++ b/pandas/tools/tests/test_pivot.py @@ -781,10 +781,19 @@ def test_pivot_table_with_iterator_values(self): def test_pivot_table_margins_name_with_aggfunc_list(self): # GH 13354 - margins_name = 'Weekly' - table = self.data.pivot_table(index='A', columns='B', margins=True, - margins_name=margins_name, aggfunc=[np.mean, max]) - self.assertEqual(table[:][:].index[2], margins_name) + margins_name='Weekly' + costs = pd.DataFrame({'item':['bacon','cheese','bacon','cheese'], + 'cost':[2.5,4.5,3.2,3.3], + 'day':['M','M','T','T']}) + table = costs.pivot_table(index="item",columns="day",margins=True, + margins_name=margins_name,aggfunc=[np.mean,max]) + ix = pd.Index(['bacon', 'cheese', margins_name], dtype='object', name='item') + tups = [('mean', 'cost', 'M'), ('mean', 'cost', 'T'), + ('mean', 'cost', margins_name), ('max', 'cost', 'M'), + ('max', 'cost', 'T'), ('max', 'cost', margins_name)] + cols = pd.MultiIndex.from_tuples(tups, names=[None, None, 'day']) + expected = pd.DataFrame(table.values, index=ix, columns=cols) + tm.assert_frame_equal(table, expected) class TestCrosstab(tm.TestCase): From 6017e252e81e2754e21b04e71d6c4226bf410016 Mon Sep 17 00:00:00 2001 From: cmazzullo Date: Sat, 18 Jun 2016 00:51:40 -0400 Subject: [PATCH 6/6] Fixed up whitespace for pep8 --- pandas/tools/tests/test_pivot.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/pandas/tools/tests/test_pivot.py b/pandas/tools/tests/test_pivot.py index 3e8a86f03acc7..7ec4018d301af 100644 --- a/pandas/tools/tests/test_pivot.py +++ b/pandas/tools/tests/test_pivot.py @@ -781,13 +781,19 @@ def test_pivot_table_with_iterator_values(self): def test_pivot_table_margins_name_with_aggfunc_list(self): # GH 13354 - margins_name='Weekly' - costs = pd.DataFrame({'item':['bacon','cheese','bacon','cheese'], - 'cost':[2.5,4.5,3.2,3.3], - 'day':['M','M','T','T']}) - table = costs.pivot_table(index="item",columns="day",margins=True, - margins_name=margins_name,aggfunc=[np.mean,max]) - ix = pd.Index(['bacon', 'cheese', margins_name], dtype='object', name='item') + margins_name = 'Weekly' + costs = pd.DataFrame( + {'item': ['bacon', 'cheese', 'bacon', 'cheese'], + 'cost': [2.5, 4.5, 3.2, 3.3], + 'day': ['M', 'M', 'T', 'T']} + ) + table = costs.pivot_table( + index="item", columns="day", margins=True, + margins_name=margins_name, aggfunc=[np.mean, max] + ) + ix = pd.Index( + ['bacon', 'cheese', margins_name], dtype='object', name='item' + ) tups = [('mean', 'cost', 'M'), ('mean', 'cost', 'T'), ('mean', 'cost', margins_name), ('max', 'cost', 'M'), ('max', 'cost', 'T'), ('max', 'cost', margins_name)]