Skip to content

Commit 357729d

Browse files
cscanlinjreback
authored andcommitted
ENH: pivot_table() now accepts most iterables for the values parameter
closes #12017
1 parent fc77caf commit 357729d

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ Other enhancements
200200
- ``Series`` gained an ``is_unique`` attribute (:issue:`11946`)
201201
- ``DataFrame.quantile`` and ``Series.quantile`` now accept ``interpolation`` keyword (:issue:`10174`).
202202
- ``DataFrame.select_dtypes`` now allows the ``np.float16`` typecode (:issue:`11990`)
203+
- ``pivot_table()`` now accepts most iterables for the ``values`` parameter (:issue:`12017`)
203204

204205
.. _whatsnew_0180.api_breaking:
205206

pandas/tools/pivot.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
9494

9595
values_passed = values is not None
9696
if values_passed:
97-
if isinstance(values, (list, tuple)):
97+
if com.is_list_like(values):
9898
values_multi = True
99+
values = list(values)
99100
else:
100101
values_multi = False
101102
values = [values]

pandas/tools/tests/test_pivot.py

+19
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,25 @@ def test_pivot_dtaccessor(self):
736736
index=['X', 'Y'], columns=exp_col)
737737
tm.assert_frame_equal(result, expected)
738738

739+
def test_pivot_table_with_iterator_values(self):
740+
# GH 12017
741+
aggs = {'D': 'sum', 'E': 'mean'}
742+
743+
pivot_values_list = pd.pivot_table(
744+
self.data, index=['A'], values=list(aggs.keys()), aggfunc=aggs,
745+
)
746+
747+
pivot_values_keys = pd.pivot_table(
748+
self.data, index=['A'], values=aggs.keys(), aggfunc=aggs,
749+
)
750+
tm.assert_frame_equal(pivot_values_keys, pivot_values_list)
751+
752+
agg_values_gen = (value for value in aggs.keys())
753+
pivot_values_gen = pd.pivot_table(
754+
self.data, index=['A'], values=agg_values_gen, aggfunc=aggs,
755+
)
756+
tm.assert_frame_equal(pivot_values_gen, pivot_values_list)
757+
739758

740759
class TestCrosstab(tm.TestCase):
741760

0 commit comments

Comments
 (0)