Skip to content

Instantly share code, notes, and snippets.

@aterrel
Created November 11, 2015 03:29
Show Gist options
  • Save aterrel/842f8e92f119f58eb8af to your computer and use it in GitHub Desktop.
Save aterrel/842f8e92f119f58eb8af to your computer and use it in GitHub Desktop.
possible pandas bug on agg via dict
In [13]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:In [1]: df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
: ...: 'foo', 'bar', 'foo', 'foo'],
: ...: 'B' : ['one', 'one', 'two', 'three',
: ...: 'two', 'two', 'one', 'three'],
: ...: 'C' : np.random.randn(8),
: ...: 'D' : np.random.randn(8)})
:--
In [14]: df
Out[14]:
A B C D
0 foo one -0.508647 -1.037872
1 bar one 0.232630 -1.498335
2 foo two -0.294682 0.506412
3 bar three -0.254762 -0.229931
4 foo two 0.374847 -0.803666
5 bar two 0.946507 -0.855568
6 foo one -0.358013 0.433566
7 foo three 0.555966 0.525245
In [15]: grouped = df.groupby(['A', 'B'])
In [19]: grouped['D'].agg({'r':np.sum, 'r2':np.mean})
Out[19]:
r r2
A
bar -2.583835 -0.861278
foo -0.376315 -0.075263
In [20]: grouped[['D','C']].agg({'r':np.sum, 'r2':np.mean})
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-20-f26edb6d56b9> in <module>()
----> 1 grouped[['D','C']].agg({'r':np.sum, 'r2':np.mean})
/Users/aterrel/miniconda/envs/boldmetrics/lib/python2.7/site-packages/pandas/core/groupby.pyc in agg(self, func, *args, **kwargs)
683 @Appender(_agg_doc)
684 def agg(self, func, *args, **kwargs):
--> 685 return self.aggregate(func, *args, **kwargs)
686
687 def _iterate_slices(self):
/Users/aterrel/miniconda/envs/boldmetrics/lib/python2.7/site-packages/pandas/core/groupby.pyc in aggregate(self, arg, *args, **kwargs)
2657 subset = obj
2658 if isinstance(subset, DataFrame):
-> 2659 raise NotImplementedError("Aggregating on a DataFrame is "
2660 "not supported")
2661
NotImplementedError: Aggregating on a DataFrame is not supported
In [21]: grouped[['D','C']].agg([np.sum, np.mean])
Out[21]:
D C
sum mean sum mean
A
bar -2.583835 -0.861278 0.924375 0.308125
foo -0.376315 -0.075263 -0.230529 -0.046106
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment