-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixturize tests/frame/test_arithmetic #22736
Changes from 6 commits
9ce1860
0d8aa09
1f8a5d6
1acc6b1
054f4e9
3ee3b70
b6e14b0
b0b82a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,7 @@ | |
import pytest | ||
import numpy as np | ||
|
||
from pandas.compat import range, PY3 | ||
import pandas.io.formats.printing as printing | ||
from pandas.compat import range | ||
|
||
import pandas as pd | ||
import pandas.util.testing as tm | ||
|
@@ -127,132 +126,92 @@ def test_df_add_flex_filled_mixed_dtypes(self): | |
'B': ser * 2}) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_arith_flex_frame(self): | ||
seriesd = tm.getSeriesData() | ||
frame = pd.DataFrame(seriesd).copy() | ||
|
||
mixed_float = pd.DataFrame({'A': frame['A'].copy().astype('float32'), | ||
'B': frame['B'].copy().astype('float32'), | ||
'C': frame['C'].copy().astype('float16'), | ||
'D': frame['D'].copy().astype('float64')}) | ||
|
||
intframe = pd.DataFrame({k: v.astype(int) | ||
for k, v in seriesd.items()}) | ||
mixed_int = pd.DataFrame({'A': intframe['A'].copy().astype('int32'), | ||
'B': np.ones(len(intframe), dtype='uint64'), | ||
'C': intframe['C'].copy().astype('uint8'), | ||
'D': intframe['D'].copy().astype('int64')}) | ||
|
||
# force these all to int64 to avoid platform testing issues | ||
intframe = pd.DataFrame({c: s for c, s in intframe.items()}, | ||
dtype=np.int64) | ||
|
||
ops = ['add', 'sub', 'mul', 'div', 'truediv', 'pow', 'floordiv', 'mod'] | ||
if not PY3: | ||
aliases = {} | ||
else: | ||
aliases = {'div': 'truediv'} | ||
def test_arith_flex_frame(self, all_arithmetic_operators, float_frame, | ||
mixed_float_frame): | ||
|
||
for op in ops: | ||
try: | ||
alias = aliases.get(op, op) | ||
f = getattr(operator, alias) | ||
result = getattr(frame, op)(2 * frame) | ||
exp = f(frame, 2 * frame) | ||
tm.assert_frame_equal(result, exp) | ||
|
||
# vs mix float | ||
result = getattr(mixed_float, op)(2 * mixed_float) | ||
exp = f(mixed_float, 2 * mixed_float) | ||
tm.assert_frame_equal(result, exp) | ||
_check_mixed_float(result, dtype=dict(C=None)) | ||
|
||
# vs mix int | ||
if op in ['add', 'sub', 'mul']: | ||
result = getattr(mixed_int, op)(2 + mixed_int) | ||
exp = f(mixed_int, 2 + mixed_int) | ||
|
||
# no overflow in the uint | ||
dtype = None | ||
if op in ['sub']: | ||
dtype = dict(B='uint64', C=None) | ||
elif op in ['add', 'mul']: | ||
dtype = dict(C=None) | ||
tm.assert_frame_equal(result, exp) | ||
_check_mixed_int(result, dtype=dtype) | ||
|
||
# rops | ||
r_f = lambda x, y: f(y, x) | ||
result = getattr(frame, 'r' + op)(2 * frame) | ||
exp = r_f(frame, 2 * frame) | ||
tm.assert_frame_equal(result, exp) | ||
|
||
# vs mix float | ||
result = getattr(mixed_float, op)(2 * mixed_float) | ||
exp = f(mixed_float, 2 * mixed_float) | ||
tm.assert_frame_equal(result, exp) | ||
_check_mixed_float(result, dtype=dict(C=None)) | ||
|
||
result = getattr(intframe, op)(2 * intframe) | ||
exp = f(intframe, 2 * intframe) | ||
tm.assert_frame_equal(result, exp) | ||
|
||
# vs mix int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some reason, this part was tested twice. only once in cleaned-up version |
||
if op in ['add', 'sub', 'mul']: | ||
result = getattr(mixed_int, op)(2 + mixed_int) | ||
exp = f(mixed_int, 2 + mixed_int) | ||
|
||
# no overflow in the uint | ||
dtype = None | ||
if op in ['sub']: | ||
dtype = dict(B='uint64', C=None) | ||
elif op in ['add', 'mul']: | ||
dtype = dict(C=None) | ||
tm.assert_frame_equal(result, exp) | ||
_check_mixed_int(result, dtype=dtype) | ||
except: | ||
printing.pprint_thing("Failing operation %r" % op) | ||
raise | ||
|
||
# ndim >= 3 | ||
ndim_5 = np.ones(frame.shape + (3, 4, 5)) | ||
op = all_arithmetic_operators # one instance of parametrized fixture | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put comments on the prior line |
||
if op.startswith('__r'): | ||
# get op without "r" and invert it | ||
tmp = getattr(operator, op[:2] + op[3:]) | ||
f = lambda x, y: tmp(y, x) | ||
else: | ||
f = getattr(operator, op) | ||
|
||
result = getattr(float_frame, op)(2 * float_frame) | ||
exp = f(float_frame, 2 * float_frame) | ||
tm.assert_frame_equal(result, exp) | ||
|
||
# vs mix float | ||
result = getattr(mixed_float_frame, op)(2 * mixed_float_frame) | ||
exp = f(mixed_float_frame, 2 * mixed_float_frame) | ||
tm.assert_frame_equal(result, exp) | ||
_check_mixed_float(result, dtype=dict(C=None)) | ||
|
||
@pytest.mark.parametrize('op', ['__add__', '__sub__', '__mul__']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Broke up the test like you wanted |
||
def test_arith_flex_frame_mixed(self, op, int_frame, mixed_int_frame, | ||
float_frame, mixed_float_frame): | ||
|
||
if op.startswith('__r'): | ||
# get op without "r" and invert it | ||
tmp = getattr(operator, op[:2] + op[3:]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make this a named function to avoid the tmp something like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment applies to the prior test actually. I don't think you need this here at all, as op is just add, mul, sub. did you mean to test the reverse of these as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Best I can come up with is the following. Is this what you meant?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That was indeed not necessary anymore. Removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok this is fine (your change). note that I suspect we do this in other places, maybe see if you can make a more generalized version of this that we can then include in the pandas.util.testing (but followon-PR) |
||
f = lambda x, y: tmp(y, x) | ||
else: | ||
f = getattr(operator, op) | ||
|
||
# vs mix int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be more clear to move these to a separate test (from here down) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't want to get into orthogonal changes here. I haven't changed anything about this except removing the indentation. This goes for the test split requested below as well. |
||
result = getattr(mixed_int_frame, op)(2 + mixed_int_frame) | ||
exp = f(mixed_int_frame, 2 + mixed_int_frame) | ||
|
||
# no overflow in the uint | ||
dtype = None | ||
if op in ['__sub__']: | ||
dtype = dict(B='uint64', C=None) | ||
elif op in ['__add__', '__mul__']: | ||
dtype = dict(C=None) | ||
tm.assert_frame_equal(result, exp) | ||
_check_mixed_int(result, dtype=dtype) | ||
|
||
# vs mix float | ||
result = getattr(mixed_float_frame, op)(2 * mixed_float_frame) | ||
exp = f(mixed_float_frame, 2 * mixed_float_frame) | ||
tm.assert_frame_equal(result, exp) | ||
_check_mixed_float(result, dtype=dict(C=None)) | ||
|
||
# vs plain int | ||
result = getattr(int_frame, op)(2 * int_frame) | ||
exp = f(int_frame, 2 * int_frame) | ||
tm.assert_frame_equal(result, exp) | ||
|
||
def test_arith_flex_frame_corner(self, all_arithmetic_operators, | ||
float_frame): | ||
|
||
op = all_arithmetic_operators | ||
|
||
# Check that arrays with dim >= 3 raise | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Added (and expanded) |
||
for dim in range(3, 6): | ||
arr = np.ones((1,) * dim) | ||
msg = "Unable to coerce to Series/DataFrame" | ||
with tm.assert_raises_regex(ValueError, msg): | ||
f(frame, ndim_5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part (testing via the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you separate out these dimension-specific tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Split them out in a separate test |
||
|
||
with tm.assert_raises_regex(ValueError, msg): | ||
getattr(frame, op)(ndim_5) | ||
getattr(float_frame, op)(arr) | ||
|
||
# res_add = frame.add(frame) | ||
# res_sub = frame.sub(frame) | ||
# res_mul = frame.mul(frame) | ||
# res_div = frame.div(2 * frame) | ||
|
||
# tm.assert_frame_equal(res_add, frame + frame) | ||
# tm.assert_frame_equal(res_sub, frame - frame) | ||
# tm.assert_frame_equal(res_mul, frame * frame) | ||
# tm.assert_frame_equal(res_div, frame / (2 * frame)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did this commented-out stuff go somewhere else? Or just determined to be redundant? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Determined to be redundant with |
||
|
||
const_add = frame.add(1) | ||
tm.assert_frame_equal(const_add, frame + 1) | ||
const_add = float_frame.add(1) | ||
tm.assert_frame_equal(const_add, float_frame + 1) | ||
|
||
# corner cases | ||
result = frame.add(frame[:0]) | ||
tm.assert_frame_equal(result, frame * np.nan) | ||
result = float_frame.add(float_frame[:0]) | ||
tm.assert_frame_equal(result, float_frame * np.nan) | ||
|
||
result = float_frame[:0].add(float_frame) | ||
tm.assert_frame_equal(result, float_frame * np.nan) | ||
|
||
result = frame[:0].add(frame) | ||
tm.assert_frame_equal(result, frame * np.nan) | ||
with tm.assert_raises_regex(NotImplementedError, 'fill_value'): | ||
frame.add(frame.iloc[0], fill_value=3) | ||
float_frame.add(float_frame.iloc[0], fill_value=3) | ||
|
||
with tm.assert_raises_regex(NotImplementedError, 'fill_value'): | ||
frame.add(frame.iloc[0], axis='index', fill_value=3) | ||
|
||
def test_arith_flex_series(self): | ||
arr = np.array([[1., 2., 3.], | ||
[4., 5., 6.], | ||
[7., 8., 9.]]) | ||
df = pd.DataFrame(arr, columns=['one', 'two', 'three'], | ||
index=['a', 'b', 'c']) | ||
float_frame.add(float_frame.iloc[0], axis='index', fill_value=3) | ||
|
||
def test_arith_flex_series(self, simple_frame): | ||
df = simple_frame | ||
|
||
row = df.xs('a') | ||
col = df['two'] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rops were only tested within that if-block. Now they're all tested