-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
CLN: Split up Boolean array tests #32780
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b332b81
Add boolean test dir
dsaxton e0ddb1b
Remove test_boolean.py
dsaxton 1887e96
Don't import unused
dsaxton 189d53d
Add new test files
dsaxton 42dd4c2
Fixme
dsaxton d457648
Move to reductions
dsaxton d2d4e10
Move to reductions
dsaxton 39c1ef2
Delete test_misc.py
dsaxton bb003fe
Don't import unused
dsaxton 33c35eb
Replace bloated fixture
dsaxton f7cea92
Merge remote-tracking branch 'upstream/master' into ref-array-test
dsaxton 13e6996
Rename
dsaxton 4fbae13
Merge remote-tracking branch 'upstream/master' into ref-array-test
dsaxton f726667
Merge remote-tracking branch 'upstream/master' into ref-array-test
dsaxton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
from pandas.tests.extension.base import BaseOpsUtil | ||
|
||
|
||
@pytest.fixture | ||
def data(): | ||
return pd.array( | ||
[True, False] * 4 + [np.nan] + [True, False] * 44 + [np.nan] + [True, False], | ||
dtype="boolean", | ||
) | ||
|
||
|
||
class TestArithmeticOps(BaseOpsUtil): | ||
def test_error(self, data, all_arithmetic_operators): | ||
# invalid ops | ||
|
||
op = all_arithmetic_operators | ||
s = pd.Series(data) | ||
ops = getattr(s, op) | ||
opa = getattr(data, op) | ||
|
||
# invalid scalars | ||
with pytest.raises(TypeError): | ||
ops("foo") | ||
with pytest.raises(TypeError): | ||
ops(pd.Timestamp("20180101")) | ||
|
||
# invalid array-likes | ||
if op not in ("__mul__", "__rmul__"): | ||
# TODO(extension) numpy's mul with object array sees booleans as numbers | ||
with pytest.raises(TypeError): | ||
ops(pd.Series("foo", index=s.index)) | ||
|
||
# 2d | ||
result = opa(pd.DataFrame({"A": s})) | ||
assert result is NotImplemented | ||
|
||
with pytest.raises(NotImplementedError): | ||
opa(np.arange(len(s)).reshape(-1, len(s))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
import pandas._testing as tm | ||
|
||
|
||
def test_astype(): | ||
# with missing values | ||
arr = pd.array([True, False, None], dtype="boolean") | ||
|
||
with pytest.raises(ValueError, match="cannot convert NA to integer"): | ||
arr.astype("int64") | ||
|
||
with pytest.raises(ValueError, match="cannot convert float NaN to"): | ||
arr.astype("bool") | ||
|
||
result = arr.astype("float64") | ||
expected = np.array([1, 0, np.nan], dtype="float64") | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
result = arr.astype("str") | ||
expected = np.array(["True", "False", "<NA>"], dtype="object") | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
# no missing values | ||
arr = pd.array([True, False, True], dtype="boolean") | ||
result = arr.astype("int64") | ||
expected = np.array([1, 0, 1], dtype="int64") | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
result = arr.astype("bool") | ||
expected = np.array([True, False, True], dtype="bool") | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
|
||
def test_astype_to_boolean_array(): | ||
# astype to BooleanArray | ||
arr = pd.array([True, False, None], dtype="boolean") | ||
|
||
result = arr.astype("boolean") | ||
tm.assert_extension_array_equal(result, arr) | ||
result = arr.astype(pd.BooleanDtype()) | ||
tm.assert_extension_array_equal(result, arr) | ||
|
||
|
||
def test_astype_to_integer_array(): | ||
# astype to IntegerArray | ||
arr = pd.array([True, False, None], dtype="boolean") | ||
|
||
result = arr.astype("Int64") | ||
expected = pd.array([1, 0, None], dtype="Int64") | ||
tm.assert_extension_array_equal(result, expected) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
import pandas._testing as tm | ||
from pandas.arrays import BooleanArray | ||
from pandas.tests.extension.base import BaseOpsUtil | ||
|
||
|
||
@pytest.fixture | ||
def data(): | ||
return pd.array( | ||
[True, False] * 4 + [np.nan] + [True, False] * 44 + [np.nan] + [True, False], | ||
dtype="boolean", | ||
) | ||
|
||
|
||
class TestComparisonOps(BaseOpsUtil): | ||
def _compare_other(self, data, op_name, other): | ||
op = self.get_op_from_name(op_name) | ||
|
||
# array | ||
result = pd.Series(op(data, other)) | ||
expected = pd.Series(op(data._data, other), dtype="boolean") | ||
# propagate NAs | ||
expected[data._mask] = pd.NA | ||
|
||
tm.assert_series_equal(result, expected) | ||
|
||
# series | ||
s = pd.Series(data) | ||
result = op(s, other) | ||
|
||
expected = pd.Series(data._data) | ||
expected = op(expected, other) | ||
expected = expected.astype("boolean") | ||
# propagate NAs | ||
expected[data._mask] = pd.NA | ||
|
||
tm.assert_series_equal(result, expected) | ||
|
||
def test_compare_scalar(self, data, all_compare_operators): | ||
op_name = all_compare_operators | ||
self._compare_other(data, op_name, True) | ||
|
||
def test_compare_array(self, data, all_compare_operators): | ||
op_name = all_compare_operators | ||
other = pd.array([True] * len(data), dtype="boolean") | ||
self._compare_other(data, op_name, other) | ||
other = np.array([True] * len(data)) | ||
self._compare_other(data, op_name, other) | ||
other = pd.Series([True] * len(data)) | ||
self._compare_other(data, op_name, other) | ||
|
||
@pytest.mark.parametrize("other", [True, False, pd.NA]) | ||
def test_scalar(self, other, all_compare_operators): | ||
op = self.get_op_from_name(all_compare_operators) | ||
a = pd.array([True, False, None], dtype="boolean") | ||
|
||
result = op(a, other) | ||
|
||
if other is pd.NA: | ||
expected = pd.array([None, None, None], dtype="boolean") | ||
else: | ||
values = op(a._data, other) | ||
expected = BooleanArray(values, a._mask, copy=True) | ||
tm.assert_extension_array_equal(result, expected) | ||
|
||
# ensure we haven't mutated anything inplace | ||
result[0] = None | ||
tm.assert_extension_array_equal( | ||
a, pd.array([True, False, None], dtype="boolean") | ||
) | ||
|
||
def test_array(self, all_compare_operators): | ||
op = self.get_op_from_name(all_compare_operators) | ||
a = pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean") | ||
b = pd.array([True, False, None] * 3, dtype="boolean") | ||
|
||
result = op(a, b) | ||
|
||
values = op(a._data, b._data) | ||
mask = a._mask | b._mask | ||
expected = BooleanArray(values, mask) | ||
tm.assert_extension_array_equal(result, expected) | ||
|
||
# ensure we haven't mutated anything inplace | ||
result[0] = None | ||
tm.assert_extension_array_equal( | ||
a, pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean") | ||
) | ||
tm.assert_extension_array_equal( | ||
b, pd.array([True, False, None] * 3, dtype="boolean") | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should these be parametrized over EA/Series/DataFrame? (and eventually ExtensionIndex)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, I'm trying to avoid making any actual logic changes to tests here though since the PR is already quite big