Skip to content

Commit 43b92df

Browse files
GH1411 Add __and__ for BooleanArray and tests (#1480)
* GH1411 Add __and__ for BooleanArray and tests * GH1411 Add __and__ for BooleanArray and tests * GH1411 Change __and__ method to & * GH1411 PR Feedback * Update tests/test_extension.py Co-authored-by: Yi-Fan Wang <[email protected]> --------- Co-authored-by: Yi-Fan Wang <[email protected]>
1 parent bc6e9d4 commit 43b92df

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

pandas-stubs/core/arrays/boolean.pyi

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
from collections.abc import Sequence
12
from typing import Any
23

4+
import numpy as np
5+
from pandas.core.arrays.integer import IntegerArray
36
from pandas.core.arrays.masked import BaseMaskedArray as BaseMaskedArray
7+
from typing_extensions import Self
48

59
from pandas._libs.missing import NAType
610
from pandas._typing import (
@@ -25,3 +29,27 @@ class BooleanArray(BaseMaskedArray):
2529
def __setitem__(self, key, value) -> None: ...
2630
def any(self, *, skipna: bool = ..., **kwargs: Any): ...
2731
def all(self, *, skipna: bool = ..., **kwargs: Any): ...
32+
def __and__(
33+
self,
34+
other: (
35+
bool
36+
| np.bool
37+
| NAType
38+
| Sequence[bool | np.bool]
39+
| np_ndarray_bool
40+
| IntegerArray
41+
| Self
42+
),
43+
) -> Self: ...
44+
def __rand__(
45+
self,
46+
other: (
47+
bool
48+
| np.bool
49+
| NAType
50+
| Sequence[bool | np.bool]
51+
| np_ndarray_bool
52+
| IntegerArray
53+
| Self
54+
),
55+
) -> Self: ...

tests/test_extension.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import pandas as pd
55
from pandas.arrays import IntegerArray
6+
from pandas.core.arrays.boolean import BooleanArray
67
from pandas.core.indexers import check_array_indexer
78
from typing_extensions import assert_type
89

@@ -56,3 +57,24 @@ def test_array_indexer() -> None:
5657
check(assert_type(check_array_indexer(arr, 1), int), int)
5758

5859
check(assert_type(check_array_indexer(arr, slice(0, 1, 1)), slice), slice)
60+
61+
62+
def test_boolean_array() -> None:
63+
"""Test creation of and operations on BooleanArray GH1411."""
64+
arr = pd.array([True], dtype="boolean")
65+
arr_bool = pd.array([True, False])
66+
arr_int = pd.array([3, 5])
67+
check(assert_type(arr, BooleanArray), BooleanArray)
68+
arr_and = arr & arr
69+
check(assert_type(arr_and, BooleanArray), BooleanArray)
70+
71+
check(assert_type(arr_bool & True, BooleanArray), BooleanArray)
72+
check(assert_type(arr_bool & np.bool(True), BooleanArray), BooleanArray)
73+
check(assert_type(arr_bool & pd.NA, BooleanArray), BooleanArray)
74+
check(assert_type(arr_bool & [True, False], BooleanArray), BooleanArray)
75+
check(assert_type(arr_bool & [np.bool(True), False], BooleanArray), BooleanArray)
76+
# TODO: pandas-dev/pandas#63095
77+
# check(assert_type(b & [pd.NA, False])
78+
check(assert_type(arr_bool & np.array([True, False]), BooleanArray), BooleanArray)
79+
check(assert_type(arr_bool & arr_int, BooleanArray), BooleanArray)
80+
check(assert_type(arr_bool & arr_bool, BooleanArray), BooleanArray)

0 commit comments

Comments
 (0)