Skip to content

Commit 22b6123

Browse files
h-vetinarivictor
authored and
victor
committed
Fixturize tests/frame/test_arithmetic (pandas-dev#22736)
1 parent 7301dbe commit 22b6123

File tree

2 files changed

+84
-127
lines changed

2 files changed

+84
-127
lines changed

pandas/tests/frame/conftest.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ def mixed_float_frame():
7070
Columns are ['A', 'B', 'C', 'D'].
7171
"""
7272
df = DataFrame(tm.getSeriesData())
73-
df.A = df.A.astype('float16')
73+
df.A = df.A.astype('float32')
7474
df.B = df.B.astype('float32')
75-
df.C = df.C.astype('float64')
75+
df.C = df.C.astype('float16')
76+
df.D = df.D.astype('float64')
7677
return df
7778

7879

@@ -84,9 +85,10 @@ def mixed_float_frame2():
8485
Columns are ['A', 'B', 'C', 'D'].
8586
"""
8687
df = DataFrame(tm.getSeriesData())
87-
df.D = df.D.astype('float16')
88+
df.D = df.D.astype('float32')
8889
df.C = df.C.astype('float32')
89-
df.B = df.B.astype('float64')
90+
df.B = df.B.astype('float16')
91+
df.D = df.D.astype('float64')
9092
return df
9193

9294

@@ -99,10 +101,10 @@ def mixed_int_frame():
99101
"""
100102
df = DataFrame({k: v.astype(int)
101103
for k, v in compat.iteritems(tm.getSeriesData())})
102-
df.A = df.A.astype('uint8')
103-
df.B = df.B.astype('int32')
104-
df.C = df.C.astype('int64')
105-
df.D = np.ones(len(df.D), dtype='uint64')
104+
df.A = df.A.astype('int32')
105+
df.B = np.ones(len(df.B), dtype='uint64')
106+
df.C = df.C.astype('uint8')
107+
df.D = df.C.astype('int64')
106108
return df
107109

108110

pandas/tests/frame/test_arithmetic.py

+74-119
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import pytest
55
import numpy as np
66

7-
from pandas.compat import range, PY3
8-
import pandas.io.formats.printing as printing
7+
from pandas.compat import range
98

109
import pandas as pd
1110
import pandas.util.testing as tm
@@ -127,132 +126,88 @@ def test_df_add_flex_filled_mixed_dtypes(self):
127126
'B': ser * 2})
128127
tm.assert_frame_equal(result, expected)
129128

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)
219187
msg = "Unable to coerce to Series/DataFrame"
220188
with tm.assert_raises_regex(ValueError, msg):
221-
f(frame, ndim_5)
189+
getattr(float_frame, op)(arr)
222190

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):
235192

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)
238195

239196
# 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)
242202

243-
result = frame[:0].add(frame)
244-
tm.assert_frame_equal(result, frame * np.nan)
245203
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+
247206
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
256211

257212
row = df.xs('a')
258213
col = df['two']

0 commit comments

Comments
 (0)