Skip to content

Commit

Permalink
REGR: Series.mode with boolean and pd.NA (pandas-dev#42131)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Jun 21, 2021
1 parent 56657dc commit 3f10d51
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
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)

0 comments on commit 3f10d51

Please sign in to comment.