Skip to content

TST: added test for groupby when calling first, last, nth (GH29645) #43358

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 4 commits into from
Sep 6, 2021

Conversation

lorenzophys
Copy link
Contributor

Provided test for groupby when callling first, last, nth on a Series with None or np.nan.

Tests and linter pass locally.



@pytest.mark.parametrize("method", ["first", "last", "nth"])
def test_groupby_last_first_nth_with_none(method):
Copy link
Member

Choose a reason for hiding this comment

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

Please define expected without applying a method and then parametrize over None and nan

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't quite understand what you mean: the issue states that None and np.nan should lead to the same result when I groupby and apply a method. If I don't apply a method, then what's the point of the test? My reasoning was: I have two Series with None and np.nan and I have to test that grouping with a method gives the same output.

Copy link
Member

Choose a reason for hiding this comment

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

We would want to test the expected output explicitly. If both yield the same, the result may be incorrect nevertheless

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I think I get what you mean

expected = Series([True])
data = (
Series([nans, "x", nans, "y", nans], index=[0, 0, 0, 0, 0])
.isna()
Copy link
Member

Choose a reason for hiding this comment

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

I don't think isna is necessary.

Does parametrization over all missing values work? pd.NA, NaT etc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you mean something like @pytest.mark.parametrize("nans", [None, np.nan, pd.NA, pd.NaT]) without isna then no, all tests fail. They all pass if isna is there

Copy link
Member

Choose a reason for hiding this comment

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

If you check the same as in #29645 (comment)? We do not want to check for True False rather than for "y".

We have a missing value fixture, but yes I meant like this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's reasonable, I misunderstood

@jreback jreback added Groupby Testing pandas testing functions or related to the test suite labels Sep 2, 2021
@jreback jreback added this to the 1.4 milestone Sep 2, 2021
@@ -2419,3 +2419,20 @@ def test_rolling_wrong_param_min_period():
result_error_msg = r"__init__\(\) got an unexpected keyword argument 'min_period'"
with pytest.raises(TypeError, match=result_error_msg):
test_df.groupby("name")["val"].rolling(window=2, min_period=1).sum()


@pytest.mark.parametrize("method", ["first", "last", "nth"])
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move to test_nth.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing

Copy link
Member

@phofl phofl left a comment

Choose a reason for hiding this comment

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

lgtm

@jreback jreback merged commit b48635e into pandas-dev:master Sep 6, 2021
@jreback
Copy link
Contributor

jreback commented Sep 6, 2021

thanks @lorenzophys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent behavior with None between "normal" pandas functions and groupby's last/first/nth
3 participants