Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
natmokval committed Oct 9, 2023
1 parent 786c250 commit 533b4a2
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 46 deletions.
4 changes: 1 addition & 3 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,9 +1083,7 @@ def reduction(
skipna: bool = True,
mask: npt.NDArray[np.bool_] | None = None,
):
if values.size == 0 or not all(
isinstance(elem, type(values[0])) for elem in values[1:]
):
if values.size == 0:
return _na_for_min_count(values, axis)

values, mask = _get_values(
Expand Down
12 changes: 8 additions & 4 deletions pandas/tests/base/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +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 not all(
isinstance(elem, type(index_or_series_obj.values[0]))
for elem in index_or_series_obj.values[1:]
elif any(isinstance(elem, int) for elem in obj.values[:]) and any(
isinstance(elem, str) for elem in obj.values[:]
):
pytest.skip("'>' not supported between instances of 'str' and 'int'")
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)
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/base/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ def test_value_counts(index_or_series_obj):
# i.e IntegerDtype
expected = expected.astype("Int64")

if not all(
isinstance(elem, type(index_or_series_obj.values[0]))
for elem in index_or_series_obj.values[1:]
if (
len(obj) > 0
and isinstance(obj.values[0], int)
and isinstance(obj.values[1], str)
):
msg = "'<' not supported between "
with pytest.raises(TypeError, match=msg):
Expand All @@ -74,9 +75,8 @@ def test_value_counts_null(null_obj, index_or_series_obj):
pytest.skip("Test doesn't make sense on empty data")
elif isinstance(orig, MultiIndex):
pytest.skip(f"MultiIndex can't hold '{null_obj}'")
elif not all(
isinstance(elem, type(index_or_series_obj.values[0]))
for elem in index_or_series_obj.values[1:]
elif any(isinstance(elem, int) for elem in orig.values[:]) and any(
isinstance(elem, str) for elem in orig.values[:]
):
pytest.skip("'<' not supported between instances of 'str' and 'int'")

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ def test_union_duplicates(index, request):
values = index.unique().values.tolist()
mi1 = MultiIndex.from_arrays([values, [1] * len(values)])
mi2 = MultiIndex.from_arrays([[values[0]] + values, [1] * (len(values) + 1)])
if not all(isinstance(elem, type(values[0])) for elem in values[1:]):
if isinstance(index.values[0], int) and isinstance(index.values[1], str):
pytest.skip("'<' not supported between instances of 'str' and 'int'")
else:
result = mi2.union(mi1)
Expand Down
12 changes: 7 additions & 5 deletions pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,11 @@ def test_hasnans_isnans(self, index_flat):
@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning")
@pytest.mark.parametrize("na_position", [None, "middle"])
def test_sort_values_invalid_na_position(index_with_missing, na_position):
if not all(
isinstance(sub, type(index_with_missing.values[0]))
for sub in index_with_missing.values[1:]
if any(isinstance(elem, int) for elem in index_with_missing.values[:]) and any(
isinstance(elem, str) for elem in index_with_missing.values[:]
):
pytest.skip("'<' not supported between instances of 'str' and 'int'")
with pytest.raises(TypeError, match="'<' not supported between "):
index_with_missing.sort_values(na_position=na_position)
else:
with pytest.raises(ValueError, match=f"invalid na_position: {na_position}"):
index_with_missing.sort_values(na_position=na_position)
Expand All @@ -473,7 +473,9 @@ def test_sort_values_with_missing(index_with_missing, na_position, request):

missing_count = np.sum(index_with_missing.isna())
not_na_vals = index_with_missing[index_with_missing.notna()].values
if not all(isinstance(sub, type(not_na_vals[0])) for sub in not_na_vals[1:]):
if any(isinstance(elem, int) for elem in index_with_missing.values[:]) and any(
isinstance(elem, str) for elem in index_with_missing.values[:]
):
with pytest.raises(
TypeError, match="'<' not supported between instances of 'int' and 'str'"
):
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/test_numpy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ def test_numpy_ufuncs_reductions(index, func, request):
with pytest.raises(TypeError, match="is not ordered for"):
func.reduce(index)
return
elif isinstance(index.values[0], int) and isinstance(index.values[1], str):
with pytest.raises(
TypeError, match=".* not supported between instances of 'int' and 'str'"
):
func.reduce(index)
return
else:
result = func.reduce(index)

Expand Down
46 changes: 30 additions & 16 deletions pandas/tests/indexes/test_old_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,26 +331,40 @@ def test_argsort(self, index):
if isinstance(index, CategoricalIndex):
pytest.skip(f"{type(self).__name__} separately tested")

if not all(
isinstance(elem, type(index.values[0])) for elem in index.values[1:]
if (
len(index.values) > 0
and isinstance(index.values[0], int)
and isinstance(index.values[1], str)
):
pytest.skip("'<' not supported between instances of 'str' and 'int'")
result = index.argsort()
expected = np.array(index).argsort()
tm.assert_numpy_array_equal(result, expected, check_dtype=False)
with pytest.raises(
TypeError,
match="'<' not supported between instances of 'str' and 'int'",
):
index.argsort()
else:
result = index.argsort()
expected = np.array(index).argsort()
tm.assert_numpy_array_equal(result, expected, check_dtype=False)

def test_numpy_argsort(self, index):
if not all(
isinstance(elem, type(index.values[0])) for elem in index.values[1:]
if (
len(index.values) > 0
and isinstance(index.values[0], int)
and isinstance(index.values[1], str)
):
pytest.skip("'<' not supported between instances of 'str' and 'int'")
result = np.argsort(index)
expected = index.argsort()
tm.assert_numpy_array_equal(result, expected)

result = np.argsort(index, kind="mergesort")
expected = index.argsort(kind="mergesort")
tm.assert_numpy_array_equal(result, expected)
with pytest.raises(
TypeError,
match="'<' not supported between instances of 'str' and 'int'",
):
np.argsort(index)
else:
result = np.argsort(index)
expected = index.argsort()
tm.assert_numpy_array_equal(result, expected)

result = np.argsort(index, kind="mergesort")
expected = index.argsort(kind="mergesort")
tm.assert_numpy_array_equal(result, expected)

# these are the only two types that perform
# pandas compatibility input validation - the
Expand Down
27 changes: 19 additions & 8 deletions pandas/tests/indexes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
def test_union_same_types(index):
# Union with a non-unique, non-monotonic index raises error
# Only needed for bool index factory
if not all(isinstance(elem, type(index.values[0])) for elem in index.values[1:]):
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:
Expand Down Expand Up @@ -101,9 +105,14 @@ 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
if not all(
isinstance(elem, type(idx1.values[0])) for elem in idx1.values[1:]
) or not all(isinstance(elem, type(idx2.values[0])) for elem in idx2.values[1:]):
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()
Expand Down Expand Up @@ -379,8 +388,8 @@ 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)
if not all(
isinstance(elem, type(second.values[0])) for elem in second.values[1:]
if any(isinstance(elem, int) for elem in second.values[:]) and any(
isinstance(elem, str) for elem in second.values[:]
):
with pytest.raises(
TypeError,
Expand Down Expand Up @@ -455,8 +464,10 @@ 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)
if not all(
isinstance(elem, type(index.values[0])) for elem in index.values[1:]
if (
len(index.values) > 0
and isinstance(index.values[0], int)
and isinstance(index.values[1], str)
):
with pytest.raises(
TypeError,
Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ def test_factorize(self, index_or_series_obj, sort):
expected_uniques = expected_uniques.astype(object)

if sort:
if not all(
isinstance(elem, type(expected_uniques.values[0]))
for elem in expected_uniques.values[1:]
if (
len(expected_uniques.values) > 0
and isinstance(expected_uniques.values[0], int)
and isinstance(expected_uniques.values[1], str)
):
pytest.skip("'<' not supported between instances of 'str' and 'int'")
expected_uniques = expected_uniques.sort_values()
Expand Down

0 comments on commit 533b4a2

Please sign in to comment.