Skip to content

TST: add test for df comparing strings to numbers raises ValueError #29535

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 3 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
28 changes: 28 additions & 0 deletions pandas/tests/frame/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,34 @@ def test_comp(func):
test_comp(operator.ge)
test_comp(operator.le)

@pytest.mark.parametrize("op", ["lt", "le", "gt", "ge", "eq", "ne"])
Copy link
Member

Choose a reason for hiding this comment

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

Can you use the compare_operators_no_eq_ne pytest fixture instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, @mroeschke - that was neat! Now using compare_operators_no_eq_ne.

def test_strings_to_numbers_comparisons(self, op):
# GH 11565
df = DataFrame(
{x: {"x": "foo", "y": "bar", "z": "baz"} for x in ["a", "b", "c"]}
)

f = getattr(operator, op)
if op == "eq":
result = f(df, 0)
expected = DataFrame(
{x: {y: False for y in ["x", "y", "z"]} for x in ["a", "b", "c"]}
)

tm.assert_frame_equal(result, expected)

elif op == "ne":
result = f(df, 0)
expected = DataFrame(
{x: {y: True for y in ["x", "y", "z"]} for x in ["a", "b", "c"]}
)

tm.assert_frame_equal(result, expected)

else:
with pytest.raises(TypeError):
f(df, 0)

def test_comparison_protected_from_errstate(self):
missing_df = tm.makeDataFrame()
missing_df.iloc[0]["A"] = np.nan
Expand Down