Skip to content

CLN: Clean test_arithmetic.py #36390

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 3 commits into from
Sep 15, 2020
Merged
Changes from all commits
Commits
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
99 changes: 56 additions & 43 deletions pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ def test_comparison_flex_basic(self):
tm.assert_series_equal(left.gt(right), left > right)
tm.assert_series_equal(left.ge(right), left >= right)

# axis
for axis in [0, None, "index"]:
Copy link
Contributor

Choose a reason for hiding this comment

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

could parameterize here (might have to split the test)

tm.assert_series_equal(left.eq(right, axis=axis), left == right)
tm.assert_series_equal(left.ne(right, axis=axis), left != right)
Expand All @@ -280,7 +279,6 @@ def test_comparison_flex_basic(self):
tm.assert_series_equal(left.gt(right, axis=axis), left > right)
tm.assert_series_equal(left.ge(right, axis=axis), left >= right)

#
msg = "No axis named 1 for object type"
Copy link
Contributor

Choose a reason for hiding this comment

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

could use the fixture

for op in ["eq", "ne", "le", "le", "gt", "ge"]:
with pytest.raises(ValueError, match=msg):
Expand Down Expand Up @@ -553,68 +551,83 @@ def test_comparison_tuples(self):
expected = Series([True, False])
tm.assert_series_equal(result, expected)

def test_comparison_operators_with_nas(self):
def test_comparison_operators_with_nas(self, all_compare_operators):
op = all_compare_operators
ser = Series(bdate_range("1/1/2000", periods=10), dtype=object)
ser[::2] = np.nan

# test that comparisons work
ops = ["lt", "le", "gt", "ge", "eq", "ne"]
for op in ops:
val = ser[5]
f = getattr(operator, op)

f = getattr(operator, op)
result = f(ser, val)
# test that comparisons work
val = ser[5]

expected = f(ser.dropna(), val).reindex(ser.index)
result = f(ser, val)
expected = f(ser.dropna(), val).reindex(ser.index)

if op == "ne":
expected = expected.fillna(True).astype(bool)
else:
expected = expected.fillna(False).astype(bool)
if op == "__ne__":
expected = expected.fillna(True).astype(bool)
else:
expected = expected.fillna(False).astype(bool)

tm.assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# FIXME: dont leave commented-out
# fffffffuuuuuuuuuuuu
# result = f(val, s)
# expected = f(val, s.dropna()).reindex(s.index)
# tm.assert_series_equal(result, expected)
# FIXME: dont leave commented-out
# result = f(val, ser)
# expected = f(val, ser.dropna()).reindex(ser.index)
# tm.assert_series_equal(result, expected)

def test_ne(self):
ts = Series([3, 4, 5, 6, 7], [3, 4, 5, 6, 7], dtype=float)
expected = [True, True, False, True, True]
assert tm.equalContents(ts.index != 5, expected)
assert tm.equalContents(~(ts.index == 5), expected)

def test_comp_ops_df_compat(self):
@pytest.mark.parametrize(
"left, right",
[
(
pd.Series([1, 2, 3], index=list("ABC"), name="x"),
pd.Series([2, 2, 2], index=list("ABD"), name="x"),
),
(
pd.Series([1, 2, 3], index=list("ABC"), name="x"),
pd.Series([2, 2, 2, 2], index=list("ABCD"), name="x"),
),
],
)
def test_comp_ops_df_compat(self, left, right):
# GH 1134
s1 = pd.Series([1, 2, 3], index=list("ABC"), name="x")
s2 = pd.Series([2, 2, 2], index=list("ABD"), name="x")

s3 = pd.Series([1, 2, 3], index=list("ABC"), name="x")
s4 = pd.Series([2, 2, 2, 2], index=list("ABCD"), name="x")

for left, right in [(s1, s2), (s2, s1), (s3, s4), (s4, s3)]:

msg = "Can only compare identically-labeled Series objects"
with pytest.raises(ValueError, match=msg):
left == right
msg = "Can only compare identically-labeled Series objects"
with pytest.raises(ValueError, match=msg):
left == right
with pytest.raises(ValueError, match=msg):
right == left

with pytest.raises(ValueError, match=msg):
left != right
with pytest.raises(ValueError, match=msg):
left != right
with pytest.raises(ValueError, match=msg):
right != left

with pytest.raises(ValueError, match=msg):
left < right
with pytest.raises(ValueError, match=msg):
left < right
with pytest.raises(ValueError, match=msg):
right < left

msg = "Can only compare identically-labeled DataFrame objects"
with pytest.raises(ValueError, match=msg):
left.to_frame() == right.to_frame()
msg = "Can only compare identically-labeled DataFrame objects"
with pytest.raises(ValueError, match=msg):
left.to_frame() == right.to_frame()
with pytest.raises(ValueError, match=msg):
right.to_frame() == left.to_frame()

with pytest.raises(ValueError, match=msg):
left.to_frame() != right.to_frame()
with pytest.raises(ValueError, match=msg):
left.to_frame() != right.to_frame()
with pytest.raises(ValueError, match=msg):
right.to_frame() != left.to_frame()

with pytest.raises(ValueError, match=msg):
left.to_frame() < right.to_frame()
with pytest.raises(ValueError, match=msg):
left.to_frame() < right.to_frame()
with pytest.raises(ValueError, match=msg):
right.to_frame() < left.to_frame()

def test_compare_series_interval_keyword(self):
# GH#25338
Expand Down