Skip to content

ENH: implement Kleene versions of GroupBy any/all kernels for nullable dtypes #37506

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
arw2019 opened this issue Oct 29, 2020 · 4 comments · Fixed by #40819
Closed

ENH: implement Kleene versions of GroupBy any/all kernels for nullable dtypes #37506

arw2019 opened this issue Oct 29, 2020 · 4 comments · Fixed by #40819
Assignees
Labels
Enhancement Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate NA - MaskedArrays Related to pd.NA and nullable extension arrays
Milestone

Comments

@arw2019
Copy link
Member

arw2019 commented Oct 29, 2020

xref #37493 (comment)

@arw2019 arw2019 added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member ExtensionArray Extending pandas with custom dtypes or arrays. NA - MaskedArrays Related to pd.NA and nullable extension arrays Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels Oct 29, 2020
@jorisvandenbossche jorisvandenbossche added Groupby and removed ExtensionArray Extending pandas with custom dtypes or arrays. Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 30, 2020
@phofl
Copy link
Member

phofl commented Oct 30, 2020

Wanted to look into this and tried to understand the implementation for Keene logic in case of any/all for a Series. This means, that

s = pd.Series([False, pd.NA, False], dtype="boolean")
print(s.any(skipna=False))

works and returns pd.NA. But

df= pd.DataFrame({"a": [False, pd.NA, False]}, dtype="boolean")
print(df.any(skipna=False))

returns an empty Series. I could not find the reason, why this was not implemted for DataFrames too, when any columns there is from dtype boolean. Should this get implemented for SeriesGroupBy and DataFrameGroupBy?

@arw2019
Copy link
Member Author

arw2019 commented Oct 30, 2020

I haven't gone through the whole path put I think we may want to do this in the cython method

@phofl
Copy link
Member

phofl commented Oct 30, 2020

For GroupBy that would probably be the best. But we have to fix #37505 before doing this, because this raises for

s = pd.Series([False, pd.NA, False], dtype="boolean", index=[0,0, 1])
grouped = s.groupby(level=0)
print(grouped.any(skipna=False))

too before even reaching the cython part. But I meant the regular DataFrame.any() without groupby. This does not work either

Traceback:

Traceback (most recent call last):
  File "/home/developer/PycharmProjects/pandas/pandas/core/groupby/groupby.py", line 2594, in _get_cythonized_result
    vals, inferences = pre_processing(vals)
  File "/home/developer/PycharmProjects/pandas/pandas/core/groupby/groupby.py", line 1368, in objs_to_bool
    vals = vals.astype(bool)
  File "/home/developer/PycharmProjects/pandas/pandas/core/arrays/boolean.py", line 388, in astype
    raise ValueError("cannot convert float NaN to bool")
ValueError: cannot convert float NaN to bool

@arw2019
Copy link
Member Author

arw2019 commented Oct 30, 2020

For GroupBy that would probably be the best. But we have to fix #37505 before doing this, because this raises for

s = pd.Series([False, pd.NA, False], dtype="boolean", index=[0,0, 1])
grouped = s.groupby(level=0)
print(grouped.any(skipna=False))

too before even reaching the cython part. But I meant the regular DataFrame.any() without groupby. This does not work either

Traceback:

Traceback (most recent call last):
  File "/home/developer/PycharmProjects/pandas/pandas/core/groupby/groupby.py", line 2594, in _get_cythonized_result
    vals, inferences = pre_processing(vals)
  File "/home/developer/PycharmProjects/pandas/pandas/core/groupby/groupby.py", line 1368, in objs_to_bool
    vals = vals.astype(bool)
  File "/home/developer/PycharmProjects/pandas/pandas/core/arrays/boolean.py", line 388, in astype
    raise ValueError("cannot convert float NaN to bool")
ValueError: cannot convert float NaN to bool
``

Ah but "boolean" is an Extension array right? I think if we rewrite in terms of a mask that's a fix and it's preferred performance-wise. But in the meantime we can cast to numpy before passing to cython as was done for std in #37433

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants