From 25d1300fa387be513adfa25f6b5817e56cf88c7f Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 23 Jan 2020 17:20:47 -0800 Subject: [PATCH 1/3] pass str_rep, parametrize tests --- pandas/core/ops/__init__.py | 4 +- pandas/tests/frame/test_query_eval.py | 58 +++++++++++++-------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 1355060efd097..7df6a8e4fd8b6 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -750,7 +750,7 @@ def f(self, other, axis=default_axis, level=None): ) else: # in this case we always have `np.ndim(other) == 0` - new_data = dispatch_to_series(self, other, op) + new_data = dispatch_to_series(self, other, op, str_rep) return self._construct_result(new_data) f.__name__ = op_name @@ -784,7 +784,7 @@ def f(self, other): # straight boolean comparisons we want to allow all columns # (regardless of dtype to pass thru) See #4537 for discussion. - new_data = dispatch_to_series(self, other, op) + new_data = dispatch_to_series(self, other, op, str_rep) return self._construct_result(new_data) f.__name__ = op_name diff --git a/pandas/tests/frame/test_query_eval.py b/pandas/tests/frame/test_query_eval.py index 703e05998e93c..3ced02886fbab 100644 --- a/pandas/tests/frame/test_query_eval.py +++ b/pandas/tests/frame/test_query_eval.py @@ -78,45 +78,45 @@ def test_query_numexpr(self): class TestDataFrameEval: - def test_ops(self): + + # smaller hits python, larger hits numexpr + @pytest.mark.parametrize("n", [4, 4000]) + @pytest.mark.parametrize("op_str,op,rop", [ + ("+", "__add__", "__radd__"), + ("-", "__sub__", "__rsub__"), + ("*", "__mul__", "__rmul__"), + ("/", "__truediv__", "__rtruediv__"), + ]) + def test_ops(self, op_str, op, rop, n): # tst ops and reversed ops in evaluation # GH7198 - # smaller hits python, larger hits numexpr - for n in [4, 4000]: - - df = DataFrame(1, index=range(n), columns=list("abcd")) - df.iloc[0] = 2 - m = df.mean() + df = DataFrame(1, index=range(n), columns=list("abcd")) + df.iloc[0] = 2 + m = df.mean() - for op_str, op, rop in [ - ("+", "__add__", "__radd__"), - ("-", "__sub__", "__rsub__"), - ("*", "__mul__", "__rmul__"), - ("/", "__truediv__", "__rtruediv__"), - ]: - - base = DataFrame( # noqa - np.tile(m.values, n).reshape(n, -1), columns=list("abcd") - ) + base = DataFrame( # noqa + np.tile(m.values, n).reshape(n, -1), columns=list("abcd") + ) - expected = eval("base{op}df".format(op=op_str)) + expected = eval("base{op}df".format(op=op_str)) - # ops as strings - result = eval("m{op}df".format(op=op_str)) - tm.assert_frame_equal(result, expected) + # ops as strings + result = eval("m{op}df".format(op=op_str)) + tm.assert_frame_equal(result, expected) - # these are commutative - if op in ["+", "*"]: - result = getattr(df, op)(m) - tm.assert_frame_equal(result, expected) + # these are commutative + if op in ["+", "*"]: + result = getattr(df, op)(m) + tm.assert_frame_equal(result, expected) - # these are not - elif op in ["-", "/"]: - result = getattr(df, rop)(m) - tm.assert_frame_equal(result, expected) + # these are not + elif op in ["-", "/"]: + result = getattr(df, rop)(m) + tm.assert_frame_equal(result, expected) + def test_dataframe_sub_numexpr_path(self): # GH7192: Note we need a large number of rows to ensure this # goes through the numexpr path df = DataFrame(dict(A=np.random.randn(25000))) From 90fa2a1811c8349ea73859d6a4327c4fed4c17f4 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 24 Jan 2020 15:07:59 -0800 Subject: [PATCH 2/3] use fstring --- pandas/core/ops/__init__.py | 2 +- pandas/tests/frame/test_query_eval.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 98cf94135546d..e097f85532c7b 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -778,7 +778,7 @@ def f(self, other): self, other = self.align( other, join="outer", axis=1, level=None, copy=False ) - new_data = dispatch_to_series(self, other, op, axis="columns", str_rep=str_rep) + new_data = dispatch_to_series(self, other, op, str_rep=str_rep, axis="columns") else: diff --git a/pandas/tests/frame/test_query_eval.py b/pandas/tests/frame/test_query_eval.py index 3ced02886fbab..facca2f6b43e8 100644 --- a/pandas/tests/frame/test_query_eval.py +++ b/pandas/tests/frame/test_query_eval.py @@ -100,10 +100,10 @@ def test_ops(self, op_str, op, rop, n): np.tile(m.values, n).reshape(n, -1), columns=list("abcd") ) - expected = eval("base{op}df".format(op=op_str)) + expected = eval(f"base {op_str} df") # ops as strings - result = eval("m{op}df".format(op=op_str)) + result = eval(f"m {op_str} df") tm.assert_frame_equal(result, expected) # these are commutative From cba3728f2a800cd60b406af17b7abd4736ed39f7 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 24 Jan 2020 15:12:53 -0800 Subject: [PATCH 3/3] blackify --- pandas/core/ops/__init__.py | 4 +++- pandas/tests/frame/test_query_eval.py | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index e097f85532c7b..ceb94ed2c47df 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -778,7 +778,9 @@ def f(self, other): self, other = self.align( other, join="outer", axis=1, level=None, copy=False ) - new_data = dispatch_to_series(self, other, op, str_rep=str_rep, axis="columns") + new_data = dispatch_to_series( + self, other, op, str_rep=str_rep, axis="columns" + ) else: diff --git a/pandas/tests/frame/test_query_eval.py b/pandas/tests/frame/test_query_eval.py index facca2f6b43e8..318467acbef95 100644 --- a/pandas/tests/frame/test_query_eval.py +++ b/pandas/tests/frame/test_query_eval.py @@ -81,12 +81,15 @@ class TestDataFrameEval: # smaller hits python, larger hits numexpr @pytest.mark.parametrize("n", [4, 4000]) - @pytest.mark.parametrize("op_str,op,rop", [ + @pytest.mark.parametrize( + "op_str,op,rop", + [ ("+", "__add__", "__radd__"), ("-", "__sub__", "__rsub__"), ("*", "__mul__", "__rmul__"), ("/", "__truediv__", "__rtruediv__"), - ]) + ], + ) def test_ops(self, op_str, op, rop, n): # tst ops and reversed ops in evaluation