Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.0.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
- Bug in :meth:`GroupBy.first` and :meth:`GroupBy.last` where None is not preserved in object dtype (:issue:`32800`)
-

.. _whatsnew_104.bug_fixes:
Expand Down
8 changes: 6 additions & 2 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,9 @@ def group_last(rank_t[:, :] out,
for j in range(K):
val = values[i, j]

if not checknull(val):
# 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:
# NB: use _treat_as_na here once
# conditional-nogil is available.
nobs[lab, j] += 1
Expand Down Expand Up @@ -983,7 +985,9 @@ def group_nth(rank_t[:, :] out,
for j in range(K):
val = values[i, j]

if not checknull(val):
# 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:
# NB: use _treat_as_na here once
# conditional-nogil is available.
nobs[lab, j] += 1
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/groupby/test_nth.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ def test_nth_with_na_object(index, nulls_fixture):
tm.assert_frame_equal(result, expected)


@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 = pd.DataFrame.from_dict({"id": ["a"], "value": [None]})
groups = df.groupby("id", as_index=False)
result = getattr(groups, method)()

tm.assert_frame_equal(result, df)


def test_first_last_nth_dtypes(df_mixed_floats):

df = df_mixed_floats.copy()
Expand Down