-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
groupby.nth with dropna is buggy #11038
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
Comments
@behzadnouri You should pass either of When you however pass either >>> df = pd.DataFrame([[1],[2,3],[1],[2,4]], columns=['1st', '2nd'])
>>> df
1st 2nd
0 1 NaN
1 2 3
2 1 NaN
3 2 4
>>> df.groupby('1st')['2nd'].nth(0, dropna='any')
1st
1 3
2 4
Name: 2nd, dtype: float64
>>> df.groupby('1st')['2nd'].nth(0, dropna='all')
1st
1 3
2 4
Name: 2nd, dtype: float64 Exchanging the order of selecting a column and >>> df.groupby('1st').nth(0, dropna='any')['2nd']
1st
1 NaN
2 3
Name: 2nd, dtype: float64
>>> df.groupby('1st').nth(0, dropna='all')['2nd']
1st
1 NaN
2 3
Name: 2nd, dtype: float64 |
@tnir yep, the argument is a bit too free and should be checked at the top of As always pull-requests to fix are welcome. |
This is still reproducible with pandas-0.20.3 on Python 3.6: $ python
Python 3.6.0 (default, Dec 26 2016, 15:43:50)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin >>> import pandas as pd
>>> df = pd.DataFrame([[1],[2,3],[1],[2,4]], columns=['1st', '2nd'])
>>> df
1st 2nd
0 1 NaN
1 2 3.0
2 1 NaN
3 2 4.0
>>> df.groupby('1st')['2nd'].nth(0, dropna='any')
1st
1 3.0
2 4.0
Name: 2nd, dtype: float64
>>> df.groupby('1st')['2nd'].nth(0, dropna='all')
1st
1 3.0
2 4.0
Name: 2nd, dtype: float64
>>> df.groupby('1st').nth(0, dropna='any')['2nd']
1st
1 NaN
2 3.0
Name: 2nd, dtype: float64
>>> df.groupby('1st').nth(0, dropna='all')['2nd']
1st
1 NaN
2 3.0
Name: 2nd, dtype: float64 |
This now raises The code from #11038 (comment) now results in
This deviates from the expected behavior mentioned there, but looks right to me. In particular, by performing selection dropna is now dropping all of group 1. Also note that the |
The text was updated successfully, but these errors were encountered: