@@ -48,30 +48,37 @@ def setup_method(self, method):
48
48
def teardown_method (self , method ):
49
49
expr ._MIN_ELEMENTS = self ._MIN_ELEMENTS
50
50
51
- def run_arithmetic (self , df , other ):
51
+ @staticmethod
52
+ def call_op (df , other , flex : bool , opname : str ):
53
+ if flex :
54
+ op = lambda x , y : getattr (x , opname )(y )
55
+ op .__name__ = opname
56
+ else :
57
+ op = getattr (operator , opname )
58
+
59
+ expr .set_use_numexpr (False )
60
+ expected = op (df , other )
61
+ expr .set_use_numexpr (True )
62
+
63
+ expr .get_test_result ()
64
+
65
+ result = op (df , other )
66
+ return result , expected
67
+
68
+ def run_arithmetic (self , df , other , flex : bool ):
52
69
expr ._MIN_ELEMENTS = 0
53
70
operations = ["add" , "sub" , "mul" , "mod" , "truediv" , "floordiv" ]
54
- for test_flex in [ True , False ] :
55
- for arith in operations :
56
- # TODO: share with run_binary
57
- if test_flex :
58
- op = lambda x , y : getattr ( x , arith )( y )
59
- op . __name__ = arith
71
+ for arith in operations :
72
+ result , expected = self . call_op ( df , other , flex , arith )
73
+
74
+ if arith == "truediv" :
75
+ if expected . ndim == 1 :
76
+ assert expected . dtype . kind == "f"
60
77
else :
61
- op = getattr (operator , arith )
62
- expr .set_use_numexpr (False )
63
- expected = op (df , other )
64
- expr .set_use_numexpr (True )
65
-
66
- result = op (df , other )
67
- if arith == "truediv" :
68
- if expected .ndim == 1 :
69
- assert expected .dtype .kind == "f"
70
- else :
71
- assert all (x .kind == "f" for x in expected .dtypes .values )
72
- tm .assert_equal (expected , result )
73
-
74
- def run_binary (self , df , other ):
78
+ assert all (x .kind == "f" for x in expected .dtypes .values )
79
+ tm .assert_equal (expected , result )
80
+
81
+ def run_binary (self , df , other , flex : bool ):
75
82
"""
76
83
tests solely that the result is the same whether or not numexpr is
77
84
enabled. Need to test whether the function does the correct thing
@@ -81,37 +88,27 @@ def run_binary(self, df, other):
81
88
expr .set_test_mode (True )
82
89
operations = ["gt" , "lt" , "ge" , "le" , "eq" , "ne" ]
83
90
84
- for test_flex in [True , False ]:
85
- for arith in operations :
86
- if test_flex :
87
- op = lambda x , y : getattr (x , arith )(y )
88
- op .__name__ = arith
89
- else :
90
- op = getattr (operator , arith )
91
- expr .set_use_numexpr (False )
92
- expected = op (df , other )
93
- expr .set_use_numexpr (True )
94
-
95
- expr .get_test_result ()
96
- result = op (df , other )
97
- used_numexpr = expr .get_test_result ()
98
- assert used_numexpr , "Did not use numexpr as expected."
99
- tm .assert_equal (expected , result )
100
-
101
- def run_frame (self , df , other , run_binary = True ):
102
- self .run_arithmetic (df , other )
103
- if run_binary :
104
- expr .set_use_numexpr (False )
105
- binary_comp = other + 1
106
- expr .set_use_numexpr (True )
107
- self .run_binary (df , binary_comp )
91
+ for arith in operations :
92
+ result , expected = self .call_op (df , other , flex , arith )
93
+
94
+ used_numexpr = expr .get_test_result ()
95
+ assert used_numexpr , "Did not use numexpr as expected."
96
+ tm .assert_equal (expected , result )
97
+
98
+ def run_frame (self , df , other , flex : bool ):
99
+ self .run_arithmetic (df , other , flex )
100
+
101
+ expr .set_use_numexpr (False )
102
+ binary_comp = other + 1
103
+ expr .set_use_numexpr (True )
104
+ self .run_binary (df , binary_comp , flex )
108
105
109
106
for i in range (len (df .columns )):
110
- self .run_arithmetic (df .iloc [:, i ], other .iloc [:, i ])
107
+ self .run_arithmetic (df .iloc [:, i ], other .iloc [:, i ], flex )
111
108
# FIXME: dont leave commented-out
112
109
# series doesn't uses vec_compare instead of numexpr...
113
110
# binary_comp = other.iloc[:, i] + 1
114
- # self.run_binary(df.iloc[:, i], binary_comp)
111
+ # self.run_binary(df.iloc[:, i], binary_comp, flex )
115
112
116
113
@pytest .mark .parametrize (
117
114
"df" ,
@@ -126,14 +123,9 @@ def run_frame(self, df, other, run_binary=True):
126
123
_mixed2 ,
127
124
],
128
125
)
129
- def test_arithmetic (self , df ):
130
- # TODO: FIGURE OUT HOW TO GET RUN_BINARY TO WORK WITH MIXED=...
131
- # can't do arithmetic because comparison methods try to do *entire*
132
- # frame instead of by-column
133
- kinds = {x .kind for x in df .dtypes .values }
134
- should = len (kinds ) == 1
135
-
136
- self .run_frame (df , df , run_binary = should )
126
+ @pytest .mark .parametrize ("flex" , [True , False ])
127
+ def test_arithmetic (self , df , flex ):
128
+ self .run_frame (df , df , flex )
137
129
138
130
def test_invalid (self ):
139
131
0 commit comments