Skip to content

BUG: fix empty intersection of RangeIndex (GH14364) #14481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Bug Fixes




- Bug in ``RangeIndex.intersection`` when result is a empty set (:issue:`14364`).



Expand Down
7 changes: 5 additions & 2 deletions pandas/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,17 @@ 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),
min(other._start, other._stop + 1))
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
Expand All @@ -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
Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/indexes/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down