|
11 | 11 |
|
12 | 12 | from .test_numeric import Numeric
|
13 | 13 |
|
| 14 | +# aliases to make some tests easier to read |
| 15 | +RI = RangeIndex |
| 16 | +I64 = Int64Index |
| 17 | +F64 = Float64Index |
| 18 | +OI = Index |
| 19 | + |
14 | 20 |
|
15 | 21 | class TestRangeIndex(Numeric):
|
16 | 22 | _holder = RangeIndex
|
@@ -565,51 +571,73 @@ def test_intersection(self, sort):
|
565 | 571 | expected = RangeIndex(0, 0, 1)
|
566 | 572 | tm.assert_index_equal(result, expected)
|
567 | 573 |
|
568 |
| - def test_union_noncomparable(self): |
| 574 | + @pytest.mark.parametrize('sort', [False, None]) |
| 575 | + def test_union_noncomparable(self, sort): |
569 | 576 | from datetime import datetime, timedelta
|
570 | 577 | # corner case, non-Int64Index
|
571 | 578 | now = datetime.now()
|
572 | 579 | other = Index([now + timedelta(i) for i in range(4)], dtype=object)
|
573 |
| - result = self.index.union(other) |
| 580 | + result = self.index.union(other, sort=sort) |
574 | 581 | expected = Index(np.concatenate((self.index, other)))
|
575 | 582 | tm.assert_index_equal(result, expected)
|
576 | 583 |
|
577 |
| - result = other.union(self.index) |
| 584 | + result = other.union(self.index, sort=sort) |
578 | 585 | expected = Index(np.concatenate((other, self.index)))
|
579 | 586 | tm.assert_index_equal(result, expected)
|
580 | 587 |
|
581 |
| - def test_union(self): |
582 |
| - RI = RangeIndex |
583 |
| - I64 = Int64Index |
584 |
| - cases = [(RI(0, 10, 1), RI(0, 10, 1), RI(0, 10, 1)), |
585 |
| - (RI(0, 10, 1), RI(5, 20, 1), RI(0, 20, 1)), |
586 |
| - (RI(0, 10, 1), RI(10, 20, 1), RI(0, 20, 1)), |
587 |
| - (RI(0, -10, -1), RI(0, -10, -1), RI(0, -10, -1)), |
588 |
| - (RI(0, -10, -1), RI(-10, -20, -1), RI(-19, 1, 1)), |
589 |
| - (RI(0, 10, 2), RI(1, 10, 2), RI(0, 10, 1)), |
590 |
| - (RI(0, 11, 2), RI(1, 12, 2), RI(0, 12, 1)), |
591 |
| - (RI(0, 21, 4), RI(-2, 24, 4), RI(-2, 24, 2)), |
592 |
| - (RI(0, -20, -2), RI(-1, -21, -2), RI(-19, 1, 1)), |
593 |
| - (RI(0, 100, 5), RI(0, 100, 20), RI(0, 100, 5)), |
594 |
| - (RI(0, -100, -5), RI(5, -100, -20), RI(-95, 10, 5)), |
595 |
| - (RI(0, -11, -1), RI(1, -12, -4), RI(-11, 2, 1)), |
596 |
| - (RI(0), RI(0), RI(0)), |
597 |
| - (RI(0, -10, -2), RI(0), RI(0, -10, -2)), |
598 |
| - (RI(0, 100, 2), RI(100, 150, 200), RI(0, 102, 2)), |
599 |
| - (RI(0, -100, -2), RI(-100, 50, 102), RI(-100, 4, 2)), |
600 |
| - (RI(0, -100, -1), RI(0, -50, -3), RI(-99, 1, 1)), |
601 |
| - (RI(0, 1, 1), RI(5, 6, 10), RI(0, 6, 5)), |
602 |
| - (RI(0, 10, 5), RI(-5, -6, -20), RI(-5, 10, 5)), |
603 |
| - (RI(0, 3, 1), RI(4, 5, 1), I64([0, 1, 2, 4])), |
604 |
| - (RI(0, 10, 1), I64([]), RI(0, 10, 1)), |
605 |
| - (RI(0), I64([1, 5, 6]), I64([1, 5, 6]))] |
606 |
| - for idx1, idx2, expected in cases: |
607 |
| - res1 = idx1.union(idx2) |
608 |
| - res2 = idx2.union(idx1) |
609 |
| - res3 = idx1._int64index.union(idx2) |
610 |
| - tm.assert_index_equal(res1, expected, exact=True) |
611 |
| - tm.assert_index_equal(res2, expected, exact=True) |
612 |
| - tm.assert_index_equal(res3, expected) |
| 588 | + @pytest.fixture(params=[ |
| 589 | + (RI(0, 10, 1), RI(0, 10, 1), RI(0, 10, 1), RI(0, 10, 1)), |
| 590 | + (RI(0, 10, 1), RI(5, 20, 1), RI(0, 20, 1), I64(range(20))), |
| 591 | + (RI(0, 10, 1), RI(10, 20, 1), RI(0, 20, 1), I64(range(20))), |
| 592 | + (RI(0, -10, -1), RI(0, -10, -1), RI(0, -10, -1), RI(0, -10, -1)), |
| 593 | + (RI(0, -10, -1), RI(-10, -20, -1), RI(-19, 1, 1), |
| 594 | + I64(range(0, -20, -1))), |
| 595 | + (RI(0, 10, 2), RI(1, 10, 2), RI(0, 10, 1), |
| 596 | + I64(list(range(0, 10, 2)) + list(range(1, 10, 2)))), |
| 597 | + (RI(0, 11, 2), RI(1, 12, 2), RI(0, 12, 1), |
| 598 | + I64(list(range(0, 11, 2)) + list(range(1, 12, 2)))), |
| 599 | + (RI(0, 21, 4), RI(-2, 24, 4), RI(-2, 24, 2), |
| 600 | + I64(list(range(0, 21, 4)) + list(range(-2, 24, 4)))), |
| 601 | + (RI(0, -20, -2), RI(-1, -21, -2), RI(-19, 1, 1), |
| 602 | + I64(list(range(0, -20, -2)) + list(range(-1, -21, -2)))), |
| 603 | + (RI(0, 100, 5), RI(0, 100, 20), RI(0, 100, 5), 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), I64([0, 5, -5])), |
| 618 | + (RI(0, 3, 1), RI(4, 5, 1), I64([0, 1, 2, 4]), I64([0, 1, 2, 4])), |
| 619 | + (RI(0, 10, 1), I64([]), RI(0, 10, 1), RI(0, 10, 1)), |
| 620 | + (RI(0), I64([1, 5, 6]), I64([1, 5, 6]), I64([1, 5, 6])) |
| 621 | + ]) |
| 622 | + def unions(self, request): |
| 623 | + """Inputs and expected outputs for RangeIndex.union tests""" |
| 624 | + |
| 625 | + return request.param |
| 626 | + |
| 627 | + def test_union_sorted(self, unions): |
| 628 | + |
| 629 | + idx1, idx2, expected_sorted, expected_notsorted = unions |
| 630 | + |
| 631 | + res1 = idx1.union(idx2, sort=None) |
| 632 | + tm.assert_index_equal(res1, expected_sorted, exact=True) |
| 633 | + |
| 634 | + res1 = idx1.union(idx2, sort=False) |
| 635 | + tm.assert_index_equal(res1, expected_notsorted, exact=True) |
| 636 | + |
| 637 | + res2 = idx2.union(idx1, sort=None) |
| 638 | + res3 = idx1._int64index.union(idx2, sort=None) |
| 639 | + tm.assert_index_equal(res2, expected_sorted, exact=True) |
| 640 | + tm.assert_index_equal(res3, expected_sorted) |
613 | 641 |
|
614 | 642 | def test_nbytes(self):
|
615 | 643 |
|
@@ -840,38 +868,41 @@ def test_len_specialised(self):
|
840 | 868 | i = RangeIndex(0, 5, step)
|
841 | 869 | assert len(i) == 0
|
842 | 870 |
|
843 |
| - def test_append(self): |
| 871 | + @pytest.fixture(params=[ |
| 872 | + ([RI(1, 12, 5)], RI(1, 12, 5)), |
| 873 | + ([RI(0, 6, 4)], RI(0, 6, 4)), |
| 874 | + ([RI(1, 3), RI(3, 7)], RI(1, 7)), |
| 875 | + ([RI(1, 5, 2), RI(5, 6)], RI(1, 6, 2)), |
| 876 | + ([RI(1, 3, 2), RI(4, 7, 3)], RI(1, 7, 3)), |
| 877 | + ([RI(-4, 3, 2), RI(4, 7, 2)], RI(-4, 7, 2)), |
| 878 | + ([RI(-4, -8), RI(-8, -12)], RI(0, 0)), |
| 879 | + ([RI(-4, -8), RI(3, -4)], RI(0, 0)), |
| 880 | + ([RI(-4, -8), RI(3, 5)], RI(3, 5)), |
| 881 | + ([RI(-4, -2), RI(3, 5)], I64([-4, -3, 3, 4])), |
| 882 | + ([RI(-2,), RI(3, 5)], RI(3, 5)), |
| 883 | + ([RI(2,), RI(2)], I64([0, 1, 0, 1])), |
| 884 | + ([RI(2,), RI(2, 5), RI(5, 8, 4)], RI(0, 6)), |
| 885 | + ([RI(2,), RI(3, 5), RI(5, 8, 4)], I64([0, 1, 3, 4, 5])), |
| 886 | + ([RI(-2, 2), RI(2, 5), RI(5, 8, 4)], RI(-2, 6)), |
| 887 | + ([RI(3,), I64([-1, 3, 15])], I64([0, 1, 2, -1, 3, 15])), |
| 888 | + ([RI(3,), F64([-1, 3.1, 15.])], F64([0, 1, 2, -1, 3.1, 15.])), |
| 889 | + ([RI(3,), OI(['a', None, 14])], OI([0, 1, 2, 'a', None, 14])), |
| 890 | + ([RI(3, 1), OI(['a', None, 14])], OI(['a', None, 14])) |
| 891 | + ]) |
| 892 | + def appends(self, request): |
| 893 | + """Inputs and expected outputs for RangeIndex.append test""" |
| 894 | + |
| 895 | + return request.param |
| 896 | + |
| 897 | + def test_append(self, appends): |
844 | 898 | # GH16212
|
845 |
| - RI = RangeIndex |
846 |
| - I64 = Int64Index |
847 |
| - F64 = Float64Index |
848 |
| - OI = Index |
849 |
| - cases = [([RI(1, 12, 5)], RI(1, 12, 5)), |
850 |
| - ([RI(0, 6, 4)], RI(0, 6, 4)), |
851 |
| - ([RI(1, 3), RI(3, 7)], RI(1, 7)), |
852 |
| - ([RI(1, 5, 2), RI(5, 6)], RI(1, 6, 2)), |
853 |
| - ([RI(1, 3, 2), RI(4, 7, 3)], RI(1, 7, 3)), |
854 |
| - ([RI(-4, 3, 2), RI(4, 7, 2)], RI(-4, 7, 2)), |
855 |
| - ([RI(-4, -8), RI(-8, -12)], RI(0, 0)), |
856 |
| - ([RI(-4, -8), RI(3, -4)], RI(0, 0)), |
857 |
| - ([RI(-4, -8), RI(3, 5)], RI(3, 5)), |
858 |
| - ([RI(-4, -2), RI(3, 5)], I64([-4, -3, 3, 4])), |
859 |
| - ([RI(-2,), RI(3, 5)], RI(3, 5)), |
860 |
| - ([RI(2,), RI(2)], I64([0, 1, 0, 1])), |
861 |
| - ([RI(2,), RI(2, 5), RI(5, 8, 4)], RI(0, 6)), |
862 |
| - ([RI(2,), RI(3, 5), RI(5, 8, 4)], I64([0, 1, 3, 4, 5])), |
863 |
| - ([RI(-2, 2), RI(2, 5), RI(5, 8, 4)], RI(-2, 6)), |
864 |
| - ([RI(3,), I64([-1, 3, 15])], I64([0, 1, 2, -1, 3, 15])), |
865 |
| - ([RI(3,), F64([-1, 3.1, 15.])], F64([0, 1, 2, -1, 3.1, 15.])), |
866 |
| - ([RI(3,), OI(['a', None, 14])], OI([0, 1, 2, 'a', None, 14])), |
867 |
| - ([RI(3, 1), OI(['a', None, 14])], OI(['a', None, 14])) |
868 |
| - ] |
869 |
| - |
870 |
| - for indices, expected in cases: |
871 |
| - result = indices[0].append(indices[1:]) |
872 |
| - tm.assert_index_equal(result, expected, exact=True) |
873 |
| - |
874 |
| - if len(indices) == 2: |
875 |
| - # Append single item rather than list |
876 |
| - result2 = indices[0].append(indices[1]) |
877 |
| - tm.assert_index_equal(result2, expected, exact=True) |
| 899 | + |
| 900 | + indices, expected = appends |
| 901 | + |
| 902 | + result = indices[0].append(indices[1:]) |
| 903 | + tm.assert_index_equal(result, expected, exact=True) |
| 904 | + |
| 905 | + if len(indices) == 2: |
| 906 | + # Append single item rather than list |
| 907 | + result2 = indices[0].append(indices[1]) |
| 908 | + tm.assert_index_equal(result2, expected, exact=True) |
0 commit comments