Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Bug on pivot_table with margins and dict aggfunc #8349
Comments
|
The issue is that your aggregation function applied different to different columns, your example will work if you do this:
So right now this just silently bombs on the aggregation function (as its a dict) for the grand margins and then raises a later error. So the choices are:
So calling this an error-reportng issue |
jreback
added Error Reporting Good as first PR Reshaping
labels
Sep 22, 2014
jreback
added this to the
0.15.1
milestone
Sep 22, 2014
|
Ok but seems to works well if i change "pandas/tools/pivot.py" form:
to:
Is it not a solution? |
|
I think the grand margin is wrong no? Why don't you show the output as you envision |
|
Seems good:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
hmm, yeh that looks ok. pls submit a pull-request (with tests!) |
RenzoBertocchi
referenced
this issue
Sep 23, 2014
Closed
Bug on pivot_table with margins and dict aggfunc #8354
jreback
added Bug and removed Error Reporting
labels
Sep 23, 2014
|
Pull-request done |
RenzoBertocchi
closed this
Sep 29, 2014
jreback
reopened this
Sep 29, 2014
|
@RenzoBertocchi when the PR is merged this issue will be automatically closed. |
jreback
modified the milestone: 0.15.0, 0.15.1
Sep 29, 2014
|
closed by #8354 |
RenzoBertocchi commentedSep 22, 2014
I think there is a bug on pandas 0.14.1 pivot_table using dictionary aggfunc and margins.
Test code:
df=pandas.DataFrame([{'JOB':'Worker','NAME':'Bob' ,'YEAR':2013,'MONTH':12,'DAYS': 3,'SALARY': 17},
{'JOB':'Employ','NAME':'Mary','YEAR':2013,'MONTH':12,'DAYS': 5,'SALARY': 23},
{'JOB':'Worker','NAME':'Bob' ,'YEAR':2014,'MONTH': 1,'DAYS':10,'SALARY':100},
{'JOB':'Worker','NAME':'Bob' ,'YEAR':2014,'MONTH': 1,'DAYS':11,'SALARY':110},
{'JOB':'Employ','NAME':'Mary','YEAR':2014,'MONTH': 1,'DAYS':15,'SALARY':200},
{'JOB':'Worker','NAME':'Bob' ,'YEAR':2014,'MONTH': 2,'DAYS': 8,'SALARY': 80},
{'JOB':'Employ','NAME':'Mary','YEAR':2014,'MONTH': 2,'DAYS': 5,'SALARY':190}
])
df=df.set_index(['JOB','NAME','YEAR','MONTH'],drop=False,append=False)df=df.pivot_table(index=['JOB','NAME'],columns=['YEAR','MONTH'],values=['DAYS','SALARY'],aggfunc={'DAYS':'mean','SALARY':'sum'})All works fine but raise error using margins:
df=df.pivot_table(index=['JOB','NAME'],columns=['YEAR','MONTH'],values=['DAYS','SALARY'],aggfunc={'DAYS':'mean','SALARY':'sum'},margins=True)df=df.pivot_table(index=['JOB','NAME'],columns=['YEAR','MONTH'],values=['DAYS','SALARY'],aggfunc={'DAYS':'mean','SALARY':'sum'},margins=True)
File "/usr/local/lib/python2.7/site-packages/pandas-0.14.1-py2.7-linux-x86_64.egg/pandas/util/decorators.py", line 60, in wrapper
return func(_args, *_kwargs)
File "/usr/local/lib/python2.7/site-packages/pandas-0.14.1-py2.7-linux-x86_64.egg/pandas/util/decorators.py", line 60, in wrapper
return func(_args, *_kwargs)
File "/usr/local/lib/python2.7/site-packages/pandas-0.14.1-py2.7-linux-x86_64.egg/pandas/tools/pivot.py", line 147, in pivot_table
cols=columns, aggfunc=aggfunc)
File "/usr/local/lib/python2.7/site-packages/pandas-0.14.1-py2.7-linux-x86_64.egg/pandas/tools/pivot.py", line 191, in _add_margins
row_margin[k] = grand_margin[k[0]]
KeyError: 'SALARY'