Skip to content

Commit 86cb950

Browse files
authored
TST: Test groupby method drop na (#50755)
* TST: test-groupby-method-drop-na * add first last tail * construct DataFrame in one line * use actual func instead of agg
1 parent f941b76 commit 86cb950

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

pandas/tests/groupby/test_groupby.py

+19
Original file line numberDiff line numberDiff line change
@@ -2846,3 +2846,22 @@ def test_sum_of_booleans(n):
28462846
result = df.groupby("groupby_col").sum()
28472847
expected = DataFrame({"bool": [n]}, index=Index([1], name="groupby_col"))
28482848
tm.assert_frame_equal(result, expected)
2849+
2850+
2851+
@pytest.mark.parametrize("method", ["head", "tail", "nth", "first", "last"])
2852+
def test_groupby_method_drop_na(method):
2853+
# GH 21755
2854+
df = DataFrame({"A": ["a", np.nan, "b", np.nan, "c"], "B": range(5)})
2855+
2856+
if method == "nth":
2857+
result = getattr(df.groupby("A"), method)(n=0)
2858+
else:
2859+
result = getattr(df.groupby("A"), method)()
2860+
2861+
if method in ["first", "last"]:
2862+
expected = DataFrame({"B": [0, 2, 4]}).set_index(
2863+
Series(["a", "b", "c"], name="A")
2864+
)
2865+
else:
2866+
expected = DataFrame({"A": ["a", "b", "c"], "B": [0, 2, 4]}, index=[0, 2, 4])
2867+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)