From b613e5aee3615262d51d9d86e8393575a1cc527d Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sun, 15 Oct 2023 18:59:26 +0200 Subject: [PATCH] TST: Fix assert_is_sorted for eas --- pandas/_testing/asserters.py | 5 ++++- pandas/tests/util/test_util.py | 14 +++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index d4e7e196dc2d4..8323c6a443fac 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -440,7 +440,10 @@ def assert_is_sorted(seq) -> None: if isinstance(seq, (Index, Series)): seq = seq.values # sorting does not change precisions - assert_numpy_array_equal(seq, np.sort(np.array(seq))) + if isinstance(seq, np.ndarray): + assert_numpy_array_equal(seq, np.sort(np.array(seq))) + else: + assert_extension_array_equal(seq, seq[seq.argsort()]) def assert_categorical_equal( diff --git a/pandas/tests/util/test_util.py b/pandas/tests/util/test_util.py index 5718480fdec5e..dfb8587d3924e 100644 --- a/pandas/tests/util/test_util.py +++ b/pandas/tests/util/test_util.py @@ -2,7 +2,10 @@ import pytest -from pandas import compat +from pandas import ( + array, + compat, +) import pandas._testing as tm @@ -44,3 +47,12 @@ def test_datapath(datapath): def test_external_error_raised(): with tm.external_error_raised(TypeError): raise TypeError("Should not check this error message, so it will pass") + + +def test_is_sorted(): + arr = array([1, 2, 3], dtype="Int64") + tm.assert_is_sorted(arr) + + arr = array([4, 2, 3], dtype="Int64") + with pytest.raises(AssertionError, match="ExtensionArray are different"): + tm.assert_is_sorted(arr)