Skip to content

Commit 823e83d

Browse files
BUG: fix empty intersection of RangeIndex (GH14364)
1 parent 7cad3f1 commit 823e83d

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

doc/source/whatsnew/v0.19.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Bug Fixes
3838

3939

4040

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

4343

4444

pandas/indexes/range.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,17 @@ def intersection(self, other):
315315
if not isinstance(other, RangeIndex):
316316
return super(RangeIndex, self).intersection(other)
317317

318+
if not len(self) or not len(other):
319+
return RangeIndex._simple_new(None)
320+
318321
# check whether intervals intersect
319322
# deals with in- and decreasing ranges
320323
int_low = max(min(self._start, self._stop + 1),
321324
min(other._start, other._stop + 1))
322325
int_high = min(max(self._stop, self._start + 1),
323326
max(other._stop, other._start + 1))
324327
if int_high <= int_low:
325-
return RangeIndex()
328+
return RangeIndex._simple_new(None)
326329

327330
# Method hint: linear Diophantine equation
328331
# solve intersection problem
@@ -332,7 +335,7 @@ def intersection(self, other):
332335

333336
# check whether element sets intersect
334337
if (self._start - other._start) % gcd:
335-
return RangeIndex()
338+
return RangeIndex._simple_new(None)
336339

337340
# calculate parameters for the RangeIndex describing the
338341
# intersection disregarding the lower bounds

pandas/tests/indexes/test_range.py

+32
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,38 @@ def test_intersection(self):
587587
other.values)))
588588
self.assert_index_equal(result, expected)
589589

590+
index = RangeIndex(5)
591+
592+
# intersect of non-overlapping indices
593+
other = RangeIndex(5, 10, 1)
594+
result = index.intersection(other)
595+
expected = RangeIndex(0, 0, 1)
596+
self.assert_index_equal(result, expected)
597+
598+
other = RangeIndex(-1, -5, -1)
599+
result = index.intersection(other)
600+
expected = RangeIndex(0, 0, 1)
601+
self.assert_index_equal(result, expected)
602+
603+
# intersection of empty indices
604+
other = RangeIndex(0, 0, 1)
605+
result = index.intersection(other)
606+
expected = RangeIndex(0, 0, 1)
607+
self.assert_index_equal(result, expected)
608+
609+
result = other.intersection(index)
610+
self.assert_index_equal(result, expected)
611+
612+
result = other.intersection(other)
613+
self.assert_index_equal(result, expected)
614+
615+
# intersection of non-overlapping values based on start value and gcd
616+
index = RangeIndex(1, 10, 2)
617+
other = RangeIndex(0, 10, 4)
618+
result = index.intersection(other)
619+
expected = RangeIndex(0, 0, 1)
620+
self.assert_index_equal(result, expected)
621+
590622
def test_intersect_str_dates(self):
591623
dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)]
592624

0 commit comments

Comments
 (0)