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
4 changes: 2 additions & 2 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ def group_last(rank_t[:, :] out,
for j in range(K):
val = values[i, j]

if not checknull(val):
if not checknull(val) or val is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment here of why we are doing this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

# NB: use _treat_as_na here once
# conditional-nogil is available.
nobs[lab, j] += 1
Expand Down Expand Up @@ -986,7 +986,7 @@ def group_nth(rank_t[:, :] out,
for j in range(K):
val = values[i, j]

if not checknull(val):
if not checknull(val) or val is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

# NB: use _treat_as_na here once
# conditional-nogil is available.
nobs[lab, j] += 1
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/groupby/test_nth.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment, that we wish to preserve None in object dtypes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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