Skip to content

Commit 612c244

Browse files
vladserkoffjreback
authored andcommitted
BUG: support casting from bool array to EA Integer dtype (#25265)
1 parent 0558a3c commit 612c244

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,9 @@ Numeric
309309
- Bug in :meth:`Series.divmod` and :meth:`Series.rdivmod` which would raise an (incorrect) ``ValueError`` rather than return a pair of :class:`Series` objects as result (:issue:`25557`)
310310
- Raises a helpful exception when a non-numeric index is sent to :meth:`interpolate` with methods which require numeric index. (:issue:`21662`)
311311
- Bug in :meth:`~pandas.eval` when comparing floats with scalar operators, for example: ``x < -0.1`` (:issue:`25928`)
312+
- Fixed bug where casting all-boolean array to integer extension array failed (:issue:`25211`)
313+
-
312314
-
313-
314315

315316
Conversion
316317
^^^^^^^^^^

pandas/core/arrays/integer.py

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ def coerce_to_array(values, dtype, mask=None, copy=False):
188188
raise TypeError("{} cannot be converted to an IntegerDtype".format(
189189
values.dtype))
190190

191+
elif is_bool_dtype(values) and is_integer_dtype(dtype):
192+
values = np.array(values, dtype=int, copy=copy)
193+
191194
elif not (is_integer_dtype(values) or is_float_dtype(values)):
192195
raise TypeError("{} cannot be converted to an IntegerDtype".format(
193196
values.dtype))

pandas/core/series.py

-1
Original file line numberDiff line numberDiff line change
@@ -2670,7 +2670,6 @@ def combine(self, other, func, fill_value=None):
26702670
# do in _values_from_sequence. We still want ops to work
26712671
# though, so we catch any regular Exception.
26722672
pass
2673-
26742673
return self._constructor(new_values, index=new_index, name=new_name)
26752674

26762675
def combine_first(self, other):

pandas/tests/arrays/test_integer.py

+13
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,19 @@ def test_to_integer_array_float():
613613
assert result.dtype == Int64Dtype()
614614

615615

616+
@pytest.mark.parametrize(
617+
'bool_values, int_values, target_dtype, expected_dtype',
618+
[([False, True], [0, 1], Int64Dtype(), Int64Dtype()),
619+
([False, True], [0, 1], 'Int64', Int64Dtype()),
620+
([False, True, np.nan], [0, 1, np.nan], Int64Dtype(), Int64Dtype())])
621+
def test_to_integer_array_bool(bool_values, int_values, target_dtype,
622+
expected_dtype):
623+
result = integer_array(bool_values, dtype=target_dtype)
624+
assert result.dtype == expected_dtype
625+
expected = integer_array(int_values, dtype=target_dtype)
626+
tm.assert_extension_array_equal(result, expected)
627+
628+
616629
@pytest.mark.parametrize(
617630
'values, to_dtype, result_dtype',
618631
[

0 commit comments

Comments
 (0)