Skip to content

Commit fe2ebc1

Browse files
jorisvandenbosschejreback
authored andcommitted
BUG: fix empty intersection of RangeIndex (GH14364)
closes pandas-dev#14364 Author: Joris Van den Bossche <[email protected]> Closes pandas-dev#14481 from jorisvandenbossche/bug-rangeindex-empty and squashes the following commits: 823e83d [Joris Van den Bossche] BUG: fix empty intersection of RangeIndex (GH14364)
1 parent 1308884 commit fe2ebc1

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

doc/source/whatsnew/v0.19.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Bug Fixes
4343
- Bug in string indexing against data with ``object`` ``Index`` may raise ``AttributeError`` (:issue:`14424`)
4444
- Corrrecly raise ``ValueError`` on empty input to ``pd.eval()`` and ``df.query()`` (:issue:`13139`)
4545

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

4848
- ``pd.merge()`` will raise ``ValueError`` with non-boolean parameters in passed boolean type arguments (:issue:`14434`)
4949

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

+29
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,35 @@ 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+
# intersection of non-overlapping values based on start value and gcd
613+
index = RangeIndex(1, 10, 2)
614+
other = RangeIndex(0, 10, 4)
615+
result = index.intersection(other)
616+
expected = RangeIndex(0, 0, 1)
617+
self.assert_index_equal(result, expected)
618+
590619
def test_intersect_str_dates(self):
591620
dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)]
592621

0 commit comments

Comments
 (0)