-
-
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
Fixturize tests/frame/test_arithmetic #22736
Conversation
Hello @h-vetinari! Thanks for updating the PR.
Comment last updated on September 20, 2018 at 10:06 Hours UTC |
8d5c307
to
9ce1860
Compare
def test_arith_flex_frame(self): | ||
seriesd = tm.getSeriesData() | ||
frame = pd.DataFrame(seriesd).copy() | ||
@pytest.mark.parametrize('op', ['add', 'sub', 'mul', 'div', 'truediv', |
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.
can you change these to use the fixture: all_arithmetic_operators
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.
will do
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: |
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.
you prob don't need this (when using the fixture)
tm.assert_frame_equal(result, exp) | ||
_check_mixed_float(result, dtype=dict(C=None)) | ||
|
||
# vs mix int |
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.
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 comment
The 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.
|
||
const_add = frame.add(1) | ||
tm.assert_frame_equal(const_add, frame + 1) | ||
# ndim >= 3 |
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.
here and down could be in the top-section, e.g. the first test as they test all ops (or another tests is even better, with the fixture this becomes easy)
const_add = frame.add(1) | ||
tm.assert_frame_equal(const_add, frame + 1) | ||
# ndim >= 3 | ||
ndim_5 = np.ones(float_frame.shape + (3, 4, 5)) |
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.
can you add a comment here on what this is testing
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.
See above - I did not write the test; just removed indentation
@h-vetinari ping when updated |
I though your request for
I've skipped those tests now. |
Codecov Report
@@ Coverage Diff @@
## master #22736 +/- ##
==========================================
+ Coverage 92.17% 92.18% +0.01%
==========================================
Files 169 169
Lines 50778 50819 +41
==========================================
+ Hits 46807 46850 +43
+ Misses 3971 3969 -2
Continue to review full report at Codecov.
|
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.
@ jreback
might be more clear to move these to a separate test (from here down)
can you add a comment here on what this is testing
OK, I relented, and split up and cleaned up that huge test. Also the skips
for the r
-versions of the ops aren't necessary anymore. They were tested for add/sub/mult
before, and I extended that to all ops now.
tm.assert_frame_equal(result, exp) | ||
_check_mixed_int(result, dtype=dtype) | ||
|
||
# rops |
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
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 comment
The 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
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Broke up the test like you wanted
|
||
op = all_arithmetic_operators | ||
|
||
# Check that arrays with dim >= 3 raise |
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.
can you add a comment here on what this is testing
Added (and expanded)
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 comment
The reason will be displayed to describe this comment to others. Learn more.
This part (testing via the f
of the op
) is removed because it's testing the same thing as testing the op
from the frame directly below
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.
Can you separate out these dimension-specific tests?
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.
Split them out in a separate test
08fae94
to
3ee3b70
Compare
@jreback Green |
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.
ok test looks a lot nicer. small change, ping on green.
|
||
# 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 comment
The 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:]) |
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.
make this a named function to avoid the tmp something like
def f(x, y):
# comment
op = op.replace('__r', '__')
return getattr(operator, op)
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
make this a named function to avoid the tmp something like [...]
Best I can come up with is the following. Is this what you meant?
[...]
# one instance of parametrized fixture
op = all_arithmetic_operators
def f(x, y):
if op.startswith('__r'):
# get op without "r" and invert it
return getattr(operator, op.replace('__r', '__'))(y, x)
return getattr(operator, op)(x, y)
[...]
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.
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?
That was indeed not necessary anymore. Removed
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.
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)
|
||
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 comment
The 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?
def f(x, y): | ||
if op.startswith('__r'): | ||
# get op without "r" and invert it | ||
return getattr(operator, op.replace('__r', '__'))(y, x) |
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.
@jbrockmendel pls have a look here
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.
This is fine. The alternative would be to do a lookup in core.ops, but I think for a reader who is unfamiliar with core.ops this is clearer.
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.
Modified comment slightly
ok by me. @jbrockmendel if you'd have a look and merge if ok. |
# 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Determined to be redundant with test_arith_flex_frame
, which checks all arith_ops, and much more thoroughly.
thanks @h-vetinari so this is a good example of a change. you moved / parameterized things, but its a single change that is quite straightforward for a reviewer to grok / comment on w/o diving into things too much. |
Split off from #22730 as per review from @WillAyd
The changes in
conftest.py
are due to the following: