Skip to content

BUG: Make nullable booleans numeric #34056

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

Merged
merged 9 commits into from
May 11, 2020
Merged

BUG: Make nullable booleans numeric #34056

merged 9 commits into from
May 11, 2020

Conversation

dsaxton
Copy link
Member

@dsaxton dsaxton commented May 7, 2020

Also a decent speedup since this is now going through Cython space:

df = pd.DataFrame({"a": [1] * 50_000 + [2] * 50_000, "b": pd.array([True] * 100_000)})

# master
%timeit df.groupby("a").sum()                                                                                                                                                                  
# 5.15 ms ± 18 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

# branch
%timeit df.groupby("a").sum()                                                                                                                                                                  
# 1.94 ms ± 10.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

@pep8speaks
Copy link

pep8speaks commented May 7, 2020

Hello @dsaxton! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-05-10 15:59:48 UTC

@dsaxton dsaxton added Groupby NA - MaskedArrays Related to pd.NA and nullable extension arrays Numeric Operations Arithmetic, Comparison, and Logical operations labels May 7, 2020
@dsaxton dsaxton marked this pull request as ready for review May 9, 2020 22:16
return d.get((dtype, how), dtype)
if how in ["add", "cumsum", "sum"] and (dtype == np.dtype(np.bool)):
return np.dtype(np.int64)
if how in ["add", "cumsum", "sum"] and isinstance(dtype, pd.BooleanDtype):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont use pd import anywhere, rather prefer to import directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hitting some circular import issues when I try to import these directly, is it okay to import from within the function itself?

if how in ["add", "cumsum", "sum"] and (dtype == np.dtype(np.bool)):
return np.dtype(np.int64)
if how in ["add", "cumsum", "sum"] and isinstance(dtype, pd.BooleanDtype):
return pd.Int64Dtype()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use pd

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can either import (best) or return a string "Int64"

@jreback jreback added this to the 1.1 milestone May 10, 2020
@jreback jreback merged commit 8f3afd7 into pandas-dev:master May 11, 2020
@jreback
Copy link
Contributor

jreback commented May 11, 2020

thanks

@dsaxton dsaxton deleted the boolean-is-numeric branch May 11, 2020 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby NA - MaskedArrays Related to pd.NA and nullable extension arrays Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: DataFrameGroupBy.sum ignores min_count for boolean data type
3 participants