From a8cbc3751f255da95b2f0ff2a8b14024cbd9ca4c Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 25 Mar 2020 16:26:15 +0200 Subject: [PATCH 01/15] Added RuntimeWarning to lib.fast_unique_multiple --- pandas/_libs/lib.pyx | 5 ++++- pandas/tests/test_lib.py | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 6aa9a8b2dedfd..927fefbc6b697 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,9 @@ def fast_unique_multiple(list arrays, sort: bool = True): try: uniques.sort() except TypeError: - # TODO: RuntimeWarning? + warnings.warn( + "Not sure what will be a helpful error message", RuntimeWarning + ) pass return uniques diff --git a/pandas/tests/test_lib.py b/pandas/tests/test_lib.py index d914cf873de24..b8f97275534f6 100644 --- a/pandas/tests/test_lib.py +++ b/pandas/tests/test_lib.py @@ -4,6 +4,7 @@ from pandas._libs import lib, writers as libwriters from pandas import Index +import pandas as pd 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): + idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000")], ["foo", "bar"]]) + with tm.assert_produces_warning(RuntimeWarning): + idx.union(idx[:1], sort=None) + class TestIndexing: def test_maybe_indices_to_slice_left_edge(self): From 28871d1af166ea74b464ea8e87874fe48f984c27 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 25 Mar 2020 17:30:35 +0200 Subject: [PATCH 02/15] isort --- pandas/tests/test_lib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/test_lib.py b/pandas/tests/test_lib.py index b8f97275534f6..f9283baa7bbb6 100644 --- a/pandas/tests/test_lib.py +++ b/pandas/tests/test_lib.py @@ -3,8 +3,8 @@ from pandas._libs import lib, writers as libwriters -from pandas import Index import pandas as pd +from pandas import Index import pandas._testing as tm From 3db55196acd643a8826b48a63ad70bf23dc89bda Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 25 Mar 2020 20:36:08 +0200 Subject: [PATCH 03/15] Fixed test case to be lib specific --- pandas/tests/test_lib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/test_lib.py b/pandas/tests/test_lib.py index f9283baa7bbb6..b6f59807eaa15 100644 --- a/pandas/tests/test_lib.py +++ b/pandas/tests/test_lib.py @@ -41,9 +41,9 @@ def test_fast_unique_multiple_list_gen_sort(self): tm.assert_numpy_array_equal(np.array(out), expected) def test_fast_unique_multiple_unsortable_runtimewarning(self): - idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000")], ["foo", "bar"]]) + arr = [np.array(["foo", pd.Timestamp("2000")])] with tm.assert_produces_warning(RuntimeWarning): - idx.union(idx[:1], sort=None) + lib.fast_unique_multiple(arr, sort=None) class TestIndexing: From 8a7f6c93e8bed92887b5bf0292daf311c29a08a1 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 25 Mar 2020 20:40:02 +0200 Subject: [PATCH 04/15] Warning error message --- pandas/_libs/lib.pyx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 927fefbc6b697..fdf8344171abb 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -287,9 +287,7 @@ def fast_unique_multiple(list arrays, sort: bool = True): try: uniques.sort() except TypeError: - warnings.warn( - "Not sure what will be a helpful error message", RuntimeWarning - ) + warnings.warn("The values in the array are unorderable", RuntimeWarning) pass return uniques From 9ce23ab776a3de5a22ddff20f9ed41bfd0def5cc Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 25 Mar 2020 23:41:45 +0200 Subject: [PATCH 05/15] Not asserting the warning if on numpy_dev --- pandas/tests/indexes/multi/test_setops.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/tests/indexes/multi/test_setops.py b/pandas/tests/indexes/multi/test_setops.py index c97704e8a2066..3311552243a03 100644 --- a/pandas/tests/indexes/multi/test_setops.py +++ b/pandas/tests/indexes/multi/test_setops.py @@ -1,6 +1,8 @@ import numpy as np import pytest +from pandas.compat.numpy import _is_numpy_dev + import pandas as pd from pandas import MultiIndex, Series import pandas._testing as tm @@ -337,7 +339,9 @@ 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]) + warn = None if _is_numpy_dev else RuntimeWarning + with tm.assert_produces_warning(warn): + result = idx.union(idx[:1]) tm.assert_index_equal(result, idx) # sort=False From 1c3a45f48c33dab154a2b9a7662463a9090164c8 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 26 Mar 2020 03:10:51 +0200 Subject: [PATCH 06/15] Troubleshoot failing test --- pandas/tests/indexes/multi/test_setops.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/indexes/multi/test_setops.py b/pandas/tests/indexes/multi/test_setops.py index 3311552243a03..f58a7086e3538 100644 --- a/pandas/tests/indexes/multi/test_setops.py +++ b/pandas/tests/indexes/multi/test_setops.py @@ -1,6 +1,7 @@ import numpy as np import pytest +from pandas.compat import PY37 from pandas.compat.numpy import _is_numpy_dev import pandas as pd @@ -339,7 +340,7 @@ def test_union_sort_other_incomparable(): idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]]) # default, sort=None - warn = None if _is_numpy_dev else RuntimeWarning + warn = None if (PY37 and _is_numpy_dev) else RuntimeWarning with tm.assert_produces_warning(warn): result = idx.union(idx[:1]) tm.assert_index_equal(result, idx) From d186e6d50757997a1fda795b2666dee1e527c06a Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 26 Mar 2020 17:31:58 +0200 Subject: [PATCH 07/15] Added information on how to suppress the warning XREF: https://github.com/pandas-dev/pandas/pull/33015#discussion_r398410352 --- pandas/_libs/lib.pyx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index fdf8344171abb..3b89642de6045 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -287,7 +287,11 @@ def fast_unique_multiple(list arrays, sort: bool = True): try: uniques.sort() except TypeError: - warnings.warn("The values in the array are unorderable", RuntimeWarning) + warnings.warn( + "The values in the array are unorderable. " + "Pass `sort=Flase` to suppress this warning.", + RuntimeWarning, + ) pass return uniques From df301eb1fd4b33b5ef1f80ca380d340525c0e661 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 26 Mar 2020 17:39:01 +0200 Subject: [PATCH 08/15] Troubleshoot failing test --- pandas/tests/indexes/multi/test_setops.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/indexes/multi/test_setops.py b/pandas/tests/indexes/multi/test_setops.py index f58a7086e3538..e2f4c9459fa9d 100644 --- a/pandas/tests/indexes/multi/test_setops.py +++ b/pandas/tests/indexes/multi/test_setops.py @@ -2,7 +2,6 @@ import pytest from pandas.compat import PY37 -from pandas.compat.numpy import _is_numpy_dev import pandas as pd from pandas import MultiIndex, Series @@ -340,7 +339,7 @@ def test_union_sort_other_incomparable(): idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]]) # default, sort=None - warn = None if (PY37 and _is_numpy_dev) else RuntimeWarning + warn = None if PY37 else RuntimeWarning with tm.assert_produces_warning(warn): result = idx.union(idx[:1]) tm.assert_index_equal(result, idx) From fde1e1ace12c1aa1fba8678b0ff3f64da351b579 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <50263213+MomIsBestFriend@users.noreply.github.com> Date: Thu, 26 Mar 2020 21:10:34 +0200 Subject: [PATCH 09/15] Update pandas/_libs/lib.pyx Co-Authored-By: Joris Van den Bossche --- pandas/_libs/lib.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 3b89642de6045..de20103643f66 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -289,7 +289,7 @@ def fast_unique_multiple(list arrays, sort: bool = True): except TypeError: warnings.warn( "The values in the array are unorderable. " - "Pass `sort=Flase` to suppress this warning.", + "Pass `sort=False` to suppress this warning.", RuntimeWarning, ) pass From 6d1508d83260f88ff45ef48f3e9ec0c19d3186c1 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 26 Mar 2020 21:23:59 +0200 Subject: [PATCH 10/15] Marked failed test as xfail Everything else failed --- pandas/tests/indexes/multi/test_setops.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/multi/test_setops.py b/pandas/tests/indexes/multi/test_setops.py index e2f4c9459fa9d..64a873efa4335 100644 --- a/pandas/tests/indexes/multi/test_setops.py +++ b/pandas/tests/indexes/multi/test_setops.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from pandas.compat import PY37 +from pandas.compat.numpy import _is_numpy_dev import pandas as pd from pandas import MultiIndex, Series @@ -334,12 +334,13 @@ def test_union_sort_other_empty_sort(slice_): tm.assert_index_equal(result, expected) +@pytest.mark.xfail(reason="`warn` if statement is not working properly") def test_union_sort_other_incomparable(): # https://github.com/pandas-dev/pandas/issues/24959 idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]]) # default, sort=None - warn = None if PY37 else RuntimeWarning + warn = None if _is_numpy_dev else RuntimeWarning with tm.assert_produces_warning(warn): result = idx.union(idx[:1]) tm.assert_index_equal(result, idx) From 1314e998555164753c5c9f1960825a5a47bed357 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 27 Mar 2020 23:43:42 +0300 Subject: [PATCH 11/15] Marked xfailed-strict as False --- pandas/tests/indexes/multi/test_setops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexes/multi/test_setops.py b/pandas/tests/indexes/multi/test_setops.py index 64a873efa4335..4296c9f48d08b 100644 --- a/pandas/tests/indexes/multi/test_setops.py +++ b/pandas/tests/indexes/multi/test_setops.py @@ -334,7 +334,7 @@ def test_union_sort_other_empty_sort(slice_): tm.assert_index_equal(result, expected) -@pytest.mark.xfail(reason="`warn` if statement is not working properly") +@pytest.mark.xfail(reason="`warn` if statement is not working properly", strict=False) def test_union_sort_other_incomparable(): # https://github.com/pandas-dev/pandas/issues/24959 idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]]) From e55bc815f6dc8e7af550109f9bf9f844f18b9e7e Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 29 Mar 2020 12:12:09 +0300 Subject: [PATCH 12/15] Troubleshoot failing test --- pandas/tests/indexes/multi/test_setops.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/indexes/multi/test_setops.py b/pandas/tests/indexes/multi/test_setops.py index 4296c9f48d08b..3311552243a03 100644 --- a/pandas/tests/indexes/multi/test_setops.py +++ b/pandas/tests/indexes/multi/test_setops.py @@ -334,7 +334,6 @@ def test_union_sort_other_empty_sort(slice_): tm.assert_index_equal(result, expected) -@pytest.mark.xfail(reason="`warn` if statement is not working properly", strict=False) def test_union_sort_other_incomparable(): # https://github.com/pandas-dev/pandas/issues/24959 idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]]) From b931025130b203b1b072d1e78752d89a1adad2a5 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 29 Mar 2020 14:41:29 +0300 Subject: [PATCH 13/15] Removed if statement related to numpy-dev --- pandas/tests/indexes/multi/test_setops.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/tests/indexes/multi/test_setops.py b/pandas/tests/indexes/multi/test_setops.py index 3311552243a03..d7427ee622977 100644 --- a/pandas/tests/indexes/multi/test_setops.py +++ b/pandas/tests/indexes/multi/test_setops.py @@ -1,8 +1,6 @@ import numpy as np import pytest -from pandas.compat.numpy import _is_numpy_dev - import pandas as pd from pandas import MultiIndex, Series import pandas._testing as tm @@ -339,8 +337,7 @@ def test_union_sort_other_incomparable(): idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]]) # default, sort=None - warn = None if _is_numpy_dev else RuntimeWarning - with tm.assert_produces_warning(warn): + with tm.assert_produces_warning(RuntimeWarning): result = idx.union(idx[:1]) tm.assert_index_equal(result, idx) From 17a6f99dd5b77df660941a14f86e0e1249d05f06 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 29 Mar 2020 19:29:25 +0300 Subject: [PATCH 14/15] Added whatsnew --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 58ac2b4cba3b7..cce3b0eea195d 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. - .. --------------------------------------------------------------------------- From 83e2e82f129e5726c73c75ceced290316a6917da Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 31 Mar 2020 09:38:18 +0300 Subject: [PATCH 15/15] What's new refrence to this PR XREF: https://github.com/pandas-dev/pandas/pull/33015#discussion_r400535139 --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index cce3b0eea195d..b06b6d19aceb3 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -87,7 +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. +- :meth:`MultiIndex.union` will now raise `RuntimeWarning` if the object inside are unsortable, pass `sort=False` to suppress this warning (:issue:`33015`) - .. ---------------------------------------------------------------------------