From 8379f117832ea4863f9c75c3d593490a82519170 Mon Sep 17 00:00:00 2001 From: lorenzophys Date: Thu, 2 Sep 2021 12:37:29 +0200 Subject: [PATCH 1/4] TST: added test for groupby when calling first, last, nth (GH29645) --- pandas/tests/groupby/test_groupby.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index a714abd461461..1e7f6592963e9 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2419,3 +2419,27 @@ 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"]) +def test_groupby_last_first_nth_with_none(method): + # GH29645 + s1 = ( + Series([None, "x", None, "y", None], index=[0, 0, 0, 0, 0]) + .isna() + .groupby(level=0) + ) + s2 = ( + Series([np.nan, "x", np.nan, "y", np.nan], index=[0, 0, 0, 0, 0]) + .isna() + .groupby(level=0) + ) + + if method == "nth": + s_none = getattr(s1, method)(2) + s_nan = getattr(s2, method)(2) + else: + s_none = getattr(s1, method)() + s_nan = getattr(s2, method)() + + tm.assert_series_equal(s_none, s_nan) From 384d32f700c5a90417a15792f4f96719f23a009a Mon Sep 17 00:00:00 2001 From: lorenzophys Date: Thu, 2 Sep 2021 15:32:57 +0200 Subject: [PATCH 2/4] TST: code review, explicit expected, parametrized over nans --- pandas/tests/groupby/test_groupby.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 1e7f6592963e9..e8205bb1a16e0 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2422,24 +2422,19 @@ def test_rolling_wrong_param_min_period(): @pytest.mark.parametrize("method", ["first", "last", "nth"]) -def test_groupby_last_first_nth_with_none(method): +@pytest.mark.parametrize("nans", [None, np.nan]) +def test_groupby_last_first_nth_with_none(method, nans): # GH29645 - s1 = ( - Series([None, "x", None, "y", None], index=[0, 0, 0, 0, 0]) - .isna() - .groupby(level=0) - ) - s2 = ( - Series([np.nan, "x", np.nan, "y", np.nan], index=[0, 0, 0, 0, 0]) + expected = Series([True]) + data = ( + Series([nans, "x", nans, "y", nans], index=[0, 0, 0, 0, 0]) .isna() .groupby(level=0) ) if method == "nth": - s_none = getattr(s1, method)(2) - s_nan = getattr(s2, method)(2) + result = getattr(data, method)(2) else: - s_none = getattr(s1, method)() - s_nan = getattr(s2, method)() + result = getattr(data, method)() - tm.assert_series_equal(s_none, s_nan) + tm.assert_series_equal(result, expected) From a9646d85516d03bcc4881c00c52c3826d73a69ec Mon Sep 17 00:00:00 2001 From: lorenzophys Date: Thu, 2 Sep 2021 16:16:05 +0200 Subject: [PATCH 3/4] TST: code review, check for value, use nulls_fixture --- pandas/tests/groupby/test_groupby.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index e8205bb1a16e0..f5a000d485cc7 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2422,18 +2422,16 @@ def test_rolling_wrong_param_min_period(): @pytest.mark.parametrize("method", ["first", "last", "nth"]) -@pytest.mark.parametrize("nans", [None, np.nan]) -def test_groupby_last_first_nth_with_none(method, nans): +def test_groupby_last_first_nth_with_none(method, nulls_fixture): # GH29645 - expected = Series([True]) - data = ( - Series([nans, "x", nans, "y", nans], index=[0, 0, 0, 0, 0]) - .isna() - .groupby(level=0) - ) + expected = Series(["y"]) + data = Series( + [nulls_fixture, nulls_fixture, nulls_fixture, "y", nulls_fixture], + index=[0, 0, 0, 0, 0], + ).groupby(level=0) if method == "nth": - result = getattr(data, method)(2) + result = getattr(data, method)(3) else: result = getattr(data, method)() From 2636ad763488b09cb8f1a855c3852838fe813407 Mon Sep 17 00:00:00 2001 From: lorenzophys Date: Fri, 3 Sep 2021 09:14:51 +0200 Subject: [PATCH 4/4] TST: code review, moved test to test_nth.py --- pandas/tests/groupby/test_groupby.py | 17 ----------------- pandas/tests/groupby/test_nth.py | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index f5a000d485cc7..a714abd461461 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2419,20 +2419,3 @@ 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"]) -def test_groupby_last_first_nth_with_none(method, nulls_fixture): - # GH29645 - expected = Series(["y"]) - data = Series( - [nulls_fixture, nulls_fixture, nulls_fixture, "y", nulls_fixture], - index=[0, 0, 0, 0, 0], - ).groupby(level=0) - - if method == "nth": - result = getattr(data, method)(3) - else: - result = getattr(data, method)() - - tm.assert_series_equal(result, expected) diff --git a/pandas/tests/groupby/test_nth.py b/pandas/tests/groupby/test_nth.py index e7a5e931f5297..f0eef550b39ac 100644 --- a/pandas/tests/groupby/test_nth.py +++ b/pandas/tests/groupby/test_nth.py @@ -689,3 +689,20 @@ def test_first_multi_key_groupbby_categorical(): [(1, 100), (1, 200), (2, 100)], names=["A", "B"] ) tm.assert_frame_equal(result, expected) + + +@pytest.mark.parametrize("method", ["first", "last", "nth"]) +def test_groupby_last_first_nth_with_none(method, nulls_fixture): + # GH29645 + expected = Series(["y"]) + data = Series( + [nulls_fixture, nulls_fixture, nulls_fixture, "y", nulls_fixture], + index=[0, 0, 0, 0, 0], + ).groupby(level=0) + + if method == "nth": + result = getattr(data, method)(3) + else: + result = getattr(data, method)() + + tm.assert_series_equal(result, expected)