diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 25f847c698278..19e8acdaa7384 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -87,6 +87,7 @@ Other enhancements - Positional slicing on a :class:`IntervalIndex` now supports slices with ``step > 1`` (:issue:`31658`) - :class:`Series.str` now has a `fullmatch` method that matches a regular expression against the entire string in each row of the series, similar to `re.fullmatch` (:issue:`32806`). - :meth:`DataFrame.sample` will now also allow array-like and BitGenerator objects to be passed to ``random_state`` as seeds (:issue:`32503`) +- :meth:`MultiIndex.union` will now raise `RuntimeWarning` if the object inside are unsortable, pass `sort=False` to suppress this warning (:issue:`33015`) - .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 6c6f6a8600ba2..6e7e8ff51f201 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -4,6 +4,7 @@ from fractions import Fraction from numbers import Number import sys +import warnings import cython from cython import Py_ssize_t @@ -286,7 +287,11 @@ def fast_unique_multiple(list arrays, sort: bool = True): try: uniques.sort() except TypeError: - # TODO: RuntimeWarning? + warnings.warn( + "The values in the array are unorderable. " + "Pass `sort=False` to suppress this warning.", + RuntimeWarning, + ) pass return uniques diff --git a/pandas/tests/indexes/multi/test_setops.py b/pandas/tests/indexes/multi/test_setops.py index c97704e8a2066..d7427ee622977 100644 --- a/pandas/tests/indexes/multi/test_setops.py +++ b/pandas/tests/indexes/multi/test_setops.py @@ -337,7 +337,8 @@ def test_union_sort_other_incomparable(): idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]]) # default, sort=None - result = idx.union(idx[:1]) + with tm.assert_produces_warning(RuntimeWarning): + result = idx.union(idx[:1]) tm.assert_index_equal(result, idx) # sort=False diff --git a/pandas/tests/test_lib.py b/pandas/tests/test_lib.py index d914cf873de24..b6f59807eaa15 100644 --- a/pandas/tests/test_lib.py +++ b/pandas/tests/test_lib.py @@ -3,6 +3,7 @@ from pandas._libs import lib, writers as libwriters +import pandas as pd from pandas import Index import pandas._testing as tm @@ -39,6 +40,11 @@ def test_fast_unique_multiple_list_gen_sort(self): out = lib.fast_unique_multiple_list_gen(gen, sort=False) tm.assert_numpy_array_equal(np.array(out), expected) + def test_fast_unique_multiple_unsortable_runtimewarning(self): + arr = [np.array(["foo", pd.Timestamp("2000")])] + with tm.assert_produces_warning(RuntimeWarning): + lib.fast_unique_multiple(arr, sort=None) + class TestIndexing: def test_maybe_indices_to_slice_left_edge(self):