-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TST/REF: arithmetic tests for BooleanArray + consolidate with integer masked tests #34623
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
Changes from 1 commit
71ca60e
125e7f0
841956f
8e88544
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import operator | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
from pandas.tests.extension.base import BaseOpsUtil | ||
import pandas._testing as tm | ||
|
||
|
||
@pytest.fixture | ||
|
@@ -13,30 +15,87 @@ def data(): | |
) | ||
|
||
|
||
class TestArithmeticOps(BaseOpsUtil): | ||
def test_error(self, data, all_arithmetic_operators): | ||
# invalid ops | ||
@pytest.fixture | ||
def a(): | ||
return pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean") | ||
|
||
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")) | ||
@pytest.fixture | ||
def b(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be burdensome to give a and b more informative names? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have a suggestion what you would find clearer? Basically, before moving it to a fixture, I had:
in each test, and I would think in such a context you would be fine with those names? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would call these left_array, right_array There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed |
||
return pd.array([True, False, None] * 3, dtype="boolean") | ||
|
||
|
||
# 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)) | ||
# Basic test for the arithmetic array ops | ||
# ----------------------------------------------------------------------------- | ||
|
||
# 2d | ||
result = opa(pd.DataFrame({"A": s})) | ||
assert result is NotImplemented | ||
|
||
with pytest.raises(NotImplementedError): | ||
opa(np.arange(len(s)).reshape(-1, len(s))) | ||
@pytest.mark.parametrize( | ||
"opname, exp", | ||
[ | ||
("add", [True, True, None, True, False, None, None, None, None]), | ||
("mul", [True, False, None, False, False, None, None, None, None]), | ||
], | ||
ids=["add", "mul"], | ||
) | ||
def test_add_mul(a, b, opname, exp): | ||
op = getattr(operator, opname) | ||
result = op(a, b) | ||
expected = pd.array(exp, dtype="boolean") | ||
tm.assert_extension_array_equal(result, expected) | ||
|
||
|
||
def test_sub(a, b): | ||
with pytest.raises(TypeError): | ||
# numpy points to ^ operator or logical_xor function instead | ||
a - b | ||
|
||
|
||
def test_div(a, b): | ||
# for now division gives a float numpy array | ||
result = a / b | ||
expected = np.array( | ||
[1.0, np.inf, np.nan, 0.0, np.nan, np.nan, np.nan, np.nan, np.nan], | ||
dtype="float64", | ||
) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"opname", | ||
[ | ||
"floordiv", | ||
"mod", | ||
pytest.param( | ||
"pow", marks=pytest.mark.xfail(reason="TODO follow int8 behaviour?") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there an issue number? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not yet, will open one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -> #34686 |
||
), | ||
], | ||
) | ||
def test_op_int8(a, b, opname): | ||
op = getattr(operator, opname) | ||
result = op(a, b) | ||
expected = op(a.astype("Int8"), b.astype("Int8")) | ||
tm.assert_extension_array_equal(result, expected) | ||
|
||
|
||
# Test generic charachteristics / errors | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sp? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does "sp" point to the typo in this title? Or did you mean something else? |
||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
def test_error_invalid_values(data, all_arithmetic_operators): | ||
# invalid ops | ||
|
||
op = all_arithmetic_operators | ||
s = pd.Series(data) | ||
ops = getattr(s, 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)) |
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.
can you type op_name and return type