Skip to content

Commit 92478d5

Browse files
authored
Added RuntimeWarning to lib.fast_unique_multiple (#33015)
1 parent 11dff59 commit 92478d5

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Other enhancements
8787
- Positional slicing on a :class:`IntervalIndex` now supports slices with ``step > 1`` (:issue:`31658`)
8888
- :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`).
8989
- :meth:`DataFrame.sample` will now also allow array-like and BitGenerator objects to be passed to ``random_state`` as seeds (:issue:`32503`)
90+
- :meth:`MultiIndex.union` will now raise `RuntimeWarning` if the object inside are unsortable, pass `sort=False` to suppress this warning (:issue:`33015`)
9091
-
9192

9293
.. ---------------------------------------------------------------------------

pandas/_libs/lib.pyx

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ from fractions import Fraction
44
from numbers import Number
55

66
import sys
7+
import warnings
78

89
import cython
910
from cython import Py_ssize_t
@@ -286,7 +287,11 @@ def fast_unique_multiple(list arrays, sort: bool = True):
286287
try:
287288
uniques.sort()
288289
except TypeError:
289-
# TODO: RuntimeWarning?
290+
warnings.warn(
291+
"The values in the array are unorderable. "
292+
"Pass `sort=False` to suppress this warning.",
293+
RuntimeWarning,
294+
)
290295
pass
291296

292297
return uniques

pandas/tests/indexes/multi/test_setops.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ def test_union_sort_other_incomparable():
337337
idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]])
338338

339339
# default, sort=None
340-
result = idx.union(idx[:1])
340+
with tm.assert_produces_warning(RuntimeWarning):
341+
result = idx.union(idx[:1])
341342
tm.assert_index_equal(result, idx)
342343

343344
# sort=False

pandas/tests/test_lib.py

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from pandas._libs import lib, writers as libwriters
55

6+
import pandas as pd
67
from pandas import Index
78
import pandas._testing as tm
89

@@ -39,6 +40,11 @@ def test_fast_unique_multiple_list_gen_sort(self):
3940
out = lib.fast_unique_multiple_list_gen(gen, sort=False)
4041
tm.assert_numpy_array_equal(np.array(out), expected)
4142

43+
def test_fast_unique_multiple_unsortable_runtimewarning(self):
44+
arr = [np.array(["foo", pd.Timestamp("2000")])]
45+
with tm.assert_produces_warning(RuntimeWarning):
46+
lib.fast_unique_multiple(arr, sort=None)
47+
4248

4349
class TestIndexing:
4450
def test_maybe_indices_to_slice_left_edge(self):

0 commit comments

Comments
 (0)