From a5199fef8c0b8a0b2100d9734b490dfa20e81960 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Sun, 10 Nov 2019 20:13:26 +0000 Subject: [PATCH 1/5] add test for comparing strings to numbers df raises ValueError --- pandas/tests/series/test_operators.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index 7d212ee7cd667..dcd0186185300 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -557,6 +557,14 @@ def test_categorical_comparisons(self): with pytest.raises(TypeError): b > a + def test_strings_to_numbers_comparisons_raises(self): + # GH 11565 + df = DataFrame( + {x: {"x": "foo", "y": "bar", "z": "baz"} for x in ["a", "b", "c"]} + ) + with pytest.raises(TypeError): + df > 0 + def test_comparison_tuples(self): # GH11339 # comparisons vs tuple From 8f9000b3cb8a6796b1f782dc36dfca49af9dfdf9 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Sun, 10 Nov 2019 20:15:22 +0000 Subject: [PATCH 2/5] move test --- pandas/tests/frame/test_operators.py | 8 ++++++++ pandas/tests/series/test_operators.py | 8 -------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/tests/frame/test_operators.py b/pandas/tests/frame/test_operators.py index 19d91241d6a6b..a188a78213486 100644 --- a/pandas/tests/frame/test_operators.py +++ b/pandas/tests/frame/test_operators.py @@ -530,6 +530,14 @@ def test_comp(func): test_comp(operator.ge) test_comp(operator.le) + def test_strings_to_numbers_comparisons_raises(self): + # GH 11565 + df = DataFrame( + {x: {"x": "foo", "y": "bar", "z": "baz"} for x in ["a", "b", "c"]} + ) + with pytest.raises(TypeError): + df > 0 + def test_comparison_protected_from_errstate(self): missing_df = tm.makeDataFrame() missing_df.iloc[0]["A"] = np.nan diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index dcd0186185300..7d212ee7cd667 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -557,14 +557,6 @@ def test_categorical_comparisons(self): with pytest.raises(TypeError): b > a - def test_strings_to_numbers_comparisons_raises(self): - # GH 11565 - df = DataFrame( - {x: {"x": "foo", "y": "bar", "z": "baz"} for x in ["a", "b", "c"]} - ) - with pytest.raises(TypeError): - df > 0 - def test_comparison_tuples(self): # GH11339 # comparisons vs tuple From 0b2b6a9b63dc6666977b235ecff125d69e4d31cb Mon Sep 17 00:00:00 2001 From: ganevgv Date: Mon, 11 Nov 2019 21:45:37 +0000 Subject: [PATCH 3/5] add parameterize over all operators --- pandas/tests/frame/test_operators.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/pandas/tests/frame/test_operators.py b/pandas/tests/frame/test_operators.py index a188a78213486..e16b8260709fc 100644 --- a/pandas/tests/frame/test_operators.py +++ b/pandas/tests/frame/test_operators.py @@ -530,13 +530,33 @@ def test_comp(func): test_comp(operator.ge) test_comp(operator.le) - def test_strings_to_numbers_comparisons_raises(self): + @pytest.mark.parametrize("op", ["lt", "le", "gt", "ge", "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"]} ) - with pytest.raises(TypeError): - df > 0 + + 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() From a3795b35bdfdf6c7bcd3cafed4e02da3ecb8f594 Mon Sep 17 00:00:00 2001 From: ganevgv Date: Tue, 12 Nov 2019 00:31:13 +0000 Subject: [PATCH 4/5] use compare_operators_no_eq_ne --- pandas/tests/frame/test_operators.py | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/pandas/tests/frame/test_operators.py b/pandas/tests/frame/test_operators.py index e16b8260709fc..f2068d3eeb3ad 100644 --- a/pandas/tests/frame/test_operators.py +++ b/pandas/tests/frame/test_operators.py @@ -530,33 +530,15 @@ def test_comp(func): test_comp(operator.ge) test_comp(operator.le) - @pytest.mark.parametrize("op", ["lt", "le", "gt", "ge", "eq", "ne"]) - def test_strings_to_numbers_comparisons(self, op): + def test_strings_to_numbers_comparisons(self, compare_operators_no_eq_ne): # 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) + f = getattr(operator, compare_operators_no_eq_ne) + with pytest.raises(TypeError): + f(df, 0) def test_comparison_protected_from_errstate(self): missing_df = tm.makeDataFrame() From a70c14322fc8c3cb3956657b01d1fda2331e5e8e Mon Sep 17 00:00:00 2001 From: ganevgv Date: Tue, 12 Nov 2019 01:18:56 +0000 Subject: [PATCH 5/5] add _rasises to test name --- pandas/tests/frame/test_operators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_operators.py b/pandas/tests/frame/test_operators.py index f2068d3eeb3ad..ac60f04248da5 100644 --- a/pandas/tests/frame/test_operators.py +++ b/pandas/tests/frame/test_operators.py @@ -530,7 +530,7 @@ def test_comp(func): test_comp(operator.ge) test_comp(operator.le) - def test_strings_to_numbers_comparisons(self, compare_operators_no_eq_ne): + def test_strings_to_numbers_comparisons_raises(self, compare_operators_no_eq_ne): # GH 11565 df = DataFrame( {x: {"x": "foo", "y": "bar", "z": "baz"} for x in ["a", "b", "c"]}