Skip to content

Commit 1ca094f

Browse files
toobazjreback
authored andcommitted
BUG: Refine validation of parameters to RangeIndex.__init__
closes pandas-dev#12288 Author: Pietro Battiston <[email protected]> Closes pandas-dev#12295 from toobaz/rangeindexparams and squashes the following commits: ee92354 [Pietro Battiston] BUG: Refine validation of parameters to RangeIndex.__init__
1 parent 3e4c572 commit 1ca094f

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

doc/source/whatsnew/v0.18.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Range Index
134134

135135
A ``RangeIndex`` has been added to the ``Int64Index`` sub-classes to support a memory saving alternative for common use cases. This has a similar implementation to the python ``range`` object (``xrange`` in python 2), in that it only stores the start, stop, and step values for the index. It will transparently interact with the user API, converting to ``Int64Index`` if needed.
136136

137-
This will now be the default constructed index for ``NDFrame`` objects, rather than previous an ``Int64Index``. (:issue:`939`, :issue:`12070`, :issue:`12071`, :issue:`12109`)
137+
This will now be the default constructed index for ``NDFrame`` objects, rather than previous an ``Int64Index``. (:issue:`939`, :issue:`12070`, :issue:`12071`, :issue:`12109`, :issue:`12888`)
138138

139139
Previous Behavior:
140140

pandas/indexes/range.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ class RangeIndex(Int64Index):
2323
2424
Parameters
2525
----------
26-
start : int (default: 0)
26+
start : int (default: 0), or other RangeIndex instance.
27+
If int and "stop" is not given, interpreted as "stop" instead.
2728
stop : int (default: 0)
2829
step : int (default: 1)
2930
name : object, optional
3031
Name to be stored in the index
3132
copy : bool, default False
32-
Make a copy of input if its a RangeIndex
33+
Unused, accepted for homogeneity with other index types.
3334
3435
"""
3536

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

4748
# RangeIndex
4849
if isinstance(start, RangeIndex):
49-
if not copy:
50-
return start
5150
if name is None:
52-
name = getattr(start, 'name', None)
53-
start, stop, step = start._start, start._stop, start._step
51+
name = start.name
52+
return cls._simple_new(name=name,
53+
**dict(start._get_data_as_items()))
5454

5555
# validate the arguments
5656
def _ensure_int(value, field):
5757
try:
5858
new_value = int(value)
59-
except:
60-
new_value = value
61-
62-
if not com.is_integer(new_value) or new_value != value:
59+
assert(new_value == value)
60+
except (ValueError, AssertionError):
6361
raise TypeError("RangeIndex(...) must be called with integers,"
6462
" {value} was passed for {field}".format(
6563
value=type(value).__name__,

pandas/tests/indexes/test_range.py

+19
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,25 @@ def test_constructor_range(self):
138138
self.assertRaises(TypeError,
139139
lambda: Index(range(1, 5, 2), dtype='float64'))
140140

141+
def test_constructor_name(self):
142+
# GH12288
143+
orig = RangeIndex(10)
144+
orig.name = 'original'
145+
146+
copy = RangeIndex(orig)
147+
copy.name = 'copy'
148+
149+
self.assertTrue(orig.name, 'original')
150+
self.assertTrue(copy.name, 'copy')
151+
152+
new = Index(copy)
153+
self.assertTrue(new.name, 'copy')
154+
155+
new.name = 'new'
156+
self.assertTrue(orig.name, 'original')
157+
self.assertTrue(new.name, 'copy')
158+
self.assertTrue(new.name, 'new')
159+
141160
def test_numeric_compat2(self):
142161
# validate that we are handling the RangeIndex overrides to numeric ops
143162
# and returning RangeIndex where possible

0 commit comments

Comments
 (0)