Skip to content

_concat_rangeindex_... now returns an empty RangeIndex for empty ranges #18214

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

Merged
merged 4 commits into from
Nov 11, 2017
Merged
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
17 changes: 6 additions & 11 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ def _concat_rangeindex_same_dtype(indexes):
indexes = [RangeIndex(3), RangeIndex(3, 6)] -> RangeIndex(6)
indexes = [RangeIndex(3), RangeIndex(4, 6)] -> Int64Index([0,1,2,4,5])
"""
from pandas import Int64Index, RangeIndex

start = step = next = None

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

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

if step is not None:
Expand All @@ -604,12 +603,8 @@ def _concat_rangeindex_same_dtype(indexes):
# Get the stop value from "next" or alternatively
# from the last non-empty index
stop = non_empty_indexes[-1]._stop if next is None else next
else:
# Here all "indexes" had 0 length, i.e. were empty.
# Simply take start, stop, and step from the last empty index.
obj = indexes[-1]
start = obj._start
step = obj._step
stop = obj._stop

return indexes[0].__class__(start, stop, step)
return RangeIndex(start, stop, step)

# Here all "indexes" had 0 length, i.e. were empty.
# In this case return an empty range index.
return RangeIndex(0, 0)
4 changes: 2 additions & 2 deletions pandas/tests/indexes/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,8 +971,8 @@ def test_append(self):
([RI(1, 5, 2), RI(5, 6)], RI(1, 6, 2)),
([RI(1, 3, 2), RI(4, 7, 3)], RI(1, 7, 3)),
([RI(-4, 3, 2), RI(4, 7, 2)], RI(-4, 7, 2)),
([RI(-4, -8), RI(-8, -12)], RI(-8, -12)),
([RI(-4, -8), RI(3, -4)], RI(3, -8)),
([RI(-4, -8), RI(-8, -12)], RI(0, 0)),
([RI(-4, -8), RI(3, -4)], RI(0, 0)),
([RI(-4, -8), RI(3, 5)], RI(3, 5)),
([RI(-4, -2), RI(3, 5)], I64([-4, -3, 3, 4])),
([RI(-2,), RI(3, 5)], RI(3, 5)),
Expand Down