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/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..2720350afb9fe 100644 --- a/pandas/tools/tests/test_pivot.py +++ b/pandas/tools/tests/test_pivot.py @@ -736,6 +736,25 @@ 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): + # GH 12017 + 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):