File tree Expand file tree Collapse file tree 4 files changed +9
-11
lines changed
Expand file tree Collapse file tree 4 files changed +9
-11
lines changed Original file line number Diff line number Diff line change @@ -140,6 +140,7 @@ Other enhancements
140140- :meth: `pandas.read_stata ` and :class: `StataReader ` support reading data from compressed files.
141141- Add support for parsing ``ISO 8601 ``-like timestamps with negative signs to :meth: `pandas.Timedelta ` (:issue: `37172 `)
142142- 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 `)
143144- :meth: `round ` being enabled for the nullable integer and floating dtypes (:issue: `38844 `)
144145
145146.. ---------------------------------------------------------------------------
Original file line number Diff line number Diff line change @@ -366,16 +366,11 @@ def __new__(
366366 data_dtype = getattr (data , "dtype" , None )
367367
368368 # range
369- if isinstance (data , RangeIndex ):
369+ if isinstance (data , ( range , RangeIndex ) ):
370370 result = RangeIndex (start = data , copy = copy , name = name )
371371 if dtype is not None :
372372 return result .astype (dtype , copy = False )
373373 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
379374
380375 elif is_ea_or_datetimelike_dtype (dtype ):
381376 # 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__(
116116
117117 # RangeIndex
118118 if isinstance (start , RangeIndex ):
119- start = start ._range
119+ return start .copy (name = name )
120+ elif isinstance (start , range ):
120121 return cls ._simple_new (start , name = name )
121122
122123 # validate the arguments
Original file line number Diff line number Diff line change @@ -91,11 +91,12 @@ def test_constructor_same(self):
9191 ):
9292 RangeIndex (index , dtype = "float64" )
9393
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 )
9598
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 ):
99100
100101 result = RangeIndex .from_range (range (1 , 5 , 2 ))
101102 expected = RangeIndex (1 , 5 , 2 )
You can’t perform that action at this time.
0 commit comments