|
4 | 4 | import pytest
|
5 | 5 | import numpy as np
|
6 | 6 |
|
7 |
| -from pandas.compat import range, PY3 |
8 |
| -import pandas.io.formats.printing as printing |
| 7 | +from pandas.compat import range |
9 | 8 |
|
10 | 9 | import pandas as pd
|
11 | 10 | import pandas.util.testing as tm
|
@@ -127,132 +126,88 @@ def test_df_add_flex_filled_mixed_dtypes(self):
|
127 | 126 | 'B': ser * 2})
|
128 | 127 | tm.assert_frame_equal(result, expected)
|
129 | 128 |
|
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'] |
151 |
| - if not PY3: |
152 |
| - aliases = {} |
153 |
| - else: |
154 |
| - 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) |
162 |
| - tm.assert_frame_equal(result, exp) |
163 |
| - |
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)) |
| 129 | + def test_arith_flex_frame(self, all_arithmetic_operators, float_frame, |
| 130 | + mixed_float_frame): |
| 131 | + # one instance of parametrized fixture |
| 132 | + op = all_arithmetic_operators |
| 133 | + |
| 134 | + def f(x, y): |
| 135 | + # r-versions not in operator-stdlib; get op without "r" and invert |
| 136 | + if op.startswith('__r'): |
| 137 | + return getattr(operator, op.replace('__r', '__'))(y, x) |
| 138 | + return getattr(operator, op)(x, y) |
| 139 | + |
| 140 | + result = getattr(float_frame, op)(2 * float_frame) |
| 141 | + exp = f(float_frame, 2 * float_frame) |
| 142 | + tm.assert_frame_equal(result, exp) |
| 143 | + |
| 144 | + # vs mix float |
| 145 | + result = getattr(mixed_float_frame, op)(2 * mixed_float_frame) |
| 146 | + exp = f(mixed_float_frame, 2 * mixed_float_frame) |
| 147 | + tm.assert_frame_equal(result, exp) |
| 148 | + _check_mixed_float(result, dtype=dict(C=None)) |
| 149 | + |
| 150 | + @pytest.mark.parametrize('op', ['__add__', '__sub__', '__mul__']) |
| 151 | + def test_arith_flex_frame_mixed(self, op, int_frame, mixed_int_frame, |
| 152 | + mixed_float_frame): |
| 153 | + f = getattr(operator, op) |
| 154 | + |
| 155 | + # vs mix int |
| 156 | + result = getattr(mixed_int_frame, op)(2 + mixed_int_frame) |
| 157 | + exp = f(mixed_int_frame, 2 + mixed_int_frame) |
| 158 | + |
| 159 | + # no overflow in the uint |
| 160 | + dtype = None |
| 161 | + if op in ['__sub__']: |
| 162 | + dtype = dict(B='uint64', C=None) |
| 163 | + elif op in ['__add__', '__mul__']: |
| 164 | + dtype = dict(C=None) |
| 165 | + tm.assert_frame_equal(result, exp) |
| 166 | + _check_mixed_int(result, dtype=dtype) |
| 167 | + |
| 168 | + # vs mix float |
| 169 | + result = getattr(mixed_float_frame, op)(2 * mixed_float_frame) |
| 170 | + exp = f(mixed_float_frame, 2 * mixed_float_frame) |
| 171 | + tm.assert_frame_equal(result, exp) |
| 172 | + _check_mixed_float(result, dtype=dict(C=None)) |
| 173 | + |
| 174 | + # vs plain int |
| 175 | + result = getattr(int_frame, op)(2 * int_frame) |
| 176 | + exp = f(int_frame, 2 * int_frame) |
| 177 | + tm.assert_frame_equal(result, exp) |
| 178 | + |
| 179 | + def test_arith_flex_frame_raise(self, all_arithmetic_operators, |
| 180 | + float_frame): |
| 181 | + # one instance of parametrized fixture |
| 182 | + op = all_arithmetic_operators |
| 183 | + |
| 184 | + # Check that arrays with dim >= 3 raise |
| 185 | + for dim in range(3, 6): |
| 186 | + arr = np.ones((1,) * dim) |
219 | 187 | msg = "Unable to coerce to Series/DataFrame"
|
220 | 188 | with tm.assert_raises_regex(ValueError, msg):
|
221 |
| - f(frame, ndim_5) |
| 189 | + getattr(float_frame, op)(arr) |
222 | 190 |
|
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)) |
| 191 | + def test_arith_flex_frame_corner(self, float_frame): |
235 | 192 |
|
236 |
| - const_add = frame.add(1) |
237 |
| - tm.assert_frame_equal(const_add, frame + 1) |
| 193 | + const_add = float_frame.add(1) |
| 194 | + tm.assert_frame_equal(const_add, float_frame + 1) |
238 | 195 |
|
239 | 196 | # corner cases
|
240 |
| - result = frame.add(frame[:0]) |
241 |
| - tm.assert_frame_equal(result, frame * np.nan) |
| 197 | + result = float_frame.add(float_frame[:0]) |
| 198 | + tm.assert_frame_equal(result, float_frame * np.nan) |
| 199 | + |
| 200 | + result = float_frame[:0].add(float_frame) |
| 201 | + tm.assert_frame_equal(result, float_frame * np.nan) |
242 | 202 |
|
243 |
| - result = frame[:0].add(frame) |
244 |
| - tm.assert_frame_equal(result, frame * np.nan) |
245 | 203 | with tm.assert_raises_regex(NotImplementedError, 'fill_value'):
|
246 |
| - frame.add(frame.iloc[0], fill_value=3) |
| 204 | + float_frame.add(float_frame.iloc[0], fill_value=3) |
| 205 | + |
247 | 206 | 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']) |
| 207 | + float_frame.add(float_frame.iloc[0], axis='index', fill_value=3) |
| 208 | + |
| 209 | + def test_arith_flex_series(self, simple_frame): |
| 210 | + df = simple_frame |
256 | 211 |
|
257 | 212 | row = df.xs('a')
|
258 | 213 | col = df['two']
|
|
0 commit comments