Skip to content

Commit 4074957

Browse files
RenzoBertocchijreback
authored andcommitted
BUG: Bug on pivot_table with margins and dict aggfunc (GH8349)
1 parent c213523 commit 4074957

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

doc/source/v0.15.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ Performance
856856

857857
Bug Fixes
858858
~~~~~~~~~
859-
859+
- Bug in pivot_table, when using margins and a dict aggfunc (:issue:`8349`)
860860
- Bug in ``read_csv`` where ``squeeze=True`` would return a view (:issue:`8217`)
861861
- Bug in checking of table name in ``read_sql`` in certain cases (:issue:`7826`).
862862
- Bug in ``DataFrame.groupby`` where ``Grouper`` does not recognize level when frequency is specified (:issue:`7885`)

pandas/tools/pivot.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ def _compute_grand_margin(data, values, aggfunc):
207207
try:
208208
if isinstance(aggfunc, compat.string_types):
209209
grand_margin[k] = getattr(v, aggfunc)()
210+
elif isinstance(aggfunc, dict):
211+
if isinstance(aggfunc[k], compat.string_types):
212+
grand_margin[k] = getattr(v, aggfunc[k])()
213+
else:
214+
grand_margin[k] = aggfunc[k](v)
210215
else:
211216
grand_margin[k] = aggfunc(v)
212217
except TypeError:

pandas/tools/tests/test_pivot.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,34 @@ def _check_output(res, col, index=['A', 'B'], columns=['C']):
266266
gmarg = table[item]['All', '']
267267
self.assertEqual(gmarg, self.data[item].mean())
268268

269+
# issue number #8349: pivot_table with margins and dictionary aggfunc
270+
271+
df=DataFrame([ {'JOB':'Worker','NAME':'Bob' ,'YEAR':2013,'MONTH':12,'DAYS': 3,'SALARY': 17},
272+
{'JOB':'Employ','NAME':'Mary','YEAR':2013,'MONTH':12,'DAYS': 5,'SALARY': 23},
273+
{'JOB':'Worker','NAME':'Bob' ,'YEAR':2014,'MONTH': 1,'DAYS':10,'SALARY':100},
274+
{'JOB':'Worker','NAME':'Bob' ,'YEAR':2014,'MONTH': 1,'DAYS':11,'SALARY':110},
275+
{'JOB':'Employ','NAME':'Mary','YEAR':2014,'MONTH': 1,'DAYS':15,'SALARY':200},
276+
{'JOB':'Worker','NAME':'Bob' ,'YEAR':2014,'MONTH': 2,'DAYS': 8,'SALARY': 80},
277+
{'JOB':'Employ','NAME':'Mary','YEAR':2014,'MONTH': 2,'DAYS': 5,'SALARY':190} ])
278+
279+
df=df.set_index(['JOB','NAME','YEAR','MONTH'],drop=False,append=False)
280+
281+
rs=df.pivot_table( index=['JOB','NAME'],
282+
columns=['YEAR','MONTH'],
283+
values=['DAYS','SALARY'],
284+
aggfunc={'DAYS':'mean','SALARY':'sum'},
285+
margins=True)
286+
287+
ex=df.pivot_table(index=['JOB','NAME'],columns=['YEAR','MONTH'],values=['DAYS'],aggfunc='mean',margins=True)
288+
289+
tm.assert_frame_equal(rs['DAYS'], ex['DAYS'])
290+
291+
ex=df.pivot_table(index=['JOB','NAME'],columns=['YEAR','MONTH'],values=['SALARY'],aggfunc='sum',margins=True)
292+
293+
tm.assert_frame_equal(rs['SALARY'], ex['SALARY'])
294+
295+
296+
269297
def test_pivot_integer_columns(self):
270298
# caused by upstream bug in unstack
271299

0 commit comments

Comments
 (0)