File tree 4 files changed +9
-11
lines changed
4 files changed +9
-11
lines changed Original file line number Diff line number Diff line change @@ -140,6 +140,7 @@ Other enhancements
140
140
- :meth: `pandas.read_stata ` and :class: `StataReader ` support reading data from compressed files.
141
141
- Add support for parsing ``ISO 8601 ``-like timestamps with negative signs to :meth: `pandas.Timedelta ` (:issue: `37172 `)
142
142
- Add support for unary operators in :class: `FloatingArray ` (:issue: `38749 `)
143
+ - :class: `RangeIndex ` can now be constructed by passing a ``range `` object directly e.g. ``pd.RangeIndex(range(3)) `` (:issue: `12067 `)
143
144
- :meth: `round ` being enabled for the nullable integer and floating dtypes (:issue: `38844 `)
144
145
145
146
.. ---------------------------------------------------------------------------
Original file line number Diff line number Diff line change @@ -366,16 +366,11 @@ def __new__(
366
366
data_dtype = getattr (data , "dtype" , None )
367
367
368
368
# range
369
- if isinstance (data , RangeIndex ):
369
+ if isinstance (data , ( range , RangeIndex ) ):
370
370
result = RangeIndex (start = data , copy = copy , name = name )
371
371
if dtype is not None :
372
372
return result .astype (dtype , copy = False )
373
373
return result
374
- elif isinstance (data , range ):
375
- result = RangeIndex .from_range (data , name = name )
376
- if dtype is not None :
377
- return result .astype (dtype , copy = False )
378
- return result
379
374
380
375
elif is_ea_or_datetimelike_dtype (dtype ):
381
376
# non-EA dtype indexes have special casting logic, so we punt here
Original file line number Diff line number Diff line change @@ -116,7 +116,8 @@ def __new__(
116
116
117
117
# RangeIndex
118
118
if isinstance (start , RangeIndex ):
119
- start = start ._range
119
+ return start .copy (name = name )
120
+ elif isinstance (start , range ):
120
121
return cls ._simple_new (start , name = name )
121
122
122
123
# validate the arguments
Original file line number Diff line number Diff line change @@ -91,11 +91,12 @@ def test_constructor_same(self):
91
91
):
92
92
RangeIndex (index , dtype = "float64" )
93
93
94
- def test_constructor_range (self ):
94
+ def test_constructor_range_object (self ):
95
+ result = RangeIndex (range (1 , 5 , 2 ))
96
+ expected = RangeIndex (1 , 5 , 2 )
97
+ tm .assert_index_equal (result , expected , exact = True )
95
98
96
- msg = "Value needs to be a scalar value, was type range"
97
- with pytest .raises (TypeError , match = msg ):
98
- result = RangeIndex (range (1 , 5 , 2 ))
99
+ def test_constructor_range (self ):
99
100
100
101
result = RangeIndex .from_range (range (1 , 5 , 2 ))
101
102
expected = RangeIndex (1 , 5 , 2 )
You can’t perform that action at this time.
0 commit comments