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: Series.mode with boolean and pd.NA #42131

Merged
merged 1 commit into from
Jun 21, 2021
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
6 changes: 5 additions & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ def _ensure_data(values: ArrayLike) -> tuple[np.ndarray, DtypeObj]:
return np.asarray(values).view("uint8"), values.dtype
else:
# i.e. all-bool Categorical, BooleanArray
return np.asarray(values).astype("uint8", copy=False), values.dtype
try:
return np.asarray(values).astype("uint8", copy=False), values.dtype
except TypeError:
# GH#42107 we have pd.NAs present
return np.asarray(values), values.dtype

elif is_integer_dtype(values.dtype):
return np.asarray(values), values.dtype
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1480,3 +1480,10 @@ def test_mode_sortwarning(self):
result = result.sort_values().reset_index(drop=True)

tm.assert_series_equal(result, expected)

def test_mode_boolean_with_na(self):
# GH#42107
ser = Series([True, False, True, pd.NA], dtype="boolean")
result = ser.mode()
expected = Series({0: True}, dtype="boolean")
tm.assert_series_equal(result, expected)