From e96d0d41c439bc5f5e4425f3cfd52f4131b18435 Mon Sep 17 00:00:00 2001 From: shivanshug14 <18354771+shiv-io@users.noreply.github.com> Date: Sun, 21 Mar 2021 22:44:20 +0530 Subject: [PATCH 1/6] Add test --- pandas/tests/groupby/test_missing.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/tests/groupby/test_missing.py b/pandas/tests/groupby/test_missing.py index e53518269408a..e312e8691861b 100644 --- a/pandas/tests/groupby/test_missing.py +++ b/pandas/tests/groupby/test_missing.py @@ -43,6 +43,17 @@ def test_ffill_missing_arguments(): df.groupby("b").fillna() +def test_ffill_with_string_column(): + # GH 40250 + result = ( + DataFrame({"a": pd.array([None, "a"], dtype="string"), "b": [0, 0]}) + .groupby("b") + .ffill() + ) + expected = DataFrame({"a": pd.array([None, "a"], dtype="string")}) + tm.assert_frame_equal(result, expected) + + def test_fill_consistency(): # GH9221 From e633c2a47cc03a697ff90df8c2b38001ddb79282 Mon Sep 17 00:00:00 2001 From: shivanshug14 <18354771+shiv-io@users.noreply.github.com> Date: Sat, 17 Apr 2021 14:36:29 +0530 Subject: [PATCH 2/6] Parameterize test to test bfill and ffill --- pandas/tests/groupby/test_missing.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pandas/tests/groupby/test_missing.py b/pandas/tests/groupby/test_missing.py index e312e8691861b..3966150634ea5 100644 --- a/pandas/tests/groupby/test_missing.py +++ b/pandas/tests/groupby/test_missing.py @@ -43,14 +43,18 @@ def test_ffill_missing_arguments(): df.groupby("b").fillna() -def test_ffill_with_string_column(): +@pytest.mark.parametrize("method", ["ffill", "bfill"]) +def test_ffill_with_string_dtype(method): # GH 40250 result = ( - DataFrame({"a": pd.array([None, "a"], dtype="string"), "b": [0, 0]}) + DataFrame({"a": pd.array([None, "a", None], dtype="string"), "b": [0, 0, 0]}) .groupby("b") - .ffill() + .fillna(method=method) ) - expected = DataFrame({"a": pd.array([None, "a"], dtype="string")}) + if method == "ffill": + expected = DataFrame({"a": pd.array([None, "a", "a"], dtype="string")}) + elif method == "bfill": + expected = DataFrame({"a": pd.array(["a", "a", None], dtype="string")}) tm.assert_frame_equal(result, expected) From c723e8c81b307dce46bb65bb9ed87d4f7b2ab7b2 Mon Sep 17 00:00:00 2001 From: shivanshug14 <18354771+shiv-io@users.noreply.github.com> Date: Sat, 17 Apr 2021 14:37:32 +0530 Subject: [PATCH 3/6] Update function name --- pandas/tests/groupby/test_missing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_missing.py b/pandas/tests/groupby/test_missing.py index 3966150634ea5..73e00dc9bb009 100644 --- a/pandas/tests/groupby/test_missing.py +++ b/pandas/tests/groupby/test_missing.py @@ -44,7 +44,7 @@ def test_ffill_missing_arguments(): @pytest.mark.parametrize("method", ["ffill", "bfill"]) -def test_ffill_with_string_dtype(method): +def test_fillna_with_string_dtype(method): # GH 40250 result = ( DataFrame({"a": pd.array([None, "a", None], dtype="string"), "b": [0, 0, 0]}) From c4902e6911464490c57d3f2463dd2f547536d1ac Mon Sep 17 00:00:00 2001 From: shivanshug14 <18354771+shiv-io@users.noreply.github.com> Date: Sun, 9 May 2021 16:47:07 +0530 Subject: [PATCH 4/6] Clean up result assignment --- pandas/tests/groupby/test_missing.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/tests/groupby/test_missing.py b/pandas/tests/groupby/test_missing.py index 73e00dc9bb009..2b8b0f14a54e4 100644 --- a/pandas/tests/groupby/test_missing.py +++ b/pandas/tests/groupby/test_missing.py @@ -46,11 +46,9 @@ def test_ffill_missing_arguments(): @pytest.mark.parametrize("method", ["ffill", "bfill"]) def test_fillna_with_string_dtype(method): # GH 40250 - result = ( - DataFrame({"a": pd.array([None, "a", None], dtype="string"), "b": [0, 0, 0]}) - .groupby("b") - .fillna(method=method) - ) + df = DataFrame({"a": pd.array([None, "a", None], dtype="string"), "b": [0, 0, 0]}) + grp = df.groupby("b") + result = grp.fillna(method=method) if method == "ffill": expected = DataFrame({"a": pd.array([None, "a", "a"], dtype="string")}) elif method == "bfill": From 31c93127b2aeade6a9758579883be351b26a1a70 Mon Sep 17 00:00:00 2001 From: shivanshug14 <18354771+shiv-io@users.noreply.github.com> Date: Sun, 9 May 2021 16:49:07 +0530 Subject: [PATCH 5/6] Parameterize expected dataframe --- pandas/tests/groupby/test_missing.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pandas/tests/groupby/test_missing.py b/pandas/tests/groupby/test_missing.py index 2b8b0f14a54e4..f8bee7b69f4a3 100644 --- a/pandas/tests/groupby/test_missing.py +++ b/pandas/tests/groupby/test_missing.py @@ -43,16 +43,15 @@ def test_ffill_missing_arguments(): df.groupby("b").fillna() -@pytest.mark.parametrize("method", ["ffill", "bfill"]) +@pytest.mark.parametrize( + "method, expected", [("ffill", [None, "a", "a"]), ("bfill", ["a", "a", None])] +) def test_fillna_with_string_dtype(method): # GH 40250 df = DataFrame({"a": pd.array([None, "a", None], dtype="string"), "b": [0, 0, 0]}) - grp = df.groupby("b") + grp = df.groupby("b") result = grp.fillna(method=method) - if method == "ffill": - expected = DataFrame({"a": pd.array([None, "a", "a"], dtype="string")}) - elif method == "bfill": - expected = DataFrame({"a": pd.array(["a", "a", None], dtype="string")}) + expected = DataFrame({"a": pd.array(expected, dtype="string")}) tm.assert_frame_equal(result, expected) From 4e230074c6f317d508a89b8ef424fd239c56decc Mon Sep 17 00:00:00 2001 From: shivanshug14 <18354771+shiv-io@users.noreply.github.com> Date: Sun, 9 May 2021 16:56:28 +0530 Subject: [PATCH 6/6] Add test function parameter --- pandas/tests/groupby/test_missing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_missing.py b/pandas/tests/groupby/test_missing.py index f8bee7b69f4a3..f3149abb52291 100644 --- a/pandas/tests/groupby/test_missing.py +++ b/pandas/tests/groupby/test_missing.py @@ -46,7 +46,7 @@ def test_ffill_missing_arguments(): @pytest.mark.parametrize( "method, expected", [("ffill", [None, "a", "a"]), ("bfill", ["a", "a", None])] ) -def test_fillna_with_string_dtype(method): +def test_fillna_with_string_dtype(method, expected): # GH 40250 df = DataFrame({"a": pd.array([None, "a", None], dtype="string"), "b": [0, 0, 0]}) grp = df.groupby("b")