Skip to content

BUG: nullable integer, floating arrays raising from booleans #42194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pandas/core/arrays/floating.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
1 change: 1 addition & 0 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/arrays/floating/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/arrays/integer/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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",
[
Expand Down