def test_difference_interior_non_preserving(self): # case with intersection of length 1 but RangeIndex is not preserved idx = Index(range(10)) other = idx[3:4] result = idx.difference(other) expected = Int64Index([0, 1, 2, 4, 5, 6, 7, 8, 9]) tm.assert_index_equal(result, expected, exact=True) # case with other.step / self.step > 2 other = idx[::3] result = idx.difference(other) expected = Int64Index([1, 2, 4, 5, 7, 8]) tm.assert_index_equal(result, expected, exact=True) # cases with only reaching one end of left obj = Index(range(20)) other = obj[:10:2] result = obj.difference(other) expected = Int64Index([1, 3, 5, 7, 9] + list(range(10, 20))) tm.assert_index_equal(result, expected, exact=True) other = obj[1:11:2] result = obj.difference(other) expected = Int64Index([0, 2, 4, 6, 8, 10] + list(range(11, 20))) tm.assert_index_equal(result, expected, exact=True)
def test_float64_index_difference(self): # https://github.com/pandas-dev/pandas/issues/35217 float_index = Index([1.0, 2, 3]) string_index = Index(["1", "2", "3"]) result = float_index.difference(string_index) tm.assert_index_equal(result, float_index) result = string_index.difference(float_index) tm.assert_index_equal(result, string_index)
def test_difference_sort(self): # GH#44085 ensure we respect the sort keyword idx = Index(range(4))[::-1] other = Index(range(3, 4)) result = idx.difference(other) expected = Index(range(3)) tm.assert_index_equal(result, expected, exact=True) result = idx.difference(other, sort=False) expected = expected[::-1] tm.assert_index_equal(result, expected, exact=True) # case where the intersection is empty other = range(10, 12) result = idx.difference(other, sort=None) expected = idx[::-1] tm.assert_index_equal(result, expected, exact=True)