From ef50ef82536c0239d4be14a1981af73cc3ae92b0 Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Sat, 21 Sep 2024 21:54:06 -0400 Subject: [PATCH 1/2] GH677 Allow pd.RangeIndex to be initialized with range --- pandas-stubs/core/indexes/range.pyi | 4 ++-- tests/test_indexes.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pandas-stubs/core/indexes/range.pyi b/pandas-stubs/core/indexes/range.pyi index 16b99ade4..e3f328d46 100644 --- a/pandas-stubs/core/indexes/range.pyi +++ b/pandas-stubs/core/indexes/range.pyi @@ -15,7 +15,7 @@ from pandas._typing import ( class RangeIndex(Index[int]): def __new__( cls, - start: int | RangeIndex = ..., + start: int | RangeIndex | range = ..., stop: int = ..., step: int = ..., dtype=..., @@ -24,7 +24,7 @@ class RangeIndex(Index[int]): ): ... def __init__( self, - start: int | RangeIndex = ..., + start: int | RangeIndex | range = ..., stop: int = ..., step: int = ..., dtype=..., diff --git a/tests/test_indexes.py b/tests/test_indexes.py index d151ba481..46ec843af 100644 --- a/tests/test_indexes.py +++ b/tests/test_indexes.py @@ -912,6 +912,12 @@ def test_getitem() -> None: check(assert_type(i0[[0, 2]], "pd.Index[str]"), pd.Index, str) +def test_range_index_range() -> None: + """Test that pd.RangeIndex can be initialized from range.""" + iri = pd.RangeIndex(range(5)) + check(assert_type(iri, pd.RangeIndex), pd.RangeIndex, int) + + def test_multiindex_dtypes(): # GH-597 mi = pd.MultiIndex.from_tuples([(1, 2.0), (2, 3.0)], names=["foo", "bar"]) From c7ab9769a47839e58ad775d495ad825259dc742f Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Sun, 22 Sep 2024 12:49:10 -0400 Subject: [PATCH 2/2] GH677 Remove pd.RangeIndex.__init__ to align with pandas code --- pandas-stubs/core/indexes/range.pyi | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pandas-stubs/core/indexes/range.pyi b/pandas-stubs/core/indexes/range.pyi index e3f328d46..e2b3bf7fa 100644 --- a/pandas-stubs/core/indexes/range.pyi +++ b/pandas-stubs/core/indexes/range.pyi @@ -22,15 +22,6 @@ class RangeIndex(Index[int]): copy: bool = ..., name=..., ): ... - def __init__( - self, - start: int | RangeIndex | range = ..., - stop: int = ..., - step: int = ..., - dtype=..., - copy: bool = ..., - name=..., - ) -> None: ... @classmethod def from_range(cls, data, name=..., dtype=...): ... def __reduce__(self): ...