Skip to content

Commit 924869a

Browse files
reidy-pPingviinituutti
authored andcommitted
CLN: Refactor some sorting code in Index set operations (pandas-dev#24533)
1 parent cd55b3c commit 924869a

File tree

3 files changed

+17
-63
lines changed

3 files changed

+17
-63
lines changed

pandas/core/indexes/base.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -2302,27 +2302,15 @@ def union(self, other):
23022302
allow_fill=False)
23032303
result = _concat._concat_compat((lvals, other_diff))
23042304

2305-
try:
2306-
lvals[0] < other_diff[0]
2307-
except TypeError as e:
2308-
warnings.warn("%s, sort order is undefined for "
2309-
"incomparable objects" % e, RuntimeWarning,
2310-
stacklevel=3)
2311-
else:
2312-
types = frozenset((self.inferred_type,
2313-
other.inferred_type))
2314-
if not types & _unsortable_types:
2315-
result.sort()
2316-
23172305
else:
23182306
result = lvals
23192307

2320-
try:
2321-
result = np.sort(result)
2322-
except TypeError as e:
2323-
warnings.warn("%s, sort order is undefined for "
2324-
"incomparable objects" % e, RuntimeWarning,
2325-
stacklevel=3)
2308+
try:
2309+
result = sorting.safe_sort(result)
2310+
except TypeError as e:
2311+
warnings.warn("%s, sort order is undefined for "
2312+
"incomparable objects" % e, RuntimeWarning,
2313+
stacklevel=3)
23262314

23272315
# for subclasses
23282316
return self._wrap_setop_result(other, result)

pandas/tests/indexes/test_base.py

+6-24
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,7 @@ def test_union_name_preservation(self, first_list, second_list, first_name,
803803

804804
def test_union_dt_as_obj(self):
805805
# TODO: Replace with fixturesult
806-
with tm.assert_produces_warning(RuntimeWarning):
807-
firstCat = self.strIndex.union(self.dateIndex)
806+
firstCat = self.strIndex.union(self.dateIndex)
808807
secondCat = self.strIndex.union(self.strIndex)
809808

810809
if self.dateIndex.dtype == np.object_:
@@ -1613,7 +1612,7 @@ def test_drop_tuple(self, values, to_drop):
16131612
@pytest.mark.parametrize("method,expected", [
16141613
('intersection', np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B')],
16151614
dtype=[('num', int), ('let', 'a1')])),
1616-
('union', np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B'), (1, 'C'),
1615+
('union', np.array([(1, 'A'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B'),
16171616
(2, 'C')], dtype=[('num', int), ('let', 'a1')]))
16181617
])
16191618
def test_tuple_union_bug(self, method, expected):
@@ -2240,10 +2239,7 @@ def test_copy_name(self):
22402239
s1 = Series(2, index=first)
22412240
s2 = Series(3, index=second[:-1])
22422241

2243-
warning_type = RuntimeWarning if PY3 else None
2244-
with tm.assert_produces_warning(warning_type):
2245-
# Python 3: Unorderable types
2246-
s3 = s1 * s2
2242+
s3 = s1 * s2
22472243

22482244
assert s3.index.name == 'mario'
22492245

@@ -2272,16 +2268,9 @@ def test_union_base(self):
22722268
first = index[3:]
22732269
second = index[:5]
22742270

2275-
if PY3:
2276-
# unorderable types
2277-
warn_type = RuntimeWarning
2278-
else:
2279-
warn_type = None
2280-
2281-
with tm.assert_produces_warning(warn_type):
2282-
result = first.union(second)
2271+
result = first.union(second)
22832272

2284-
expected = Index(['b', 2, 'c', 0, 'a', 1])
2273+
expected = Index([0, 1, 2, 'a', 'b', 'c'])
22852274
tm.assert_index_equal(result, expected)
22862275

22872276
@pytest.mark.parametrize("klass", [
@@ -2292,14 +2281,7 @@ def test_union_different_type_base(self, klass):
22922281
first = index[3:]
22932282
second = index[:5]
22942283

2295-
if PY3:
2296-
# unorderable types
2297-
warn_type = RuntimeWarning
2298-
else:
2299-
warn_type = None
2300-
2301-
with tm.assert_produces_warning(warn_type):
2302-
result = first.union(klass(second.values))
2284+
result = first.union(klass(second.values))
23032285

23042286
assert tm.equalContents(result, index)
23052287

pandas/tests/series/test_operators.py

+5-21
Original file line numberDiff line numberDiff line change
@@ -120,24 +120,12 @@ def test_operators_bitwise(self):
120120
s_0123 & [0.1, 4, 3.14, 2]
121121

122122
# s_0123 will be all false now because of reindexing like s_tft
123-
if compat.PY3:
124-
# unable to sort incompatible object via .union.
125-
exp = Series([False] * 7, index=['b', 'c', 'a', 0, 1, 2, 3])
126-
with tm.assert_produces_warning(RuntimeWarning):
127-
assert_series_equal(s_tft & s_0123, exp)
128-
else:
129-
exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c'])
130-
assert_series_equal(s_tft & s_0123, exp)
123+
exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c'])
124+
assert_series_equal(s_tft & s_0123, exp)
131125

132126
# s_tft will be all false now because of reindexing like s_0123
133-
if compat.PY3:
134-
# unable to sort incompatible object via .union.
135-
exp = Series([False] * 7, index=[0, 1, 2, 3, 'b', 'c', 'a'])
136-
with tm.assert_produces_warning(RuntimeWarning):
137-
assert_series_equal(s_0123 & s_tft, exp)
138-
else:
139-
exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c'])
140-
assert_series_equal(s_0123 & s_tft, exp)
127+
exp = Series([False] * 7, index=[0, 1, 2, 3, 'a', 'b', 'c'])
128+
assert_series_equal(s_0123 & s_tft, exp)
141129

142130
assert_series_equal(s_0123 & False, Series([False] * 4))
143131
assert_series_equal(s_0123 ^ False, Series([False, True, True, True]))
@@ -280,11 +268,7 @@ def test_logical_ops_label_based(self):
280268
assert_series_equal(result, a[a])
281269

282270
for e in [Series(['z'])]:
283-
if compat.PY3:
284-
with tm.assert_produces_warning(RuntimeWarning):
285-
result = a[a | e]
286-
else:
287-
result = a[a | e]
271+
result = a[a | e]
288272
assert_series_equal(result, a[a])
289273

290274
# vs scalars

0 commit comments

Comments
 (0)