Skip to content

Commit 0a725fc

Browse files
committed
BUG: Try to sort result of Index.union rather than guessing sortability
closes #17376
1 parent 0d676a3 commit 0a725fc

File tree

4 files changed

+34
-77
lines changed

4 files changed

+34
-77
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ Indexing
359359
- Bug in ``.iloc`` when used with inplace addition or assignment and an int indexer on a ``MultiIndex`` causing the wrong indexes to be read from and written to (:issue:`17148`)
360360
- Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`)
361361
- Bug in ``CategoricalIndex`` reindexing in which specified indices containing duplicates were not being respected (:issue:`17323`)
362+
- Bug in the order of the result of ``Index.union()`` when indexes contain tuples (:issue:`17376`)
362363

363364
I/O
364365
^^^

pandas/core/indexes/base.py

+7-21
Original file line numberDiff line numberDiff line change
@@ -2195,35 +2195,21 @@ def union(self, other):
21952195
value_set = set(self._values)
21962196
result.extend([x for x in other._values if x not in value_set])
21972197
else:
2198-
indexer = self.get_indexer(other)
2199-
indexer, = (indexer == -1).nonzero()
2200-
2198+
indexer = np.where(self.get_indexer(other) == -1)[0]
22012199
if len(indexer) > 0:
22022200
other_diff = algos.take_nd(other._values, indexer,
22032201
allow_fill=False)
22042202
result = _concat._concat_compat((self._values, other_diff))
22052203

2206-
try:
2207-
self._values[0] < other_diff[0]
2208-
except TypeError as e:
2209-
warnings.warn("%s, sort order is undefined for "
2210-
"incomparable objects" % e, RuntimeWarning,
2211-
stacklevel=3)
2212-
else:
2213-
types = frozenset((self.inferred_type,
2214-
other.inferred_type))
2215-
if not types & _unsortable_types:
2216-
result.sort()
2217-
22182204
else:
22192205
result = self._values
22202206

2221-
try:
2222-
result = np.sort(result)
2223-
except TypeError as e:
2224-
warnings.warn("%s, sort order is undefined for "
2225-
"incomparable objects" % e, RuntimeWarning,
2226-
stacklevel=3)
2207+
try:
2208+
result = sorting.safe_sort(result)
2209+
except TypeError as e:
2210+
warnings.warn("%s, sort order is undefined for "
2211+
"incomparable objects" % e, RuntimeWarning,
2212+
stacklevel=3)
22272213

22282214
# for subclasses
22292215
return self._wrap_union_result(other, result)

pandas/tests/indexes/test_base.py

+21-35
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,7 @@ def test_union(self):
750750
expected = Index(list('ab'), name='A')
751751
tm.assert_index_equal(union, expected)
752752

753-
with tm.assert_produces_warning(RuntimeWarning):
754-
firstCat = self.strIndex.union(self.dateIndex)
753+
firstCat = self.strIndex.union(self.dateIndex)
755754
secondCat = self.strIndex.union(self.strIndex)
756755

757756
if self.dateIndex.dtype == np.object_:
@@ -1309,29 +1308,26 @@ def test_drop(self):
13091308
expected = Index([1, 2])
13101309
tm.assert_index_equal(dropped, expected)
13111310

1312-
def test_tuple_union_bug(self):
1313-
import pandas
1314-
import numpy as np
1315-
1311+
def test_tuples_intersection_union(self):
13161312
aidx1 = np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B')],
13171313
dtype=[('num', int), ('let', 'a1')])
13181314
aidx2 = np.array([(1, 'A'), (2, 'A'), (1, 'B'),
13191315
(2, 'B'), (1, 'C'), (2, 'C')],
13201316
dtype=[('num', int), ('let', 'a1')])
13211317

1322-
idx1 = pandas.Index(aidx1)
1323-
idx2 = pandas.Index(aidx2)
1318+
idx1 = Index(aidx1)
1319+
idx2 = Index(aidx2)
13241320

1325-
# intersection broken?
1321+
# intersection
13261322
int_idx = idx1.intersection(idx2)
1323+
expected = idx1 # pandas.Index(sorted(set(idx1) & set(idx2)))
13271324
# needs to be 1d like idx1 and idx2
1328-
expected = idx1[:4] # pandas.Index(sorted(set(idx1) & set(idx2)))
13291325
assert int_idx.ndim == 1
13301326
tm.assert_index_equal(int_idx, expected)
13311327

1332-
# union broken
1328+
# GH 17376 (union)
13331329
union_idx = idx1.union(idx2)
1334-
expected = idx2
1330+
expected = idx2.sort_values()
13351331
assert union_idx.ndim == 1
13361332
tm.assert_index_equal(union_idx, expected)
13371333

@@ -1507,13 +1503,19 @@ def test_outer_join_sort(self):
15071503
left_idx = Index(np.random.permutation(15))
15081504
right_idx = tm.makeDateIndex(10)
15091505

1510-
with tm.assert_produces_warning(RuntimeWarning):
1506+
if PY3:
1507+
with tm.assert_produces_warning(RuntimeWarning):
1508+
joined = left_idx.join(right_idx, how='outer')
1509+
else:
15111510
joined = left_idx.join(right_idx, how='outer')
15121511

15131512
# right_idx in this case because DatetimeIndex has join precedence over
15141513
# Int64Index
1515-
with tm.assert_produces_warning(RuntimeWarning):
1516-
expected = right_idx.astype(object).union(left_idx.astype(object))
1514+
if PY3:
1515+
with tm.assert_produces_warning(RuntimeWarning):
1516+
expected = right_idx.astype(object).union(left_idx)
1517+
else:
1518+
expected = right_idx.astype(object).union(left_idx)
15171519
tm.assert_index_equal(joined, expected)
15181520

15191521
def test_nan_first_take_datetime(self):
@@ -1897,10 +1899,7 @@ def test_copy_name(self):
18971899
s1 = Series(2, index=first)
18981900
s2 = Series(3, index=second[:-1])
18991901

1900-
warning_type = RuntimeWarning if PY3 else None
1901-
with tm.assert_produces_warning(warning_type):
1902-
# Python 3: Unorderable types
1903-
s3 = s1 * s2
1902+
s3 = s1 * s2
19041903

19051904
assert s3.index.name == 'mario'
19061905

@@ -1933,27 +1932,14 @@ def test_union_base(self):
19331932
first = idx[3:]
19341933
second = idx[:5]
19351934

1936-
if PY3:
1937-
with tm.assert_produces_warning(RuntimeWarning):
1938-
# unorderable types
1939-
result = first.union(second)
1940-
expected = Index(['b', 2, 'c', 0, 'a', 1])
1941-
tm.assert_index_equal(result, expected)
1942-
else:
1943-
result = first.union(second)
1944-
expected = Index(['b', 2, 'c', 0, 'a', 1])
1945-
tm.assert_index_equal(result, expected)
1935+
expected = Index([0, 1, 2, 'a', 'b', 'c'])
1936+
result = first.union(second)
1937+
tm.assert_index_equal(result, expected)
19461938

19471939
# GH 10149
19481940
cases = [klass(second.values)
19491941
for klass in [np.array, Series, list]]
19501942
for case in cases:
1951-
if PY3:
1952-
with tm.assert_produces_warning(RuntimeWarning):
1953-
# unorderable types
1954-
result = first.union(case)
1955-
assert tm.equalContents(result, idx)
1956-
else:
19571943
result = first.union(case)
19581944
assert tm.equalContents(result, idx)
19591945

pandas/tests/series/test_operators.py

+5-21
Original file line numberDiff line numberDiff line change
@@ -1198,11 +1198,7 @@ def test_comparison_label_based(self):
11981198
assert_series_equal(result, a[a])
11991199

12001200
for e in [Series(['z'])]:
1201-
if compat.PY3:
1202-
with tm.assert_produces_warning(RuntimeWarning):
1203-
result = a[a | e]
1204-
else:
1205-
result = a[a | e]
1201+
result = a[a | e]
12061202
assert_series_equal(result, a[a])
12071203

12081204
# vs scalars
@@ -1394,24 +1390,12 @@ def test_operators_bitwise(self):
13941390
pytest.raises(TypeError, lambda: s_0123 & [0.1, 4, 3.14, 2])
13951391

13961392
# s_0123 will be all false now because of reindexing like s_tft
1397-
if compat.PY3:
1398-
# unable to sort incompatible object via .union.
1399-
exp = Series([False] * 7, index=['b', 'c', 'a', 0, 1, 2, 3])
1400-
with tm.assert_produces_warning(RuntimeWarning):
1401-
assert_series_equal(s_tft & s_0123, exp)
1402-
else:
1403-
exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c'])
1404-
assert_series_equal(s_tft & s_0123, exp)
1393+
exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c'])
1394+
assert_series_equal(s_tft & s_0123, exp)
14051395

14061396
# s_tft will be all false now because of reindexing like s_0123
1407-
if compat.PY3:
1408-
# unable to sort incompatible object via .union.
1409-
exp = Series([False] * 7, index=[0, 1, 2, 3, 'b', 'c', 'a'])
1410-
with tm.assert_produces_warning(RuntimeWarning):
1411-
assert_series_equal(s_0123 & s_tft, exp)
1412-
else:
1413-
exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c'])
1414-
assert_series_equal(s_0123 & s_tft, exp)
1397+
exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c'])
1398+
assert_series_equal(s_0123 & s_tft, exp)
14151399

14161400
assert_series_equal(s_0123 & False, Series([False] * 4))
14171401
assert_series_equal(s_0123 ^ False, Series([False, True, True, True]))

0 commit comments

Comments
 (0)