Skip to content
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

BUG: support casting from bool array to EA Integer dtype #25265

Merged
merged 22 commits into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0c97445
BUG: support casting from bool array to EA Integer dtype
vladserkoff Feb 11, 2019
b3e1d76
only cast bool to int if target dtype is interger type
vladserkoff Feb 12, 2019
ed61191
Merge branch 'master' into integer-array-from-bool
vladserkoff Mar 20, 2019
c80d4cd
add test to #25265
vladserkoff Mar 20, 2019
9c2877a
fix missing parameter to the test to #25265
vladserkoff Mar 20, 2019
697852d
pass dtype to integer_array to the test to #25265
vladserkoff Mar 20, 2019
c9c4d87
add another test case
vladserkoff Mar 20, 2019
c313cca
add note to watsnew
vladserkoff Mar 20, 2019
d0acd20
Merge branch 'master' into integer-array-from-bool
vladserkoff Mar 20, 2019
7c24ea8
Fix passing result_dtype as string
vladserkoff Mar 20, 2019
c18cf28
Merge branch 'master' into integer-array-from-bool
vladserkoff Mar 25, 2019
54431e4
possible fix for integer_array with dtype=None
vladserkoff Mar 25, 2019
986548f
fix pep8
vladserkoff Mar 25, 2019
c21412c
add test cases with dtype=None
vladserkoff Mar 26, 2019
775114a
fix tests
vladserkoff Mar 26, 2019
cb207aa
fix pep8
vladserkoff Mar 26, 2019
e6655bc
quickfix failing s.combine on extention array
vladserkoff Apr 10, 2019
96fa493
fix for infering dtype in failing s.combine on extention array
vladserkoff Apr 11, 2019
089a72a
fix for infering dtype in failing s.combine on extention array
vladserkoff Apr 11, 2019
f2e2eac
Merge branch 'master' into integer-array-from-bool
vladserkoff Apr 22, 2019
99a6678
Merge branch 'master' into integer-array-from-bool
vladserkoff May 14, 2019
3bce8a1
revert to c18cf28
vladserkoff May 14, 2019
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/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ Numeric
- Bug in :meth:`to_numeric` in which numbers were being coerced to float, even though ``errors`` was not ``coerce`` (:issue:`24910`)
- Bug in error messages in :meth:`DataFrame.corr` and :meth:`Series.corr`. Added the possibility of using a callable. (:issue:`25729`)
- 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`)
-
- Fixed bug where casting all-boolean array to integer extension array failed (:issue:`25211`)
-
-

Expand Down
3 changes: 3 additions & 0 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ def coerce_to_array(values, dtype, mask=None, copy=False):
raise TypeError("{} cannot be converted to an IntegerDtype".format(
values.dtype))

elif is_bool_dtype(values) and is_integer_dtype(dtype):
values = np.array(values, dtype=int, copy=copy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.asarray?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's like in other places in the code, e.g.

values = np.array(values, copy=copy)


elif not (is_integer_dtype(values) or is_float_dtype(values)):
raise TypeError("{} cannot be converted to an IntegerDtype".format(
values.dtype))
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/arrays/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,18 @@ def test_to_integer_array_float():
assert result.dtype == Int64Dtype()


@pytest.mark.parametrize(
'bool_values, int_values, result_dtype',
[([False, True], [0, 1], Int64Dtype()),
([False, True], [0, 1], 'Int64'),
([False, True, np.nan], [0, 1, np.nan], Int64Dtype())])
def test_to_integer_array_bool(bool_values, int_values, result_dtype):
result = integer_array(bool_values, dtype=result_dtype)
assert result.dtype == result_dtype
expected = integer_array(int_values, dtype=result_dtype)
tm.assert_extension_array_equal(result, expected)


@pytest.mark.parametrize(
'values, to_dtype, result_dtype',
[
Expand Down