Skip to content

Commit a6c3174

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

File tree

2 files changed

+19
-34
lines changed

2 files changed

+19
-34
lines changed

pandas/core/indexes/base.py

+7-22
Original file line numberDiff line numberDiff line change
@@ -2178,7 +2178,6 @@ def union(self, other):
21782178

21792179
if len(self) == 0:
21802180
return other._get_consensus_name(self)
2181-
21822181
if not is_dtype_equal(self.dtype, other.dtype):
21832182
this = self.astype('O')
21842183
other = other.astype('O')
@@ -2195,35 +2194,21 @@ def union(self, other):
21952194
value_set = set(self._values)
21962195
result.extend([x for x in other._values if x not in value_set])
21972196
else:
2198-
indexer = self.get_indexer(other)
2199-
indexer, = (indexer == -1).nonzero()
2200-
2197+
indexer = np.where(self.get_indexer(other) == -1)[0]
22012198
if len(indexer) > 0:
22022199
other_diff = algos.take_nd(other._values, indexer,
22032200
allow_fill=False)
22042201
result = _concat._concat_compat((self._values, other_diff))
22052202

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-
22182203
else:
22192204
result = self._values
22202205

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)
2206+
try:
2207+
result = np.sort(result)
2208+
except TypeError as e:
2209+
warnings.warn("%s, sort order is undefined for "
2210+
"incomparable objects" % e, RuntimeWarning,
2211+
stacklevel=3)
22272212

22282213
# for subclasses
22292214
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)