diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index f992d6aa09ead..c55ee32528e1b 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -202,7 +202,7 @@ Sparse ExtensionArray ^^^^^^^^^^^^^^ -- +- Bug in construction of nullable integer and floating arrays raising ``TypeError`` when passed boolean data containing ``pd.NA`` (:issue:`42137`) - Styler diff --git a/pandas/core/arrays/floating.py b/pandas/core/arrays/floating.py index 1acbcf17dfffd..7556bea38242c 100644 --- a/pandas/core/arrays/floating.py +++ b/pandas/core/arrays/floating.py @@ -139,6 +139,7 @@ def coerce_to_array( "mixed-integer", "integer-na", "mixed-integer-float", + "boolean", ]: raise TypeError(f"{values.dtype} cannot be converted to a FloatingDtype") diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py index c9ba762a271bd..b9026c4f680d1 100644 --- a/pandas/core/arrays/integer.py +++ b/pandas/core/arrays/integer.py @@ -194,6 +194,7 @@ def coerce_to_array( "mixed-integer", "integer-na", "mixed-integer-float", + "boolean", ]: raise TypeError(f"{values.dtype} cannot be converted to an IntegerDtype") diff --git a/pandas/tests/arrays/floating/test_construction.py b/pandas/tests/arrays/floating/test_construction.py index 4ce3dd35b538b..ae1041e9a7109 100644 --- a/pandas/tests/arrays/floating/test_construction.py +++ b/pandas/tests/arrays/floating/test_construction.py @@ -146,6 +146,7 @@ def test_to_array_integer(): ([False, True], [0, 1], Float64Dtype(), Float64Dtype()), ([False, True], [0, 1], "Float64", Float64Dtype()), ([False, True, np.nan], [0, 1, np.nan], Float64Dtype(), Float64Dtype()), + ([False, True, pd.NA], [0, 1, pd.NA], Float64Dtype(), Float64Dtype()), ], ) def test_to_array_bool(bool_values, values, target_dtype, expected_dtype): @@ -155,6 +156,18 @@ def test_to_array_bool(bool_values, values, target_dtype, expected_dtype): tm.assert_extension_array_equal(result, expected) +@pytest.mark.parametrize( + "bool_values,expected", + [([False, True, False], [0, 1, 0]), ([False, True, pd.NA], [0, 1, pd.NA])], +) +def test_construction_from_boolean_array(bool_values, expected): + # GH-42137 + data = pd.array(bool_values, dtype="boolean") + result = pd.array(data, dtype="Float64") + expected = pd.array(expected, dtype="Float64") + tm.assert_extension_array_equal(result, expected) + + def test_series_from_float(data): # construct from our dtype & string dtype dtype = data.dtype diff --git a/pandas/tests/arrays/integer/test_construction.py b/pandas/tests/arrays/integer/test_construction.py index b48567d37ecaf..d6b61ed44e913 100644 --- a/pandas/tests/arrays/integer/test_construction.py +++ b/pandas/tests/arrays/integer/test_construction.py @@ -187,6 +187,7 @@ def test_to_integer_array_float(): ([False, True], [0, 1], Int64Dtype(), Int64Dtype()), ([False, True], [0, 1], "Int64", Int64Dtype()), ([False, True, np.nan], [0, 1, np.nan], Int64Dtype(), Int64Dtype()), + ([False, True, pd.NA], [0, 1, pd.NA], Int64Dtype(), Int64Dtype()), ], ) def test_to_integer_array_bool( @@ -198,6 +199,18 @@ def test_to_integer_array_bool( tm.assert_extension_array_equal(result, expected) +@pytest.mark.parametrize( + "bool_values,expected", + [([False, True, False], [0, 1, 0]), ([False, True, pd.NA], [0, 1, pd.NA])], +) +def test_construction_from_boolean_array(bool_values, expected): + # GH-42137 + data = pd.array(bool_values, dtype="boolean") + result = pd.array(data, dtype="Int64") + expected = pd.array(expected, dtype="Int64") + tm.assert_extension_array_equal(result, expected) + + @pytest.mark.parametrize( "values, to_dtype, result_dtype", [