Must produce aggregated value. I swear that I am
<p>consider the <code>pd.Series</code> <code>s</code></p>

<pre><code>a = np.arange(4)
mux = pd.MultiIndex.from_product([list('ab'), list('xy')])
s = pd.Series([a] * 4, mux)
print(s)

a  x    [0, 1, 2, 3]
   y    [0, 1, 2, 3]
b  x    [0, 1, 2, 3]
   y    [0, 1, 2, 3]
dtype: object
</code></pre>

<hr>

<p><strong><em>problem</em></strong><br>
each element of <code>s</code> is a <code>numpy.array</code>.  when I try to sum within groups, I get an error because the groupby function expects the result to be scalar... (I'm guessing)</p>

<pre><code>s.groupby(level=0).sum()
</code></pre>

<blockquote>
<pre><code>Exception                                 Traceback (most recent call last)
&lt;ipython-input-627-c5b3bf6890ea&gt; in &lt;module&gt;()
----&gt; 1 s.groupby(level=0).sum()

C:\Anaconda2\lib\site-packages\pandas\core\groupby.pyc in f(self)
    101             raise SpecificationError(str(e))
    102         except Exception:
--&gt; 103             result = self.aggregate(lambda x: npfunc(x, axis=self.axis))
    104             if _convert:
    105                 result = result._convert(datetime=True)

C:\Anaconda2\lib\site-packages\pandas\core\groupby.pyc in aggregate(self, func_or_funcs, *args, **kwargs)
   2584                 return self._python_agg_general(func_or_funcs, *args, **kwargs)
   2585             except Exception:
-&gt; 2586                 result = self._aggregate_named(func_or_funcs, *args, **kwargs)
   2587 
   2588             index = Index(sorted(result), name=self.grouper.names[0])

C:\Anaconda2\lib\site-packages\pandas\core\groupby.pyc in _aggregate_named(self, func, *args, **kwargs)
   2704             output = func(group, *args, **kwargs)
   2705             if isinstance(output, (Series, Index, np.ndarray)):
-&gt; 2706                 raise Exception('Must produce aggregated value')
   2707             result[name] = self._try_cast(output, group)
   2708 

Exception: Must produce aggregated value
</code></pre>
</blockquote>

<hr>

<p><strong><em>work around</em></strong><br>
when I use <code>apply</code> with <code>np.sum</code>, it works fine.</p>

<pre><code>s.groupby(level=0).apply(np.sum)

a    [0, 2, 4, 6]
b    [0, 2, 4, 6]
dtype: object
</code></pre>

<hr>

<p><strong><em>question</em></strong><br>
is there an elegant way to handle this?</p>

<hr>

<p><strong><em>real problem</em></strong><br>
I actually want to use <code>agg</code> in this way</p>

<pre><code>s.groupby(level=0).agg(['sum', 'prod'])
</code></pre>

<p>but it fails in the same way.<br>
the only way to get this is to</p>

<pre><code>pd.concat([g.apply(np.sum), g.apply(np.prod)],
          axis=1, keys=['sum', 'prod'])
</code></pre>

<p><a href="http://i.stack.imgur.com/1108f.png" rel="nofollow"><img src="http://i.stack.imgur.com/1108f.png" alt="enter image description here"></a></p>

<p>but this doesn't generalize well to longer lists of transforms.</p>

<p><a href="http://stackoverflow.com/questions/16975318/pandas-aggregate-when-column-contains-numpy-arrays">from this well explained answer</a> you could transform your ndarray to list because pandas seems to be checking if the output is a ndarray and this is why you are getting this error raised :</p>

<pre><code>s.groupby(level=0).agg({"sum": lambda x: list(x.sum()), "prod":lambda x: list(x.prod())})
</code></pre>

<p>Out[249]: </p>

<pre><code>            sum          prod
a  [0, 2, 4, 6]  [0, 1, 4, 9]
b  [0, 2, 4, 6]  [0, 1, 4, 9]
</code></pre>

python pandas