-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
STY: use pytest.raises context syntax (series/indexing/*) #24750
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
STY: use pytest.raises context syntax (series/indexing/*) #24750
Conversation
@@ -175,7 +183,8 @@ def test_where_unsafe_itemsize_fail(dtype): | |||
mask = s < 5 | |||
|
|||
values = [2.5, 3.5, 4.5, 5.5, 6.5] | |||
pytest.raises(Exception, s.__setitem__, tuple(mask), values) | |||
with pytest.raises(Exception): | |||
s[mask] = values |
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.
does not raise using python index operator instead of setitem with tuple
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.
not clear what you mean 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.
pandas/pandas/tests/series/indexing/test_boolean.py
Lines 163 to 187 in cc9fdef
@pytest.mark.parametrize("dtype", [np.int64, np.float64]) | |
def test_where_unsafe_upcast(dtype): | |
s = Series(np.arange(10), dtype=dtype) | |
values = [2.5, 3.5, 4.5, 5.5, 6.5] | |
mask = s < 5 | |
expected = Series(values + lrange(5, 10), dtype="float64") | |
s[mask] = values | |
assert_series_equal(s, expected) | |
@pytest.mark.xfail | |
@pytest.mark.parametrize("dtype", [ | |
np.int8, np.int16, np.int32, np.float32 | |
]) | |
def test_where_unsafe_itemsize_fail(dtype): | |
# Can't do these, as we are forced to change the | |
# item size of the input to something we cannot. | |
s = Series(np.arange(10), dtype=dtype) | |
mask = s < 5 | |
values = [2.5, 3.5, 4.5, 5.5, 6.5] | |
with pytest.raises(Exception): | |
s[mask] = values |
looking at test above test_where_unsafe_upcast
i'm assuming that it should be
s[mask] = values
whereas the current test, tests
s[tuple(mask)] = values
which gives KeyError: "None of [Index([True, True, True, True, True, False, False, False, False, False], dtype='object')] are in the [index]"
which does not appear to be the intended tested behaviour whereas the test is not raising an exception with the change.
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 match the expection more specifically with an error message, would make it more informative
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.
test originally added in #3292 so i'm guessing that the expected error message was originally:
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.
maybe fixed by #9743. this is where the error message was removed from core/common.py
Codecov Report
@@ Coverage Diff @@
## master #24750 +/- ##
===========================================
- Coverage 92.38% 43.07% -49.32%
===========================================
Files 166 166
Lines 52358 52358
===========================================
- Hits 48373 22551 -25822
- Misses 3985 29807 +25822
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #24750 +/- ##
==========================================
- Coverage 92.38% 92.38% -0.01%
==========================================
Files 166 166
Lines 52358 52363 +5
==========================================
+ Hits 48373 48377 +4
- Misses 3985 3986 +1
Continue to review full report at Codecov.
|
@@ -165,6 +172,7 @@ def test_where_unsafe_upcast(dtype): | |||
assert_series_equal(s, expected) | |||
|
|||
|
|||
@pytest.mark.xfail |
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.
why does this fail? can you add a reason
@@ -175,7 +183,8 @@ def test_where_unsafe_itemsize_fail(dtype): | |||
mask = s < 5 | |||
|
|||
values = [2.5, 3.5, 4.5, 5.5, 6.5] | |||
pytest.raises(Exception, s.__setitem__, tuple(mask), values) | |||
with pytest.raises(Exception): | |||
s[mask] = values |
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.
not clear what you mean here?
@@ -175,7 +183,8 @@ def test_where_unsafe_itemsize_fail(dtype): | |||
mask = s < 5 | |||
|
|||
values = [2.5, 3.5, 4.5, 5.5, 6.5] | |||
pytest.raises(Exception, s.__setitem__, tuple(mask), values) | |||
with pytest.raises(Exception): | |||
s[mask] = values |
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 match the expection more specifically with an error message, would make it more informative
s = Series(np.arange(10), dtype=dtype) | ||
mask = s < 5 | ||
|
||
values = [2.5, 3.5, 4.5, 5.5, 6.5] | ||
pytest.raises(Exception, s.__setitem__, tuple(mask), values) | ||
s[mask] = values |
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.
so this should still raise if you try to set with a tuple though
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.
a tuple will give a ValueError: Can only tuple-index with a MultiIndex
which is tested in series/test_indexing.py
,
the tuple
keyword will create a tuple from an iterator, in this case a boolean series.
>>> import numpy as np
>>> dtype = np.int8
>>> from pandas import Series
>>> s = Series(np.arange(10), dtype=dtype)
>>> mask = s < 5
>>> print(mask)
0 True
1 True
2 True
3 True
4 True
5 False
6 False
7 False
8 False
9 False
dtype: bool
>>> tuple(mask)
(True, True, True, True, True, False, False, False, False, False)
>>> values = [2.5, 3.5, 4.5, 5.5, 6.5]
>>> s[tuple(mask)] = values
raises
KeyError: "None of [Index([True, True, True, True, True, False, False, False, False, False], dtype='object')] are in the [index]"
should we test for this?
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 think the intention was to pass a tuple of values (not tupleify one), e.g.
s[tuple(mask, )] = values
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.
in order to keep this PR more atomic should i revert the changes to this test and raise an separate issue?
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 can if you want or just fix it here, should be straightforward
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.
should have said, ci failure is unrelated.. ##[error]doc/source/comparison_with_sas.rst(754,-22): error F821: undefined name 'get_ipython'
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.
yes #24756 will address that
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.
if the original intention was to raise a Exception: cannot change dtype of input to smaller size and that is no longer a restriction then the test is now fixed
what tests catches this?
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.
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 i c, so this was in essence an old test.
thanks @simonjayhawkins |
xref #24332
@pytest.mark.xfail
applied totest_where_unsafe_itemsize_fail
: does not raise using python index operator instead of__setitem__
withtuple