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 1 commit
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
13 changes: 4 additions & 9 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
return RangeIndex(start, stop, step)
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe remove the else here (and just return the RangeIndex)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I personally like the else in such situations, it's more explicit to me, I guess it's a matter of taste. I'll remove it :-)

# 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)
# In this case return an empty integer index.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this should be RangeIndex(0, 1) to be consistent with our default indices, e.g. Series([]).index

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RangeIndex(0, 1) or rather RangeIndex(0, 0)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is changed to empty range

return Int64Index([])
5 changes: 3 additions & 2 deletions pandas/tests/indexes/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,8 +971,9 @@ 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)),
# Concat empty ranges should return empty integer index:
([RI(-4, -8), RI(-8, -12)], I64([])),
([RI(-4, -8), RI(3, -4)], I64([])),
([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