def filter(self, func, dropna=True, *args, **kwargs):  
indices = []<EOL>obj = self._selected_obj<EOL>gen = self.grouper.get_iterator(obj, axis=self.axis)<EOL>for name, group in gen:<EOL><INDENT>object.__setattr__(group, '<STR_LIT:name>', name)<EOL>res = func(group, *args, **kwargs)<EOL>try:<EOL><INDENT>res = res.squeeze()<EOL><DEDENT>except AttributeError:  <EOL><INDENT>pass<EOL><DEDENT>if is_bool(res) or (is_scalar(res) and isna(res)):<EOL><INDENT>if res and notna(res):<EOL><INDENT>indices.append(self._get_index(name))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>"<EOL>"<STR_LIT>" %<EOL>type(res).__name__)<EOL><DEDENT><DEDENT>return self._apply_filter(indices, dropna)<EOL>
Return a copy of a DataFrame excluding elements from groups that
do not satisfy the boolean criterion specified by func.

Parameters
----------
f : function
    Function to apply to each subframe. Should return True or False.
dropna : Drop groups that do not pass the filter. True by default;
    if False, groups that evaluate False are filled with NaNs.

Returns
-------
filtered : DataFrame

Notes
-----
Each subframe is endowed the attribute 'name' in case you need to know
which group you are working on.

Examples
--------
>>> df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
...                           'foo', 'bar'],
...                    'B' : [1, 2, 3, 4, 5, 6],
...                    'C' : [2.0, 5., 8., 1., 2., 9.]})
>>> grouped = df.groupby('A')
>>> grouped.filter(lambda x: x['B'].mean() > 3.)
     A  B    C
1  bar  2  5.0
3  bar  4  1.0
5  bar  6  9.0