Skip to content

BUG: Refine validation of parameters to RangeIndex.__init__ #12295

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

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 8 additions & 10 deletions pandas/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ class RangeIndex(Int64Index):

Parameters
----------
start : int (default: 0)
start : int (default: 0), or other RangeIndex instance.
If int and "stop" is not given, interpreted as "stop" instead.
stop : int (default: 0)
step : int (default: 1)
name : object, optional
Name to be stored in the index
copy : bool, default False
Make a copy of input if its a RangeIndex
Unused, accepted for homogeneity with other index types.
Copy link
Contributor

Choose a reason for hiding this comment

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

no, this is not right


"""

Expand All @@ -46,20 +47,17 @@ def __new__(cls, start=None, stop=None, step=None, name=None, dtype=None,

# RangeIndex
if isinstance(start, RangeIndex):
if not copy:
return start
if name is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

the issue is that .view needs to be fixed instead. It should do a _shallow_copy rather than returning the same instance

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't follow you on .view. I follow you on _shallow_copy... which is equivalent to what I do, independently from copy, because there is nothing "deep" to copy.

name = getattr(start, 'name', None)
start, stop, step = start._start, start._stop, start._step
name = start.name
return cls._simple_new(name=name,
**dict(start._get_data_as_items()))

# validate the arguments
def _ensure_int(value, field):
try:
new_value = int(value)
except:
new_value = value

if not com.is_integer(new_value) or new_value != value:
assert(new_value == value)
except (ValueError, AssertionError):
raise TypeError("RangeIndex(...) must be called with integers,"
" {value} was passed for {field}".format(
value=type(value).__name__,
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/indexes/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ def test_constructor_range(self):
self.assertRaises(TypeError,
lambda: Index(range(1, 5, 2), dtype='float64'))

def test_constructor_name(self):
# GH12288
orig = RangeIndex(10)
orig.name = 'original'

copy = RangeIndex(orig)
copy.name = 'copy'

self.assertTrue(orig.name, 'original')

def test_numeric_compat2(self):
# validate that we are handling the RangeIndex overrides to numeric ops
# and returning RangeIndex where possible
Expand Down