Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,7 @@ def group_last(rank_t[:, :] out,
for j in range(K):
val = values[i, j]

# None should not be treated like other NA-like
# so that it won't be converted to nan
if not checknull(val) or val is None:
if not checknull(val):
# NB: use _treat_as_na here once
# conditional-nogil is available.
nobs[lab, j] += 1
Expand All @@ -939,7 +937,7 @@ def group_last(rank_t[:, :] out,
for i in range(ncounts):
for j in range(K):
if nobs[i, j] < min_count:
out[i, j] = NAN
out[i, j] = None
else:
out[i, j] = resx[i, j]
else:
Expand Down Expand Up @@ -1023,9 +1021,7 @@ def group_nth(rank_t[:, :] out,
for j in range(K):
val = values[i, j]

# None should not be treated like other NA-like
# so that it won't be converted to nan
if not checknull(val) or val is None:
if not checknull(val):
# NB: use _treat_as_na here once
# conditional-nogil is available.
nobs[lab, j] += 1
Expand All @@ -1035,7 +1031,7 @@ def group_nth(rank_t[:, :] out,
for i in range(ncounts):
for j in range(K):
if nobs[i, j] < min_count:
out[i, j] = NAN
out[i, j] = None
else:
out[i, j] = resx[i, j]

Expand Down
29 changes: 21 additions & 8 deletions pandas/tests/groupby/test_nth.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,27 @@ def test_nth_with_na_object(index, nulls_fixture):


@pytest.mark.parametrize("method", ["first", "last"])
def test_first_last_with_None(method):
# https://github.com/pandas-dev/pandas/issues/32800
# None should be preserved as object dtype
df = DataFrame.from_dict({"id": ["a"], "value": [None]})
groups = df.groupby("id", as_index=False)
result = getattr(groups, method)()

tm.assert_frame_equal(result, df)
@pytest.mark.parametrize(
"df, expected",
[
(
DataFrame({"id": ["a"], "value": [None]}),
DataFrame({"value": [None]}, index=Index(["a"], name="id")),
),
(
DataFrame({"id": "a", "value": [None, "foo", np.nan]}),
DataFrame({"value": ["foo"]}, index=Index(["a"], name="id")),
),
(
DataFrame({"id": "a", "value": [np.nan]}, dtype=object),
DataFrame({"value": [None]}, index=Index(["a"], name="id")),
),
],
)
def test_first_last_with_none(method, df, expected):
# GH 32800, 38286
result = getattr(df.groupby("id"), method)()
tm.assert_frame_equal(result, expected)


def test_first_last_nth_dtypes(df_mixed_floats):
Expand Down