Skip to content

ENH: allow start=range_object in RangeIndex constructor #40241

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

Merged
merged 5 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

.. ---------------------------------------------------------------------------

Expand Down
7 changes: 1 addition & 6 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 5 additions & 4 deletions pandas/tests/indexes/ranges/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down