@@ -127,132 +127,97 @@ def test_df_add_flex_filled_mixed_dtypes(self):
127127 'B' : ser * 2 })
128128 tm .assert_frame_equal (result , expected )
129129
130- def test_arith_flex_frame (self ):
131- seriesd = tm .getSeriesData ()
132- frame = pd .DataFrame (seriesd ).copy ()
133-
134- mixed_float = pd .DataFrame ({'A' : frame ['A' ].copy ().astype ('float32' ),
135- 'B' : frame ['B' ].copy ().astype ('float32' ),
136- 'C' : frame ['C' ].copy ().astype ('float16' ),
137- 'D' : frame ['D' ].copy ().astype ('float64' )})
138-
139- intframe = pd .DataFrame ({k : v .astype (int )
140- for k , v in seriesd .items ()})
141- mixed_int = pd .DataFrame ({'A' : intframe ['A' ].copy ().astype ('int32' ),
142- 'B' : np .ones (len (intframe ), dtype = 'uint64' ),
143- 'C' : intframe ['C' ].copy ().astype ('uint8' ),
144- 'D' : intframe ['D' ].copy ().astype ('int64' )})
145-
146- # force these all to int64 to avoid platform testing issues
147- intframe = pd .DataFrame ({c : s for c , s in intframe .items ()},
148- dtype = np .int64 )
149-
150- ops = ['add' , 'sub' , 'mul' , 'div' , 'truediv' , 'pow' , 'floordiv' , 'mod' ]
130+ @pytest .mark .parametrize ('op' , ['add' , 'sub' , 'mul' , 'div' , 'truediv' ,
131+ 'pow' , 'floordiv' , 'mod' ])
132+ def test_arith_flex_frame (self , op , int_frame , mixed_int_frame ,
133+ float_frame , mixed_float_frame ):
134+
151135 if not PY3 :
152136 aliases = {}
153137 else :
154138 aliases = {'div' : 'truediv' }
155-
156- for op in ops :
157- try :
158- alias = aliases .get (op , op )
159- f = getattr (operator , alias )
160- result = getattr (frame , op )(2 * frame )
161- exp = f (frame , 2 * frame )
139+ alias = aliases .get (op , op )
140+
141+ f = getattr (operator , alias )
142+ result = getattr (float_frame , op )(2 * float_frame )
143+ exp = f (float_frame , 2 * float_frame )
144+ tm .assert_frame_equal (result , exp )
145+
146+ # vs mix float
147+ result = getattr (mixed_float_frame , op )(2 * mixed_float_frame )
148+ exp = f (mixed_float_frame , 2 * mixed_float_frame )
149+ tm .assert_frame_equal (result , exp )
150+ _check_mixed_float (result , dtype = dict (C = None ))
151+
152+ # vs mix int
153+ if op in ['add' , 'sub' , 'mul' ]:
154+ result = getattr (mixed_int_frame , op )(2 + mixed_int_frame )
155+ exp = f (mixed_int_frame , 2 + mixed_int_frame )
156+
157+ # no overflow in the uint
158+ dtype = None
159+ if op in ['sub' ]:
160+ dtype = dict (B = 'uint64' , C = None )
161+ elif op in ['add' , 'mul' ]:
162+ dtype = dict (C = None )
163+ tm .assert_frame_equal (result , exp )
164+ _check_mixed_int (result , dtype = dtype )
165+
166+ # rops
167+ r_f = lambda x , y : f (y , x )
168+ result = getattr (float_frame , 'r' + op )(2 * float_frame )
169+ exp = r_f (float_frame , 2 * float_frame )
170+ tm .assert_frame_equal (result , exp )
171+
172+ # vs mix float
173+ result = getattr (mixed_float_frame , op )(2 * mixed_float_frame )
174+ exp = f (mixed_float_frame , 2 * mixed_float_frame )
175+ tm .assert_frame_equal (result , exp )
176+ _check_mixed_float (result , dtype = dict (C = None ))
177+
178+ result = getattr (int_frame , op )(2 * int_frame )
179+ exp = f (int_frame , 2 * int_frame )
180+ tm .assert_frame_equal (result , exp )
181+
182+ # vs mix int
183+ if op in ['add' , 'sub' , 'mul' ]:
184+ result = getattr (mixed_int_frame , op )(2 + mixed_int_frame )
185+ exp = f (mixed_int_frame , 2 + mixed_int_frame )
186+
187+ # no overflow in the uint
188+ dtype = None
189+ if op in ['sub' ]:
190+ dtype = dict (B = 'uint64' , C = None )
191+ elif op in ['add' , 'mul' ]:
192+ dtype = dict (C = None )
162193 tm .assert_frame_equal (result , exp )
194+ _check_mixed_int (result , dtype = dtype )
163195
164- # vs mix float
165- result = getattr (mixed_float , op )(2 * mixed_float )
166- exp = f (mixed_float , 2 * mixed_float )
167- tm .assert_frame_equal (result , exp )
168- _check_mixed_float (result , dtype = dict (C = None ))
169-
170- # vs mix int
171- if op in ['add' , 'sub' , 'mul' ]:
172- result = getattr (mixed_int , op )(2 + mixed_int )
173- exp = f (mixed_int , 2 + mixed_int )
174-
175- # no overflow in the uint
176- dtype = None
177- if op in ['sub' ]:
178- dtype = dict (B = 'uint64' , C = None )
179- elif op in ['add' , 'mul' ]:
180- dtype = dict (C = None )
181- tm .assert_frame_equal (result , exp )
182- _check_mixed_int (result , dtype = dtype )
183-
184- # rops
185- r_f = lambda x , y : f (y , x )
186- result = getattr (frame , 'r' + op )(2 * frame )
187- exp = r_f (frame , 2 * frame )
188- tm .assert_frame_equal (result , exp )
189-
190- # vs mix float
191- result = getattr (mixed_float , op )(2 * mixed_float )
192- exp = f (mixed_float , 2 * mixed_float )
193- tm .assert_frame_equal (result , exp )
194- _check_mixed_float (result , dtype = dict (C = None ))
195-
196- result = getattr (intframe , op )(2 * intframe )
197- exp = f (intframe , 2 * intframe )
198- tm .assert_frame_equal (result , exp )
199-
200- # vs mix int
201- if op in ['add' , 'sub' , 'mul' ]:
202- result = getattr (mixed_int , op )(2 + mixed_int )
203- exp = f (mixed_int , 2 + mixed_int )
204-
205- # no overflow in the uint
206- dtype = None
207- if op in ['sub' ]:
208- dtype = dict (B = 'uint64' , C = None )
209- elif op in ['add' , 'mul' ]:
210- dtype = dict (C = None )
211- tm .assert_frame_equal (result , exp )
212- _check_mixed_int (result , dtype = dtype )
213- except :
214- printing .pprint_thing ("Failing operation %r" % op )
215- raise
216-
217- # ndim >= 3
218- ndim_5 = np .ones (frame .shape + (3 , 4 , 5 ))
219- msg = "Unable to coerce to Series/DataFrame"
220- with tm .assert_raises_regex (ValueError , msg ):
221- f (frame , ndim_5 )
222-
223- with tm .assert_raises_regex (ValueError , msg ):
224- getattr (frame , op )(ndim_5 )
225-
226- # res_add = frame.add(frame)
227- # res_sub = frame.sub(frame)
228- # res_mul = frame.mul(frame)
229- # res_div = frame.div(2 * frame)
230-
231- # tm.assert_frame_equal(res_add, frame + frame)
232- # tm.assert_frame_equal(res_sub, frame - frame)
233- # tm.assert_frame_equal(res_mul, frame * frame)
234- # tm.assert_frame_equal(res_div, frame / (2 * frame))
235-
236- const_add = frame .add (1 )
237- tm .assert_frame_equal (const_add , frame + 1 )
196+ # ndim >= 3
197+ ndim_5 = np .ones (float_frame .shape + (3 , 4 , 5 ))
198+ msg = "Unable to coerce to Series/DataFrame"
199+ with tm .assert_raises_regex (ValueError , msg ):
200+ f (float_frame , ndim_5 )
201+
202+ with tm .assert_raises_regex (ValueError , msg ):
203+ getattr (float_frame , op )(ndim_5 )
204+
205+ const_add = float_frame .add (1 )
206+ tm .assert_frame_equal (const_add , float_frame + 1 )
238207
239208 # corner cases
240- result = frame .add (frame [:0 ])
241- tm .assert_frame_equal (result , frame * np .nan )
209+ result = float_frame .add (float_frame [:0 ])
210+ tm .assert_frame_equal (result , float_frame * np .nan )
242211
243- result = frame [:0 ].add (frame )
244- tm .assert_frame_equal (result , frame * np .nan )
212+ result = float_frame [:0 ].add (float_frame )
213+ tm .assert_frame_equal (result , float_frame * np .nan )
245214 with tm .assert_raises_regex (NotImplementedError , 'fill_value' ):
246- frame .add (frame .iloc [0 ], fill_value = 3 )
215+ float_frame .add (float_frame .iloc [0 ], fill_value = 3 )
247216 with tm .assert_raises_regex (NotImplementedError , 'fill_value' ):
248- frame .add (frame .iloc [0 ], axis = 'index' , fill_value = 3 )
249-
250- def test_arith_flex_series (self ):
251- arr = np .array ([[1. , 2. , 3. ],
252- [4. , 5. , 6. ],
253- [7. , 8. , 9. ]])
254- df = pd .DataFrame (arr , columns = ['one' , 'two' , 'three' ],
255- index = ['a' , 'b' , 'c' ])
217+ float_frame .add (float_frame .iloc [0 ], axis = 'index' , fill_value = 3 )
218+
219+ def test_arith_flex_series (self , simple_frame ):
220+ df = simple_frame
256221
257222 row = df .xs ('a' )
258223 col = df ['two' ]
0 commit comments