diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 07aec6a0d833f..1380c5caed1c9 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2302,27 +2302,15 @@ def union(self, other): allow_fill=False) result = _concat._concat_compat((lvals, other_diff)) - try: - lvals[0] < other_diff[0] - except TypeError as e: - warnings.warn("%s, sort order is undefined for " - "incomparable objects" % e, RuntimeWarning, - stacklevel=3) - else: - types = frozenset((self.inferred_type, - other.inferred_type)) - if not types & _unsortable_types: - result.sort() - else: result = lvals - try: - result = np.sort(result) - except TypeError as e: - warnings.warn("%s, sort order is undefined for " - "incomparable objects" % e, RuntimeWarning, - stacklevel=3) + try: + result = sorting.safe_sort(result) + except TypeError as e: + warnings.warn("%s, sort order is undefined for " + "incomparable objects" % e, RuntimeWarning, + stacklevel=3) # for subclasses return self._wrap_setop_result(other, result) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index b13110a04e1c1..2108206a8cbe5 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -805,8 +805,7 @@ def test_union_name_preservation(self, first_list, second_list, first_name, def test_union_dt_as_obj(self): # TODO: Replace with fixturesult - with tm.assert_produces_warning(RuntimeWarning): - firstCat = self.strIndex.union(self.dateIndex) + firstCat = self.strIndex.union(self.dateIndex) secondCat = self.strIndex.union(self.strIndex) if self.dateIndex.dtype == np.object_: @@ -1615,7 +1614,7 @@ def test_drop_tuple(self, values, to_drop): @pytest.mark.parametrize("method,expected", [ ('intersection', np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B')], dtype=[('num', int), ('let', 'a1')])), - ('union', np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B'), (1, 'C'), + ('union', np.array([(1, 'A'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B'), (2, 'C')], dtype=[('num', int), ('let', 'a1')])) ]) def test_tuple_union_bug(self, method, expected): @@ -2242,10 +2241,7 @@ def test_copy_name(self): s1 = Series(2, index=first) s2 = Series(3, index=second[:-1]) - warning_type = RuntimeWarning if PY3 else None - with tm.assert_produces_warning(warning_type): - # Python 3: Unorderable types - s3 = s1 * s2 + s3 = s1 * s2 assert s3.index.name == 'mario' @@ -2274,16 +2270,9 @@ def test_union_base(self): first = index[3:] second = index[:5] - if PY3: - # unorderable types - warn_type = RuntimeWarning - else: - warn_type = None - - with tm.assert_produces_warning(warn_type): - result = first.union(second) + result = first.union(second) - expected = Index(['b', 2, 'c', 0, 'a', 1]) + expected = Index([0, 1, 2, 'a', 'b', 'c']) tm.assert_index_equal(result, expected) @pytest.mark.parametrize("klass", [ @@ -2294,14 +2283,7 @@ def test_union_different_type_base(self, klass): first = index[3:] second = index[:5] - if PY3: - # unorderable types - warn_type = RuntimeWarning - else: - warn_type = None - - with tm.assert_produces_warning(warn_type): - result = first.union(klass(second.values)) + result = first.union(klass(second.values)) assert tm.equalContents(result, index) diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index f6fb5f0c46cc8..4d3c9926fc5ae 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -120,24 +120,12 @@ def test_operators_bitwise(self): s_0123 & [0.1, 4, 3.14, 2] # s_0123 will be all false now because of reindexing like s_tft - if compat.PY3: - # unable to sort incompatible object via .union. - exp = Series([False] * 7, index=['b', 'c', 'a', 0, 1, 2, 3]) - with tm.assert_produces_warning(RuntimeWarning): - assert_series_equal(s_tft & s_0123, exp) - else: - exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c']) - assert_series_equal(s_tft & s_0123, exp) + exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c']) + assert_series_equal(s_tft & s_0123, exp) # s_tft will be all false now because of reindexing like s_0123 - if compat.PY3: - # unable to sort incompatible object via .union. - exp = Series([False] * 7, index=[0, 1, 2, 3, 'b', 'c', 'a']) - with tm.assert_produces_warning(RuntimeWarning): - assert_series_equal(s_0123 & s_tft, exp) - else: - exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c']) - assert_series_equal(s_0123 & s_tft, exp) + exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c']) + assert_series_equal(s_0123 & s_tft, exp) assert_series_equal(s_0123 & False, Series([False] * 4)) assert_series_equal(s_0123 ^ False, Series([False, True, True, True])) @@ -280,11 +268,7 @@ def test_logical_ops_label_based(self): assert_series_equal(result, a[a]) for e in [Series(['z'])]: - if compat.PY3: - with tm.assert_produces_warning(RuntimeWarning): - result = a[a | e] - else: - result = a[a | e] + result = a[a | e] assert_series_equal(result, a[a]) # vs scalars