-
-
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
TST: fix tests for mixed int string index #55458
Changes from 5 commits
9b8c639
3645428
6b0e2c2
786c250
533b4a2
4ad1bc4
87160c0
a27bc13
2d67a27
531beda
f09b0ac
adf4929
fac6291
5eef4fc
8a487e3
5fb2f55
3081ad5
e7edbbe
14d6b6f
454f0b6
82ad988
72435c6
7ebd7ef
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 |
---|---|---|
|
@@ -151,6 +151,15 @@ def test_searchsorted(request, index_or_series_obj): | |
# comparison semantics https://github.com/numpy/numpy/issues/15981 | ||
mark = pytest.mark.xfail(reason="complex objects are not comparable") | ||
request.node.add_marker(mark) | ||
elif any(isinstance(elem, int) for elem in obj.values[:]) and any( | ||
isinstance(elem, str) for elem in obj.values[:] | ||
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 is a pretty awkward check. maybe just check obj[0] and obj[1] with a comment the check is written with the mixed-int-string entry in mind? 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. agreed, I replaced the check with |
||
): | ||
with pytest.raises( | ||
TypeError, match="'>' not supported between instances of 'str' and 'int'" | ||
): | ||
max_obj = max(obj, default=0) | ||
index = np.searchsorted(obj, max_obj) | ||
return | ||
|
||
max_obj = max(obj, default=0) | ||
index = np.searchsorted(obj, max_obj) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,9 +33,17 @@ | |
def test_union_same_types(index): | ||
# Union with a non-unique, non-monotonic index raises error | ||
# Only needed for bool index factory | ||
idx1 = index.sort_values() | ||
idx2 = index.sort_values() | ||
assert idx1.union(idx2).dtype == idx1.dtype | ||
if ( | ||
len(index.values) > 0 | ||
and isinstance(index.values[0], int) | ||
and isinstance(index.values[1], str) | ||
): | ||
with pytest.raises(TypeError, match="'<' not supported between "): | ||
index.sort_values() | ||
else: | ||
idx1 = index.sort_values() | ||
idx2 = index.sort_values() | ||
assert idx1.union(idx2).dtype == idx1.dtype | ||
|
||
|
||
def test_union_different_types(index_flat, index_flat2, request): | ||
|
@@ -97,19 +105,30 @@ def test_union_different_types(index_flat, index_flat2, request): | |
|
||
# Union with a non-unique, non-monotonic index raises error | ||
# This applies to the boolean index | ||
idx1 = idx1.sort_values() | ||
idx2 = idx2.sort_values() | ||
|
||
with tm.assert_produces_warning(warn, match=msg): | ||
res1 = idx1.union(idx2) | ||
res2 = idx2.union(idx1) | ||
|
||
if any_uint64 and (idx1_signed or idx2_signed): | ||
assert res1.dtype == np.dtype("O") | ||
assert res2.dtype == np.dtype("O") | ||
if ( | ||
len(idx1.values) > 0 | ||
and isinstance(idx1.values[0], int) | ||
and isinstance(idx1.values[1], str) | ||
or len(idx2.values) > 0 | ||
and isinstance(idx2.values[0], int) | ||
and isinstance(idx2.values[1], str) | ||
): | ||
with pytest.raises(TypeError, match="'<' not supported between "): | ||
idx1.sort_values() | ||
idx2.sort_values() | ||
else: | ||
assert res1.dtype == common_dtype | ||
assert res2.dtype == common_dtype | ||
idx1 = idx1.sort_values() | ||
idx2 = idx2.sort_values() | ||
with tm.assert_produces_warning(warn, match=msg): | ||
res1 = idx1.union(idx2) | ||
res2 = idx2.union(idx1) | ||
|
||
if any_uint64 and (idx1_signed or idx2_signed): | ||
assert res1.dtype == np.dtype("O") | ||
assert res2.dtype == np.dtype("O") | ||
else: | ||
assert res1.dtype == common_dtype | ||
assert res2.dtype == common_dtype | ||
|
||
|
||
@pytest.mark.parametrize( | ||
|
@@ -369,9 +388,18 @@ def test_union_unequal(self, index_flat, fname, sname, expected_name): | |
# test copy.union(subset) - need sort for unicode and string | ||
first = index.copy().set_names(fname) | ||
second = index[1:].set_names(sname) | ||
union = first.union(second).sort_values() | ||
expected = index.set_names(expected_name).sort_values() | ||
tm.assert_index_equal(union, expected) | ||
if any(isinstance(elem, int) for elem in second.values[:]) and any( | ||
isinstance(elem, str) for elem in second.values[:] | ||
): | ||
with pytest.raises( | ||
TypeError, | ||
match="'<' not supported between ", | ||
): | ||
first.union(second).sort_values() | ||
else: | ||
union = first.union(second).sort_values() | ||
expected = index.set_names(expected_name).sort_values() | ||
tm.assert_index_equal(union, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"fname, sname, expected_name", | ||
|
@@ -436,9 +464,20 @@ def test_intersect_unequal(self, index_flat, fname, sname, expected_name): | |
# test copy.intersection(subset) - need sort for unicode and string | ||
first = index.copy().set_names(fname) | ||
second = index[1:].set_names(sname) | ||
intersect = first.intersection(second).sort_values() | ||
expected = index[1:].set_names(expected_name).sort_values() | ||
tm.assert_index_equal(intersect, expected) | ||
if ( | ||
len(index.values) > 0 | ||
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. maybe a tm.check_for_this_case to de-duplicate all these? or just a new fixture that specifically excludes the relevant case? 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. thank you, I added |
||
and isinstance(index.values[0], int) | ||
and isinstance(index.values[1], str) | ||
): | ||
with pytest.raises( | ||
TypeError, | ||
match="'<' not supported between ", | ||
): | ||
first.intersection(second).sort_values() | ||
else: | ||
intersect = first.intersection(second).sort_values() | ||
expected = index[1:].set_names(expected_name).sort_values() | ||
tm.assert_index_equal(intersect, expected) | ||
|
||
@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") | ||
def test_intersection_name_retention_with_nameless(self, index): | ||
|
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 looks like the message just cuts off? what is the message in err?
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.
Thank you for the comments.
Yes, I cut the message off because in tests I had both: "'<' not supported between instances of 'int' and 'str'" and "'<' not supported between instances of 'str' and 'int'" and sometimes messages did not match.
I corrected error message in the function
nargsort
and tests, now all error messages match.