diff --git a/doc/source/whatsnew/v0.19.1.txt b/doc/source/whatsnew/v0.19.1.txt index 5180b9a092f6c..b545519996c80 100644 --- a/doc/source/whatsnew/v0.19.1.txt +++ b/doc/source/whatsnew/v0.19.1.txt @@ -38,7 +38,7 @@ Bug Fixes - +- Bug in ``RangeIndex.intersection`` when result is a empty set (:issue:`14364`). diff --git a/pandas/indexes/range.py b/pandas/indexes/range.py index 76166e7155bd0..7a7902b503bd6 100644 --- a/pandas/indexes/range.py +++ b/pandas/indexes/range.py @@ -315,6 +315,9 @@ def intersection(self, other): if not isinstance(other, RangeIndex): return super(RangeIndex, self).intersection(other) + if not len(self) or not len(other): + return RangeIndex._simple_new(None) + # check whether intervals intersect # deals with in- and decreasing ranges int_low = max(min(self._start, self._stop + 1), @@ -322,7 +325,7 @@ def intersection(self, other): int_high = min(max(self._stop, self._start + 1), max(other._stop, other._start + 1)) if int_high <= int_low: - return RangeIndex() + return RangeIndex._simple_new(None) # Method hint: linear Diophantine equation # solve intersection problem @@ -332,7 +335,7 @@ def intersection(self, other): # check whether element sets intersect if (self._start - other._start) % gcd: - return RangeIndex() + return RangeIndex._simple_new(None) # calculate parameters for the RangeIndex describing the # intersection disregarding the lower bounds diff --git a/pandas/tests/indexes/test_range.py b/pandas/tests/indexes/test_range.py index b0b8864521666..acbc06d76f824 100644 --- a/pandas/tests/indexes/test_range.py +++ b/pandas/tests/indexes/test_range.py @@ -587,6 +587,38 @@ def test_intersection(self): other.values))) self.assert_index_equal(result, expected) + index = RangeIndex(5) + + # intersect of non-overlapping indices + other = RangeIndex(5, 10, 1) + result = index.intersection(other) + expected = RangeIndex(0, 0, 1) + self.assert_index_equal(result, expected) + + other = RangeIndex(-1, -5, -1) + result = index.intersection(other) + expected = RangeIndex(0, 0, 1) + self.assert_index_equal(result, expected) + + # intersection of empty indices + other = RangeIndex(0, 0, 1) + result = index.intersection(other) + expected = RangeIndex(0, 0, 1) + self.assert_index_equal(result, expected) + + result = other.intersection(index) + self.assert_index_equal(result, expected) + + result = other.intersection(other) + self.assert_index_equal(result, expected) + + # intersection of non-overlapping values based on start value and gcd + index = RangeIndex(1, 10, 2) + other = RangeIndex(0, 10, 4) + result = index.intersection(other) + expected = RangeIndex(0, 0, 1) + self.assert_index_equal(result, expected) + def test_intersect_str_dates(self): dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)]