Skip to content

Commit 0f2266e

Browse files
committed
better approach for mixed int, 1 more test passed
1 parent 0b2e755 commit 0f2266e

File tree

1 file changed

+18
-41
lines changed

1 file changed

+18
-41
lines changed

pandas/tests/base/test_misc.py

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -147,48 +147,25 @@ def test_searchsorted(request, index_or_series_obj):
147147
# See gh-12238
148148
obj = index_or_series_obj
149149

150-
# Handle mixed int string
151-
if isinstance(obj, Index) and obj.inferred_type in ["mixed", "mixed-integer"]:
152-
request.applymarker(
153-
pytest.mark.xfail(
154-
reason="Cannot compare mixed types (str and int)", strict=False
155-
)
156-
)
157-
obj = obj.unique()
158-
159-
# Mixed int string specific case
160-
if obj.equals(Index([0, "a", 1, "b", 2, "c"])):
161-
obj = obj.astype(str)
162-
150+
# Check for MultiIndex
163151
if isinstance(obj, pd.MultiIndex):
164-
# See gh-14833
165-
request.applymarker(
166-
pytest.mark.xfail(
167-
reason="np.searchsorted doesn't work on pd.MultiIndex: GH 14833",
168-
strict=False,
169-
)
170-
)
171-
return
172-
173-
if obj.dtype.kind == "c" and isinstance(obj, Index):
174-
# Complex numbers are not comparable
175-
request.applymarker(
176-
pytest.mark.xfail(reason="Complex objects are not comparable", strict=False)
177-
)
178-
return
179-
180-
if isinstance(obj, Index) and obj.inferred_type == "tuples":
181-
# Tuples may not be supported by np.searchsorted
182-
pytest.mark.xfail(reason="Cannot handle tuples in searchsorted", strict=False)
183-
184-
# Only proceed if obj is not mixed or unsupported
185-
try:
186-
max_obj = max(obj, default=0)
187-
except TypeError:
188-
pytest.mark.xfail(
189-
reason="Cannot compute max for unsupported types", strict=False
190-
)
191-
152+
pytest.xfail("np.searchsorted doesn't work on pd.MultiIndex: GH 14833")
153+
154+
# Check for Index and subtypes
155+
if isinstance(obj, Index):
156+
# Mixed types
157+
if obj.inferred_type in ["mixed", "mixed-integer"]:
158+
try:
159+
obj = obj.astype(str)
160+
except (TypeError, ValueError):
161+
pytest.xfail("Cannot compare mixed types (str and int)")
162+
163+
# Complex types
164+
elif obj.dtype.kind == "c":
165+
pytest.xfail("Complex objects are not comparable")
166+
167+
# Run tests
168+
max_obj = max(obj, default=0)
192169
index = np.searchsorted(obj, max_obj)
193170
assert 0 <= index <= len(obj)
194171

0 commit comments

Comments
 (0)