Skip to content

Commit bfa5846

Browse files
committed
Add tests
1 parent 1705532 commit bfa5846

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

pandas/tests/groupby/test_groupby_dropna.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,33 @@ def test_groupby_nan_included():
384384
assert list(result.keys())[0:2] == ["g1", "g2"]
385385

386386

387+
@pytest.mark.parametrize(
388+
"by",
389+
[
390+
pytest.param("group", id="column"),
391+
pytest.param(pd.Series(["g1", np.nan, "g1", "g2", np.nan]), id="Series"),
392+
pytest.param("_index", id="index"),
393+
],
394+
)
395+
@pytest.mark.parametrize("dropna", [True, False, None])
396+
def test_groupby_nan_included_warns(by, dropna):
397+
# GH 61339
398+
data = {"group": ["g1", np.nan, "g1", "g2", np.nan], "B": [0, 1, 2, 3, 4]}
399+
df = pd.DataFrame(data)
400+
if by == "_index":
401+
df = df.set_index("group")
402+
403+
kwargs = {}
404+
warning_type = pd.errors.NullKeyWarning
405+
if dropna is not None:
406+
kwargs = {"dropna": dropna}
407+
warning_type = None
408+
409+
with tm.assert_produces_warning(warning_type):
410+
grouped = df.groupby(by, **kwargs)
411+
result = grouped.indices # noqa:F841
412+
413+
387414
def test_groupby_drop_nan_with_multi_index():
388415
# GH 39895
389416
df = pd.DataFrame([[np.nan, 0, 1]], columns=["a", "b", "c"])

pandas/tests/groupby/test_missing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ def test_min_count(func, min_count, value):
8383
def test_indices_with_missing():
8484
# GH 9304
8585
df = DataFrame({"a": [1, 1, np.nan], "b": [2, 3, 4], "c": [5, 6, 7]})
86-
g = df.groupby(["a", "b"])
87-
result = g.indices
86+
# GH 61339
87+
with tm.assert_produces_warning(pd.errors.NullKeyWarning):
88+
g = df.groupby(["a", "b"])
89+
result = g.indices
8890
expected = {(1.0, 2): np.array([0]), (1.0, 3): np.array([1])}
8991
assert result == expected

0 commit comments

Comments
 (0)