Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REGR: Groupby first/last/nth treats None as an observation #38330

Merged
merged 4 commits into from Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.5.rst
Expand Up @@ -27,6 +27,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.fillna` not filling ``NaN`` after other operations such as :meth:`DataFrame.pivot` (:issue:`36495`).
- Fixed performance regression in ``df.groupby(..).rolling(..)`` (:issue:`38038`)
- Fixed regression in :meth:`MultiIndex.intersection` returning duplicates when at least one of the indexes had duplicates (:issue:`36915`)
- Fixed regression in :meth:`.GroupBy.first` and :meth:`.GroupBy.last` where ``None`` was considered a non-NA value (:issue:`38286`)

.. ---------------------------------------------------------------------------

Expand Down
12 changes: 4 additions & 8 deletions pandas/_libs/groupby.pyx
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
20 changes: 20 additions & 0 deletions pandas/tests/groupby/test_nth.py
Expand Up @@ -101,6 +101,26 @@ def test_first_last_with_None(method):
tm.assert_frame_equal(result, df)


@pytest.mark.parametrize("method", ["first", "last"])
@pytest.mark.parametrize(
"df, expected",
[
(
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_expanded(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):

df = df_mixed_floats.copy()
Expand Down