From 3d1ed79fe3186eecd118f4a6f38b5e3e3b8454dc Mon Sep 17 00:00:00 2001 From: Christopher Scanlin Date: Sun, 24 Jan 2016 16:03:21 -0800 Subject: [PATCH 1/2] change conditional to is_list_like, add tests for pivot values --- pandas/tools/pivot.py | 3 ++- pandas/tools/tests/test_pivot.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pandas/tools/pivot.py b/pandas/tools/pivot.py index 7a04847947bf2..120a30199e522 100644 --- a/pandas/tools/pivot.py +++ b/pandas/tools/pivot.py @@ -94,8 +94,9 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', values_passed = values is not None if values_passed: - if isinstance(values, (list, tuple)): + if com.is_list_like(values): values_multi = True + values = list(values) else: values_multi = False values = [values] diff --git a/pandas/tools/tests/test_pivot.py b/pandas/tools/tests/test_pivot.py index d303f489d9dea..c62291f9e83aa 100644 --- a/pandas/tools/tests/test_pivot.py +++ b/pandas/tools/tests/test_pivot.py @@ -736,6 +736,24 @@ def test_pivot_dtaccessor(self): index=['X', 'Y'], columns=exp_col) tm.assert_frame_equal(result, expected) + def test_pivot_table_with_iterator_values(self): + aggs = {'D': 'sum', 'E': 'mean'} + + pivot_values_list = pd.pivot_table( + self.data, index=['A'], values=list(aggs.keys()), aggfunc=aggs, + ) + + pivot_values_keys = pd.pivot_table( + self.data, index=['A'], values=aggs.keys(), aggfunc=aggs, + ) + tm.assert_frame_equal(pivot_values_keys, pivot_values_list) + + agg_values_gen = (value for value in aggs.keys()) + pivot_values_gen = pd.pivot_table( + self.data, index=['A'], values=agg_values_gen, aggfunc=aggs, + ) + tm.assert_frame_equal(pivot_values_gen, pivot_values_list) + class TestCrosstab(tm.TestCase): From 034903dd1378d5e4016e3f9c089012408419f462 Mon Sep 17 00:00:00 2001 From: Christopher Scanlin Date: Sat, 30 Jan 2016 19:28:25 -0800 Subject: [PATCH 2/2] add note about pivot_table() enhancement in whatsnew. add PR reference to test --- doc/source/whatsnew/v0.18.0.txt | 1 + pandas/tools/tests/test_pivot.py | 1 + 2 files changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 75a38544fb8eb..bbb86571aa51e 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -152,6 +152,7 @@ Other enhancements - ``Series`` gained an ``is_unique`` attribute (:issue:`11946`) - ``DataFrame.quantile`` and ``Series.quantile`` now accept ``interpolation`` keyword (:issue:`10174`). - ``DataFrame.select_dtypes`` now allows the ``np.float16`` typecode (:issue:`11990`) +- ``pivot_table()`` now accepts most iterables for the ``values`` parameter (:issue:`12017`) .. _whatsnew_0180.enhancements.rounding: diff --git a/pandas/tools/tests/test_pivot.py b/pandas/tools/tests/test_pivot.py index c62291f9e83aa..2720350afb9fe 100644 --- a/pandas/tools/tests/test_pivot.py +++ b/pandas/tools/tests/test_pivot.py @@ -737,6 +737,7 @@ def test_pivot_dtaccessor(self): tm.assert_frame_equal(result, expected) def test_pivot_table_with_iterator_values(self): + # GH 12017 aggs = {'D': 'sum', 'E': 'mean'} pivot_values_list = pd.pivot_table(