Skip to content

Commit 447a595

Browse files
authored
CLN: Split up Boolean array tests (#32780)
1 parent f0fc6dd commit 447a595

12 files changed

+1003
-936
lines changed

pandas/tests/arrays/boolean/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import numpy as np
2+
import pytest
3+
4+
import pandas as pd
5+
from pandas.tests.extension.base import BaseOpsUtil
6+
7+
8+
@pytest.fixture
9+
def data():
10+
return pd.array(
11+
[True, False] * 4 + [np.nan] + [True, False] * 44 + [np.nan] + [True, False],
12+
dtype="boolean",
13+
)
14+
15+
16+
class TestArithmeticOps(BaseOpsUtil):
17+
def test_error(self, data, all_arithmetic_operators):
18+
# invalid ops
19+
20+
op = all_arithmetic_operators
21+
s = pd.Series(data)
22+
ops = getattr(s, op)
23+
opa = getattr(data, op)
24+
25+
# invalid scalars
26+
with pytest.raises(TypeError):
27+
ops("foo")
28+
with pytest.raises(TypeError):
29+
ops(pd.Timestamp("20180101"))
30+
31+
# invalid array-likes
32+
if op not in ("__mul__", "__rmul__"):
33+
# TODO(extension) numpy's mul with object array sees booleans as numbers
34+
with pytest.raises(TypeError):
35+
ops(pd.Series("foo", index=s.index))
36+
37+
# 2d
38+
result = opa(pd.DataFrame({"A": s}))
39+
assert result is NotImplemented
40+
41+
with pytest.raises(NotImplementedError):
42+
opa(np.arange(len(s)).reshape(-1, len(s)))
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import numpy as np
2+
import pytest
3+
4+
import pandas as pd
5+
import pandas._testing as tm
6+
7+
8+
def test_astype():
9+
# with missing values
10+
arr = pd.array([True, False, None], dtype="boolean")
11+
12+
with pytest.raises(ValueError, match="cannot convert NA to integer"):
13+
arr.astype("int64")
14+
15+
with pytest.raises(ValueError, match="cannot convert float NaN to"):
16+
arr.astype("bool")
17+
18+
result = arr.astype("float64")
19+
expected = np.array([1, 0, np.nan], dtype="float64")
20+
tm.assert_numpy_array_equal(result, expected)
21+
22+
result = arr.astype("str")
23+
expected = np.array(["True", "False", "<NA>"], dtype="object")
24+
tm.assert_numpy_array_equal(result, expected)
25+
26+
# no missing values
27+
arr = pd.array([True, False, True], dtype="boolean")
28+
result = arr.astype("int64")
29+
expected = np.array([1, 0, 1], dtype="int64")
30+
tm.assert_numpy_array_equal(result, expected)
31+
32+
result = arr.astype("bool")
33+
expected = np.array([True, False, True], dtype="bool")
34+
tm.assert_numpy_array_equal(result, expected)
35+
36+
37+
def test_astype_to_boolean_array():
38+
# astype to BooleanArray
39+
arr = pd.array([True, False, None], dtype="boolean")
40+
41+
result = arr.astype("boolean")
42+
tm.assert_extension_array_equal(result, arr)
43+
result = arr.astype(pd.BooleanDtype())
44+
tm.assert_extension_array_equal(result, arr)
45+
46+
47+
def test_astype_to_integer_array():
48+
# astype to IntegerArray
49+
arr = pd.array([True, False, None], dtype="boolean")
50+
51+
result = arr.astype("Int64")
52+
expected = pd.array([1, 0, None], dtype="Int64")
53+
tm.assert_extension_array_equal(result, expected)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import numpy as np
2+
import pytest
3+
4+
import pandas as pd
5+
import pandas._testing as tm
6+
from pandas.arrays import BooleanArray
7+
from pandas.tests.extension.base import BaseOpsUtil
8+
9+
10+
@pytest.fixture
11+
def data():
12+
return pd.array(
13+
[True, False] * 4 + [np.nan] + [True, False] * 44 + [np.nan] + [True, False],
14+
dtype="boolean",
15+
)
16+
17+
18+
class TestComparisonOps(BaseOpsUtil):
19+
def _compare_other(self, data, op_name, other):
20+
op = self.get_op_from_name(op_name)
21+
22+
# array
23+
result = pd.Series(op(data, other))
24+
expected = pd.Series(op(data._data, other), dtype="boolean")
25+
# propagate NAs
26+
expected[data._mask] = pd.NA
27+
28+
tm.assert_series_equal(result, expected)
29+
30+
# series
31+
s = pd.Series(data)
32+
result = op(s, other)
33+
34+
expected = pd.Series(data._data)
35+
expected = op(expected, other)
36+
expected = expected.astype("boolean")
37+
# propagate NAs
38+
expected[data._mask] = pd.NA
39+
40+
tm.assert_series_equal(result, expected)
41+
42+
def test_compare_scalar(self, data, all_compare_operators):
43+
op_name = all_compare_operators
44+
self._compare_other(data, op_name, True)
45+
46+
def test_compare_array(self, data, all_compare_operators):
47+
op_name = all_compare_operators
48+
other = pd.array([True] * len(data), dtype="boolean")
49+
self._compare_other(data, op_name, other)
50+
other = np.array([True] * len(data))
51+
self._compare_other(data, op_name, other)
52+
other = pd.Series([True] * len(data))
53+
self._compare_other(data, op_name, other)
54+
55+
@pytest.mark.parametrize("other", [True, False, pd.NA])
56+
def test_scalar(self, other, all_compare_operators):
57+
op = self.get_op_from_name(all_compare_operators)
58+
a = pd.array([True, False, None], dtype="boolean")
59+
60+
result = op(a, other)
61+
62+
if other is pd.NA:
63+
expected = pd.array([None, None, None], dtype="boolean")
64+
else:
65+
values = op(a._data, other)
66+
expected = BooleanArray(values, a._mask, copy=True)
67+
tm.assert_extension_array_equal(result, expected)
68+
69+
# ensure we haven't mutated anything inplace
70+
result[0] = None
71+
tm.assert_extension_array_equal(
72+
a, pd.array([True, False, None], dtype="boolean")
73+
)
74+
75+
def test_array(self, all_compare_operators):
76+
op = self.get_op_from_name(all_compare_operators)
77+
a = pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
78+
b = pd.array([True, False, None] * 3, dtype="boolean")
79+
80+
result = op(a, b)
81+
82+
values = op(a._data, b._data)
83+
mask = a._mask | b._mask
84+
expected = BooleanArray(values, mask)
85+
tm.assert_extension_array_equal(result, expected)
86+
87+
# ensure we haven't mutated anything inplace
88+
result[0] = None
89+
tm.assert_extension_array_equal(
90+
a, pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
91+
)
92+
tm.assert_extension_array_equal(
93+
b, pd.array([True, False, None] * 3, dtype="boolean")
94+
)

0 commit comments

Comments
 (0)