Skip to content

Commit

Permalink
CLN: Refactor some sorting code in Index set operations (pandas-dev#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
reidy-p authored and Pingviinituutti committed Feb 28, 2019
1 parent 43a4ae5 commit a7ae768
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 63 deletions.
24 changes: 6 additions & 18 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 6 additions & 24 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,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_:
Expand Down Expand Up @@ -1613,7 +1612,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):
Expand Down Expand Up @@ -2240,10 +2239,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'

Expand Down Expand Up @@ -2272,16 +2268,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", [
Expand All @@ -2292,14 +2281,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)

Expand Down
26 changes: 5 additions & 21 deletions pandas/tests/series/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a7ae768

Please sign in to comment.