Skip to content

Commit 3493aba

Browse files
SmokinCaterpillarjreback
authored andcommitted
_concat_rangeindex_... now returns an empty RangeIndex for empty ranges (#18214)
1 parent b37e4f5 commit 3493aba

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

pandas/core/dtypes/concat.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ def _concat_rangeindex_same_dtype(indexes):
571571
indexes = [RangeIndex(3), RangeIndex(3, 6)] -> RangeIndex(6)
572572
indexes = [RangeIndex(3), RangeIndex(4, 6)] -> Int64Index([0,1,2,4,5])
573573
"""
574+
from pandas import Int64Index, RangeIndex
574575

575576
start = step = next = None
576577

@@ -587,14 +588,12 @@ def _concat_rangeindex_same_dtype(indexes):
587588
elif step is None:
588589
# First non-empty index had only one element
589590
if obj._start == start:
590-
from pandas import Int64Index
591591
return _concat_index_same_dtype(indexes, klass=Int64Index)
592592
step = obj._start - start
593593

594594
non_consecutive = ((step != obj._step and len(obj) > 1) or
595595
(next is not None and obj._start != next))
596596
if non_consecutive:
597-
from pandas import Int64Index
598597
return _concat_index_same_dtype(indexes, klass=Int64Index)
599598

600599
if step is not None:
@@ -604,12 +603,8 @@ def _concat_rangeindex_same_dtype(indexes):
604603
# Get the stop value from "next" or alternatively
605604
# from the last non-empty index
606605
stop = non_empty_indexes[-1]._stop if next is None else next
607-
else:
608-
# Here all "indexes" had 0 length, i.e. were empty.
609-
# Simply take start, stop, and step from the last empty index.
610-
obj = indexes[-1]
611-
start = obj._start
612-
step = obj._step
613-
stop = obj._stop
614-
615-
return indexes[0].__class__(start, stop, step)
606+
return RangeIndex(start, stop, step)
607+
608+
# Here all "indexes" had 0 length, i.e. were empty.
609+
# In this case return an empty range index.
610+
return RangeIndex(0, 0)

pandas/tests/indexes/test_range.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -971,8 +971,8 @@ def test_append(self):
971971
([RI(1, 5, 2), RI(5, 6)], RI(1, 6, 2)),
972972
([RI(1, 3, 2), RI(4, 7, 3)], RI(1, 7, 3)),
973973
([RI(-4, 3, 2), RI(4, 7, 2)], RI(-4, 7, 2)),
974-
([RI(-4, -8), RI(-8, -12)], RI(-8, -12)),
975-
([RI(-4, -8), RI(3, -4)], RI(3, -8)),
974+
([RI(-4, -8), RI(-8, -12)], RI(0, 0)),
975+
([RI(-4, -8), RI(3, -4)], RI(0, 0)),
976976
([RI(-4, -8), RI(3, 5)], RI(3, 5)),
977977
([RI(-4, -2), RI(3, 5)], I64([-4, -3, 3, 4])),
978978
([RI(-2,), RI(3, 5)], RI(3, 5)),

0 commit comments

Comments
 (0)