diff --git a/python/cudf/cudf/core/column/numerical.py b/python/cudf/cudf/core/column/numerical.py index 6beeae83005..02fa09519c5 100644 --- a/python/cudf/cudf/core/column/numerical.py +++ b/python/cudf/cudf/core/column/numerical.py @@ -576,6 +576,15 @@ def as_numerical_column(self, dtype: DtypeObj) -> NumericalColumn: res = self.nans_to_nulls().cast(dtype=dtype) res._dtype = dtype return res # type: ignore[return-value] + + if ( + self.dtype.kind == "f" + and dtype.kind == "b" + and not is_pandas_nullable_extension_dtype(dtype) + and self.has_nulls() + ): + return self.fillna(np.nan).cast(dtype=dtype) # type: ignore[return-value] + if dtype_to_pylibcudf_type(dtype) == dtype_to_pylibcudf_type( self.dtype ): diff --git a/python/cudf/cudf/tests/series/methods/test_astype.py b/python/cudf/cudf/tests/series/methods/test_astype.py index df7a560ad62..196128dbf7b 100644 --- a/python/cudf/cudf/tests/series/methods/test_astype.py +++ b/python/cudf/cudf/tests/series/methods/test_astype.py @@ -104,6 +104,16 @@ def test_astype_pandas_nullable_pandas_compat(dtype, klass, kind): assert_eq(actual, expected) +def test_cast_float_nan_to_bool_pandas_compat(): + with cudf.option_context("mode.pandas_compatible", True): + data = [1.0, 0.0, np.nan, None] + gs = cudf.Series(data, dtype="float64") + got = gs.astype("bool") + expected = pd.Series([True, False, True, True], dtype="bool") + assert got.null_count == 0 + assert_eq(expected, got) + + @pytest.mark.parametrize( "type1", [