Skip to content

Commit 98d5b8e

Browse files
committed
Fix fixture in test and move sort check
1 parent 320872c commit 98d5b8e

File tree

2 files changed

+49
-89
lines changed

2 files changed

+49
-89
lines changed

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,7 @@ def union(self, other, sort=None):
23202320
else:
23212321
rvals = other._values
23222322

2323-
if self.is_monotonic and other.is_monotonic and sort is None:
2323+
if sort is None and self.is_monotonic and other.is_monotonic:
23242324
try:
23252325
result = self._outer_indexer(lvals, rvals)[0]
23262326
except TypeError:

pandas/tests/indexes/test_range.py

+48-88
Original file line numberDiff line numberDiff line change
@@ -579,102 +579,62 @@ def test_union_noncomparable(self):
579579
tm.assert_index_equal(result, expected)
580580

581581
@pytest.fixture
582-
def union_inputs(self):
583-
"""Inputs for RangeIndex.union tests"""
582+
def union_fixture(self):
583+
"""Inputs and expected outputs for RangeIndex.union tests"""
584584
RI = RangeIndex
585585
I64 = Int64Index
586586

587-
inputs = [(RI(0, 10, 1), RI(0, 10, 1)),
588-
(RI(0, 10, 1), RI(5, 20, 1)),
589-
(RI(0, 10, 1), RI(10, 20, 1)),
590-
(RI(0, -10, -1), RI(0, -10, -1)),
591-
(RI(0, -10, -1), RI(-10, -20, -1)),
592-
(RI(0, 10, 2), RI(1, 10, 2)),
593-
(RI(0, 11, 2), RI(1, 12, 2)),
594-
(RI(0, 21, 4), RI(-2, 24, 4)),
595-
(RI(0, -20, -2), RI(-1, -21, -2)),
596-
(RI(0, 100, 5), RI(0, 100, 20)),
597-
(RI(0, -100, -5), RI(5, -100, -20)),
598-
(RI(0, -11, -1), RI(1, -12, -4)),
599-
(RI(0), RI(0)),
600-
(RI(0, -10, -2), RI(0)),
601-
(RI(0, 100, 2), RI(100, 150, 200)),
602-
(RI(0, -100, -2), RI(-100, 50, 102)),
603-
(RI(0, -100, -1), RI(0, -50, -3)),
604-
(RI(0, 1, 1), RI(5, 6, 10)),
605-
(RI(0, 10, 5), RI(-5, -6, -20)),
606-
(RI(0, 3, 1), RI(4, 5, 1)),
607-
(RI(0, 10, 1), I64([])),
608-
(RI(0), I64([1, 5, 6]))]
609-
610-
return inputs
611-
612-
def test_union_sorted(self, union_inputs):
613-
RI = RangeIndex
614-
I64 = Int64Index
615-
616-
expected = [RI(0, 10, 1),
617-
RI(0, 20, 1),
618-
RI(0, 20, 1),
619-
RI(0, -10, -1),
620-
RI(-19, 1, 1),
621-
RI(0, 10, 1),
622-
RI(0, 12, 1),
623-
RI(-2, 24, 2),
624-
RI(-19, 1, 1),
625-
RI(0, 100, 5),
626-
RI(-95, 10, 5),
627-
RI(-11, 2, 1),
628-
RI(0),
629-
RI(0, -10, -2),
630-
RI(0, 102, 2),
631-
RI(-100, 4, 2),
632-
RI(-99, 1, 1),
633-
RI(0, 6, 5),
634-
RI(-5, 10, 5),
635-
I64([0, 1, 2, 4]),
636-
RI(0, 10, 1),
637-
I64([1, 5, 6])]
638-
639-
for ((idx1, idx2), expected_sorted) in zip(union_inputs, expected):
640-
res1 = idx1.union(idx2)
587+
return [(RI(0, 10, 1), RI(0, 10, 1), RI(0, 10, 1), RI(0, 10, 1)),
588+
(RI(0, 10, 1), RI(5, 20, 1), RI(0, 20, 1), I64(range(20))),
589+
(RI(0, 10, 1), RI(10, 20, 1), RI(0, 20, 1), I64(range(20))),
590+
(RI(0, -10, -1), RI(0, -10, -1), RI(0, -10, -1),
591+
RI(0, -10, -1)),
592+
(RI(0, -10, -1), RI(-10, -20, -1), RI(-19, 1, 1),
593+
I64(range(0, -20, -1))),
594+
(RI(0, 10, 2), RI(1, 10, 2), RI(0, 10, 1),
595+
I64(list(range(0, 10, 2)) + list(range(1, 10, 2)))),
596+
(RI(0, 11, 2), RI(1, 12, 2), RI(0, 12, 1),
597+
I64(list(range(0, 11, 2)) + list(range(1, 12, 2)))),
598+
(RI(0, 21, 4), RI(-2, 24, 4), RI(-2, 24, 2),
599+
I64(list(range(0, 21, 4)) + list(range(-2, 24, 4)))),
600+
(RI(0, -20, -2), RI(-1, -21, -2), RI(-19, 1, 1),
601+
I64(list(range(0, -20, -2)) + list(range(-1, -21, -2)))),
602+
(RI(0, 100, 5), RI(0, 100, 20), RI(0, 100, 5),
603+
I64(range(0, 100, 5))),
604+
(RI(0, -100, -5), RI(5, -100, -20), RI(-95, 10, 5),
605+
I64(list(range(0, -100, -5)) + [5])),
606+
(RI(0, -11, -1), RI(1, -12, -4), RI(-11, 2, 1),
607+
I64(list(range(0, -11, -1)) + [1, -11])),
608+
(RI(0), RI(0), RI(0), RI(0)),
609+
(RI(0, -10, -2), RI(0), RI(0, -10, -2), RI(0, -10, -2)),
610+
(RI(0, 100, 2), RI(100, 150, 200), RI(0, 102, 2),
611+
I64(range(0, 102, 2))),
612+
(RI(0, -100, -2), RI(-100, 50, 102), RI(-100, 4, 2),
613+
I64(list(range(0, -100, -2)) + [-100, 2])),
614+
(RI(0, -100, -1), RI(0, -50, -3), RI(-99, 1, 1),
615+
I64(list(range(0, -100, -1)))),
616+
(RI(0, 1, 1), RI(5, 6, 10), RI(0, 6, 5), I64([0, 5])),
617+
(RI(0, 10, 5), RI(-5, -6, -20), RI(-5, 10, 5),
618+
I64([0, 5, -5])),
619+
(RI(0, 3, 1), RI(4, 5, 1), I64([0, 1, 2, 4]),
620+
I64([0, 1, 2, 4])),
621+
(RI(0, 10, 1), I64([]), RI(0, 10, 1), RI(0, 10, 1)),
622+
(RI(0), I64([1, 5, 6]), I64([1, 5, 6]), I64([1, 5, 6]))]
623+
624+
def test_union_sorted(self, union_fixture):
625+
626+
for (idx1, idx2, expected_sorted, expected_notsorted) in union_fixture:
627+
res1 = idx1.union(idx2, sort=None)
641628
tm.assert_index_equal(res1, expected_sorted, exact=True)
642-
res2 = idx2.union(idx1)
643-
res3 = idx1._int64index.union(idx2)
644-
tm.assert_index_equal(res2, expected_sorted, exact=True)
645-
tm.assert_index_equal(res3, expected_sorted)
646629

647-
def test_union_notsorted(self, union_inputs):
648-
RI = RangeIndex
649-
I64 = Int64Index
650-
651-
expected = [RI(0, 10, 1),
652-
I64(range(20)),
653-
I64(range(20)),
654-
RI(0, -10, -1),
655-
I64(range(0, -20, -1)),
656-
I64(list(range(0, 10, 2)) + list(range(1, 10, 2))),
657-
I64(list(range(0, 11, 2)) + list(range(1, 12, 2))),
658-
I64(list(range(0, 21, 4)) + list(range(-2, 24, 4))),
659-
I64(list(range(0, -20, -2)) + list(range(-1, -21, -2))),
660-
I64(range(0, 100, 5)),
661-
I64(list(range(0, -100, -5)) + [5]),
662-
I64(list(range(0, -11, -1)) + [1, -11]),
663-
RI(0),
664-
RI(0, -10, -2),
665-
I64(range(0, 102, 2)),
666-
I64(list(range(0, -100, -2)) + [-100, 2]),
667-
I64(list(range(0, -100, -1))),
668-
I64([0, 5]),
669-
I64([0, 5, -5]),
670-
I64([0, 1, 2, 4]),
671-
RI(0, 10, 1),
672-
I64([1, 5, 6])]
673-
674-
for ((idx1, idx2), expected_notsorted) in zip(union_inputs, expected):
675630
res1 = idx1.union(idx2, sort=False)
676631
tm.assert_index_equal(res1, expected_notsorted, exact=True)
677632

633+
res2 = idx2.union(idx1, sort=None)
634+
res3 = idx1._int64index.union(idx2, sort=None)
635+
tm.assert_index_equal(res2, expected_sorted, exact=True)
636+
tm.assert_index_equal(res3, expected_sorted)
637+
678638
def test_nbytes(self):
679639

680640
# memory savings vs int index

0 commit comments

Comments
 (0)