Skip to content

Commit 1ca8f9c

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

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
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 = np.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

+12-12
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,10 @@ 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):
753+
if PY3:
754+
with tm.assert_produces_warning(RuntimeWarning):
755+
firstCat = self.strIndex.union(self.dateIndex)
756+
else:
754757
firstCat = self.strIndex.union(self.dateIndex)
755758
secondCat = self.strIndex.union(self.strIndex)
756759

@@ -1309,29 +1312,26 @@ def test_drop(self):
13091312
expected = Index([1, 2])
13101313
tm.assert_index_equal(dropped, expected)
13111314

1312-
def test_tuple_union_bug(self):
1313-
import pandas
1314-
import numpy as np
1315-
1315+
def test_tuples_intersection_union(self):
13161316
aidx1 = np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B')],
13171317
dtype=[('num', int), ('let', 'a1')])
13181318
aidx2 = np.array([(1, 'A'), (2, 'A'), (1, 'B'),
13191319
(2, 'B'), (1, 'C'), (2, 'C')],
13201320
dtype=[('num', int), ('let', 'a1')])
13211321

1322-
idx1 = pandas.Index(aidx1)
1323-
idx2 = pandas.Index(aidx2)
1322+
idx1 = Index(aidx1)
1323+
idx2 = Index(aidx2)
13241324

1325-
# intersection broken?
1325+
# intersection
13261326
int_idx = idx1.intersection(idx2)
1327+
expected = idx1 # pandas.Index(sorted(set(idx1) & set(idx2)))
13271328
# needs to be 1d like idx1 and idx2
1328-
expected = idx1[:4] # pandas.Index(sorted(set(idx1) & set(idx2)))
13291329
assert int_idx.ndim == 1
13301330
tm.assert_index_equal(int_idx, expected)
13311331

1332-
# union broken
1332+
# GH 17376 (union)
13331333
union_idx = idx1.union(idx2)
1334-
expected = idx2
1334+
expected = idx2.sort_values()
13351335
assert union_idx.ndim == 1
13361336
tm.assert_index_equal(union_idx, expected)
13371337

@@ -1941,7 +1941,7 @@ def test_union_base(self):
19411941
tm.assert_index_equal(result, expected)
19421942
else:
19431943
result = first.union(second)
1944-
expected = Index(['b', 2, 'c', 0, 'a', 1])
1944+
expected = Index([0, 1, 2, 'a', 'b', 'c'])
19451945
tm.assert_index_equal(result, expected)
19461946

19471947
# GH 10149

0 commit comments

Comments
 (0)