Skip to content

GroupBy Filter does not work for 2+ grouping columns in 0.12.0 #4527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dragoljub opened this issue Aug 9, 2013 · 3 comments
Closed

GroupBy Filter does not work for 2+ grouping columns in 0.12.0 #4527

dragoljub opened this issue Aug 9, 2013 · 3 comments
Labels
Duplicate Report Duplicate issue or pull request Groupby
Milestone

Comments

@dragoljub
Copy link

Trying to filter out groups with less than 1 row with the new groupby().filter() in 0.12.0:

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)})

grp = df.groupby(['A', 'B'])

grp.apply(lambda x: len(x)>1) # Returns correct bool series

A    B    
bar  one      False
     three    False
     two      False
foo  one       True
     three    False
     two       True
dtype: bool

grp.filter(lambda x: len(x)>1)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-140-4dccfb561597> in <module>()
----> 1 grp.filter(lambda x: len(x)>1)

C:\Python27\lib\site-packages\pandas\core\groupby.pyc in filter(self, func, dropna, *args, **kwargs)
   2092                 res = path(group)
   2093 
-> 2094             if res:
   2095                 indexers.append(self.obj.index.get_indexer(group.index))
   2096 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
@jreback
Copy link
Contributor

jreback commented Aug 9, 2013

reported on #4447, but nice example as well!

@dragoljub
Copy link
Author

Ahh someone beat me to it. I can close or leave it open for the example.

@jreback
Copy link
Contributor

jreback commented Aug 9, 2013

its linked....so going to close..thanks for the report

@jreback jreback closed this as completed Aug 9, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request Groupby
Projects
None yet
Development

No branches or pull requests

2 participants