wesm/pandas — Issue #644: "Applying multiple functions at once" example from the docs broken
Author: elpres
Created: 2012-01-18 14:10:24+00:00
State: open

Using `agg` with a list of functions as parameter on a `groupby` object doesn't work as described:

df = DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
'C' : randn(8), 'D' : randn(8)})
grouped = df.groupby('A')
grouped.agg([np.sum, np.mean, np.std])

leads to this (with 0.6.1 stable and master):

---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
/home/ilja/env/ in ()
----> 1 grouped.agg([np.sum, np.mean, np.std])

/usr/lib/python2.7/site-packages/pandas/core/groupby.pyc in agg(self, func, *args, **kwargs)
295 See docstring for aggregate
296 """
--> 297 return self.aggregate(func, *args, **kwargs)
298
299 def _get_names(self):

/usr/lib/python2.7/site-packages/pandas/core/groupby.pyc in aggregate(self, arg, *args, **kwargs)
954 return self._python_agg_general(arg, *args, **kwargs)
955 else:
--> 956 result = self._aggregate_generic(arg, *args, **kwargs)
957
958 if not self.as_index:

/usr/lib/python2.7/site-packages/pandas/core/groupby.pyc in _aggregate_generic(self, func, *args, **kwargs)
982 result[name] = func(data, *args, **kwargs)
983 except Exception:
--> 984 return self._aggregate_item_by_item(func, *args, **kwargs)
985 else:
986 for name in self.indices:

/usr/lib/python2.7/site-packages/pandas/core/groupby.pyc in _aggregate_item_by_item(self, func, *args, **kwargs)
1027 result_columns = result_columns.drop(cannot_agg)
1028
-> 1029 return DataFrame(result, columns=result_columns)
1030
1031 def _wrap_aggregated_output(self, output, mask):

/usr/lib/python2.7/site-packages/pandas/core/frame.pyc in __init__(self, data, index, columns, dtype, copy)
191 mgr = self._init_mgr(data, index, columns, dtype=dtype, copy=copy)
192 elif isinstance(data, dict):
--> 193 mgr = self._init_dict(data, index, columns, dtype=dtype)
194 elif isinstance(data, ma.MaskedArray):
195 mask = ma.getmaskarray(data)

/usr/lib/python2.7/site-packages/pandas/core/frame.pyc in _init_dict(self, data, index, columns, dtype)
260
261 # consolidate for now

--> 262 mgr = BlockManager(blocks, axes)
263 return mgr.consolidate()
264

/usr/lib/python2.7/site-packages/pandas/core/internals.pyc in __init__(self, blocks, axes, do_integrity_check)
274
275 if do_integrity_check:
--> 276 self._verify_integrity()
277
278 @property

/usr/lib/python2.7/site-packages/pandas/core/internals.pyc in _verify_integrity(self)
353 mgr_shape = self.shape
354 for block in self.blocks:
--> 355 assert(block.values.shape[1:] == mgr_shape[1:])
356 tot_items = sum(len(x.items) for x in self.blocks)
357 assert(len(self.items) == tot_items)

AssertionError: