From daab49df3df8dc8118d0398a175088d83e850dab Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Tue, 22 Jun 2021 21:42:41 -0400 Subject: [PATCH 1/2] BUG: nullable integer, floating arrays raising from booleans --- doc/source/whatsnew/v1.4.0.rst | 2 +- pandas/core/arrays/floating.py | 1 + pandas/core/arrays/integer.py | 1 + pandas/tests/arrays/floating/test_construction.py | 13 +++++++++++++ pandas/tests/arrays/integer/test_construction.py | 13 +++++++++++++ 5 files changed, 29 insertions(+), 1 deletion(-) 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..c5602be0e2f38 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_construct_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..8e88da7c01371 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_construct_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", [ From 5a0e94d8ef9a113ad7508a17e6029e13da20640a Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Tue, 22 Jun 2021 21:45:11 -0400 Subject: [PATCH 2/2] Change test name --- pandas/tests/arrays/floating/test_construction.py | 2 +- pandas/tests/arrays/integer/test_construction.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/arrays/floating/test_construction.py b/pandas/tests/arrays/floating/test_construction.py index c5602be0e2f38..ae1041e9a7109 100644 --- a/pandas/tests/arrays/floating/test_construction.py +++ b/pandas/tests/arrays/floating/test_construction.py @@ -160,7 +160,7 @@ def test_to_array_bool(bool_values, values, target_dtype, expected_dtype): "bool_values,expected", [([False, True, False], [0, 1, 0]), ([False, True, pd.NA], [0, 1, pd.NA])], ) -def test_construct_from_boolean_array(bool_values, expected): +def test_construction_from_boolean_array(bool_values, expected): # GH-42137 data = pd.array(bool_values, dtype="boolean") result = pd.array(data, dtype="Float64") diff --git a/pandas/tests/arrays/integer/test_construction.py b/pandas/tests/arrays/integer/test_construction.py index 8e88da7c01371..d6b61ed44e913 100644 --- a/pandas/tests/arrays/integer/test_construction.py +++ b/pandas/tests/arrays/integer/test_construction.py @@ -203,7 +203,7 @@ def test_to_integer_array_bool( "bool_values,expected", [([False, True, False], [0, 1, 0]), ([False, True, pd.NA], [0, 1, pd.NA])], ) -def test_construct_from_boolean_array(bool_values, expected): +def test_construction_from_boolean_array(bool_values, expected): # GH-42137 data = pd.array(bool_values, dtype="boolean") result = pd.array(data, dtype="Int64")