Skip to content

ENH: Add support for min_count keyword for Resample and Groupby functions #37870

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 8 commits into from
Nov 26, 2020

Conversation

phofl
Copy link
Member

@phofl phofl commented Nov 15, 2020

This would add support for the documented keyword ```min_count``. If we decide to move forward with this, we have to decide what to do in the following case:

df = DataFrame({"a": [1] * 3, "b": [np.nan] * 3})
result = df.groupby("a").func(min_count=0)

first and last return 0.0 which is fine I think and consistent with sum, but min returns inf while max returns -inf.

Default parameter is set to 1 to avoid backwards incompatible changes. We should keep this at least until 2.0

If we decide to not implement min_count here, we should adjust the docs and the function signatures for the groupby functions.

cc @rhshadrach

@phofl phofl added Groupby Resample resample method labels Nov 15, 2020
@rhshadrach
Copy link
Member

Thanks for the PR @phofl. It seems to me that first, last, min, and max should all return nan even when min_count is 0. Sum behaves differently because the empty sum is 0 (likewise, the empty product is 1).

@rhshadrach
Copy link
Member

Default parameter is set to 1 to avoid backwards incompatible changes. We should keep this at least until 2.0

Do you perhaps mean -1? Otherwise, I don't follow.

@DiSchi123
Copy link

My 2 cents: min_count=0 is the same as not specifying a min_count. Min_count of x says you need to have at least x non-nan values in the bin to show a result, otherwise show nan. If you have a min_count of 0 isnt that like saying show me the sum, first etc regardless how many nans are there? Maybe I am missing something?

But while it is important to decide what min_count=0 should show it is less important for the implementation, you would not specify that imo, because as mentioned it is like not specifying it. I am ok with any decision.

@phofl
Copy link
Member Author

phofl commented Nov 22, 2020

cc @rhshadrach When we want to return nan in this case, we can keep -1 as default.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

Overall looks really good, just some minor comments on tests.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

@@ -1411,7 +1407,7 @@ def group_min(groupby_t[:, :] out,

for i in range(ncounts):
for j in range(K):
if nobs[i, j] == 0:
if nobs[i, j] < min_count or nobs[i, j] == 0:
Copy link
Member

Choose a reason for hiding this comment

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

can you avoid the double lookup/comparison here by setting min_count = max(min_count, 1) outside the loop?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@jreback jreback added this to the 1.2 milestone Nov 25, 2020
@jreback
Copy link
Contributor

jreback commented Nov 25, 2020

lgtm. ping on green (resolved conflict)

@phofl
Copy link
Member Author

phofl commented Nov 26, 2020

@jreback greenish. Failure is unrelated

@jreback jreback merged commit 8b0af58 into pandas-dev:master Nov 26, 2020
@jreback
Copy link
Contributor

jreback commented Nov 26, 2020

thanks @phofl

@phofl phofl deleted the groupby_min_count branch November 26, 2020 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby Resample resample method
Projects
None yet
5 participants