Skip to content

Commit de6b744

Browse files
GH828 Replace Iterable with Axes for Index.__new__/Index.__init__ (#1202)
* GH828 Replace Iterable with Axes for Index.__new__/Index.__init__ * GH828 Add type_invalid test * GH828 Add dict * GH828 Clean up overlapping types
1 parent 9c02c36 commit de6b744

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

pandas-stubs/core/indexes/base.pyi

+10-9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ from typing_extensions import (
4242
from pandas._libs.interval import _OrderableT
4343
from pandas._typing import (
4444
S1,
45+
Axes,
4546
Dtype,
4647
DtypeArg,
4748
DtypeObj,
@@ -81,7 +82,7 @@ class Index(IndexOpsMixin[S1]):
8182
@overload
8283
def __new__(
8384
cls,
84-
data: Iterable,
85+
data: Axes,
8586
*,
8687
dtype: Literal["int"] | type_t[int | np.integer],
8788
copy: bool = ...,
@@ -103,7 +104,7 @@ class Index(IndexOpsMixin[S1]):
103104
@overload
104105
def __new__(
105106
cls,
106-
data: Iterable,
107+
data: Axes,
107108
*,
108109
dtype: Literal["float"] | type_t[float | np.floating],
109110
copy: bool = ...,
@@ -129,7 +130,7 @@ class Index(IndexOpsMixin[S1]):
129130
@overload
130131
def __new__(
131132
cls,
132-
data: Iterable,
133+
data: Axes,
133134
*,
134135
dtype: Literal["complex"] | type_t[complex | np.complexfloating],
135136
copy: bool = ...,
@@ -152,7 +153,7 @@ class Index(IndexOpsMixin[S1]):
152153
@overload
153154
def __new__(
154155
cls,
155-
data: Iterable,
156+
data: Axes,
156157
*,
157158
dtype: TimestampDtypeArg,
158159
copy: bool = ...,
@@ -174,7 +175,7 @@ class Index(IndexOpsMixin[S1]):
174175
@overload
175176
def __new__(
176177
cls,
177-
data: Iterable,
178+
data: Axes,
178179
*,
179180
dtype: PeriodDtype,
180181
copy: bool = ...,
@@ -196,7 +197,7 @@ class Index(IndexOpsMixin[S1]):
196197
@overload
197198
def __new__(
198199
cls,
199-
data: Iterable,
200+
data: Axes,
200201
*,
201202
dtype: TimedeltaDtypeArg,
202203
copy: bool = ...,
@@ -218,7 +219,7 @@ class Index(IndexOpsMixin[S1]):
218219
@overload
219220
def __new__(
220221
cls,
221-
data: Iterable,
222+
data: Axes,
222223
*,
223224
dtype: Literal["Interval"],
224225
copy: bool = ...,
@@ -241,7 +242,7 @@ class Index(IndexOpsMixin[S1]):
241242
@overload
242243
def __new__(
243244
cls,
244-
data: Iterable = ...,
245+
data: Axes = ...,
245246
*,
246247
dtype: type[S1],
247248
copy: bool = ...,
@@ -253,7 +254,7 @@ class Index(IndexOpsMixin[S1]):
253254
@overload
254255
def __new__(
255256
cls,
256-
data: Iterable,
257+
data: Axes,
257258
*,
258259
dtype=...,
259260
copy: bool = ...,

pandas-stubs/core/indexes/datetimes.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ from datetime import (
1010
from typing import overload
1111

1212
from _typing import (
13+
Axes,
1314
Frequency,
1415
TimeZones,
1516
)
@@ -30,7 +31,6 @@ from pandas.core.series import (
3031
from typing_extensions import Self
3132

3233
from pandas._typing import (
33-
AnyArrayLike,
3434
DateAndDatetimeLike,
3535
Dtype,
3636
IntervalClosedType,
@@ -44,7 +44,7 @@ from pandas.tseries.offsets import BaseOffset
4444
class DatetimeIndex(DatetimeTimedeltaMixin[Timestamp], DatetimeIndexProperties):
4545
def __init__(
4646
self,
47-
data: AnyArrayLike | list | tuple,
47+
data: Axes,
4848
freq: Frequency = ...,
4949
tz: TimeZones = ...,
5050
ambiguous: str = ...,

pandas-stubs/core/indexes/timedeltas.pyi

+2-6
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,15 @@ from pandas._libs import (
2727
)
2828
from pandas._libs.tslibs import BaseOffset
2929
from pandas._typing import (
30-
AnyArrayLike,
30+
Axes,
3131
TimedeltaConvertibleTypes,
3232
num,
3333
)
3434

3535
class TimedeltaIndex(DatetimeTimedeltaMixin[Timedelta], TimedeltaIndexProperties):
3636
def __new__(
3737
cls,
38-
data: (
39-
AnyArrayLike
40-
| list[str]
41-
| Sequence[dt.timedelta | Timedelta | np.timedelta64 | float]
42-
) = ...,
38+
data: Sequence[dt.timedelta | Timedelta | np.timedelta64 | float] | Axes = ...,
4339
freq: str | BaseOffset = ...,
4440
closed: object = ...,
4541
dtype: Literal["<m8[ns]"] = ...,

tests/test_indexes.py

+22
Original file line numberDiff line numberDiff line change
@@ -1321,3 +1321,25 @@ def test_index_delete() -> None:
13211321

13221322
dt_ind = pd.date_range("2023-01-01", "2023-02-01")
13231323
check(assert_type(dt_ind.delete(2), pd.DatetimeIndex), pd.DatetimeIndex)
1324+
1325+
1326+
def test_index_dict() -> None:
1327+
"""Test passing an ordered iterables to Index and subclasses constructor GH828."""
1328+
check(
1329+
assert_type(pd.Index({"Jan. 1, 2008": "New Year’s Day"}), "pd.Index[str]"),
1330+
pd.Index,
1331+
str,
1332+
)
1333+
check(
1334+
assert_type(
1335+
pd.DatetimeIndex({"Jan. 1, 2008": "New Year’s Day"}), pd.DatetimeIndex
1336+
),
1337+
pd.DatetimeIndex,
1338+
)
1339+
check(
1340+
assert_type(
1341+
pd.TimedeltaIndex({pd.Timedelta(days=1): "New Year’s Day"}),
1342+
pd.TimedeltaIndex,
1343+
),
1344+
pd.TimedeltaIndex,
1345+
)

0 commit comments

Comments
 (0)