diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 7dcc83f76db75..f4a6b0b1c1694 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -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 diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 2f698a82bac49..c0c1c2f057c96 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -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)