From fcb410315b4e96a1486aa0efeafa935b9ca3c65d Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 4 Mar 2021 10:26:18 -0800 Subject: [PATCH 1/3] ENH: Allow start=range_object in RangeIndex.__new__ --- pandas/core/indexes/base.py | 7 +------ pandas/core/indexes/range.py | 2 ++ pandas/tests/indexes/ranges/test_constructors.py | 9 +++++---- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 7f5e7e3a32f14..8e2c120e360c1 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -366,16 +366,11 @@ def __new__( data_dtype = getattr(data, "dtype", None) # range - if isinstance(data, RangeIndex): + if isinstance(data, (range, RangeIndex)): result = RangeIndex(start=data, copy=copy, name=name) if dtype is not None: return result.astype(dtype, copy=False) return result - elif isinstance(data, range): - result = RangeIndex.from_range(data, name=name) - if dtype is not None: - return result.astype(dtype, copy=False) - return result elif is_ea_or_datetimelike_dtype(dtype): # non-EA dtype indexes have special casting logic, so we punt here diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 5c97361aa53fe..26ada13a783a4 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -118,6 +118,8 @@ def __new__( if isinstance(start, RangeIndex): start = start._range return cls._simple_new(start, name=name) + elif isinstance(start, range): + return cls._simple_new(start, name=name) # validate the arguments if com.all_none(start, stop, step): diff --git a/pandas/tests/indexes/ranges/test_constructors.py b/pandas/tests/indexes/ranges/test_constructors.py index 599df3732a33b..9539b0ff7cdba 100644 --- a/pandas/tests/indexes/ranges/test_constructors.py +++ b/pandas/tests/indexes/ranges/test_constructors.py @@ -91,11 +91,12 @@ def test_constructor_same(self): ): RangeIndex(index, dtype="float64") - def test_constructor_range(self): + def test_constructor_range_object(self): + result = RangeIndex(range(1, 5, 2)) + expected = RangeIndex(1, 5, 2) + tm.assert_index_equal(result, expected, exact=True) - msg = "Value needs to be a scalar value, was type range" - with pytest.raises(TypeError, match=msg): - result = RangeIndex(range(1, 5, 2)) + def test_constructor_range(self): result = RangeIndex.from_range(range(1, 5, 2)) expected = RangeIndex(1, 5, 2) From 31ecb65916be6b662dee3156c9a2bfc44a9be4b3 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 4 Mar 2021 19:33:50 -0800 Subject: [PATCH 2/3] whatsnew --- doc/source/whatsnew/v1.3.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 9a7639c0b6ae9..9453fb5563934 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -139,6 +139,7 @@ Other enhancements - :meth:`pandas.read_stata` and :class:`StataReader` support reading data from compressed files. - Add support for parsing ``ISO 8601``-like timestamps with negative signs to :meth:`pandas.Timedelta` (:issue:`37172`) - Add support for unary operators in :class:`FloatingArray` (:issue:`38749`) +- :class:`RangeIndex` can now be constructed by passing a ``range`` object directly e.g. ``pd.RangeIndex(range(3))`` (:issue:`12067`) .. --------------------------------------------------------------------------- From af37aa17a18d81c453384973a46d726313ddd3d0 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 6 Mar 2021 08:15:29 -0800 Subject: [PATCH 3/3] share cache --- pandas/core/indexes/range.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index c47855d380688..0c2d4a1872c98 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -116,8 +116,7 @@ def __new__( # RangeIndex if isinstance(start, RangeIndex): - start = start._range - return cls._simple_new(start, name=name) + return start.copy(name=name) elif isinstance(start, range): return cls._simple_new(start, name=name)