Skip to content

GH828 Replace Iterable with Axes for Index.__new__/Index.__init__ #1202

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 4 commits into from
Apr 27, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 10 additions & 9 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ from typing_extensions import (
from pandas._libs.interval import _OrderableT
from pandas._typing import (
S1,
Axes,
Dtype,
DtypeArg,
DtypeObj,
Expand Down Expand Up @@ -81,7 +82,7 @@ class Index(IndexOpsMixin[S1]):
@overload
def __new__(
cls,
data: Iterable,
data: Axes,
*,
dtype: Literal["int"] | type_t[int | np.integer],
copy: bool = ...,
Expand All @@ -103,7 +104,7 @@ class Index(IndexOpsMixin[S1]):
@overload
def __new__(
cls,
data: Iterable,
data: Axes,
*,
dtype: Literal["float"] | type_t[float | np.floating],
copy: bool = ...,
Expand All @@ -129,7 +130,7 @@ class Index(IndexOpsMixin[S1]):
@overload
def __new__(
cls,
data: Iterable,
data: Axes,
*,
dtype: Literal["complex"] | type_t[complex | np.complexfloating],
copy: bool = ...,
Expand All @@ -152,7 +153,7 @@ class Index(IndexOpsMixin[S1]):
@overload
def __new__(
cls,
data: Iterable,
data: Axes,
*,
dtype: TimestampDtypeArg,
copy: bool = ...,
Expand All @@ -174,7 +175,7 @@ class Index(IndexOpsMixin[S1]):
@overload
def __new__(
cls,
data: Iterable,
data: Axes,
*,
dtype: PeriodDtype,
copy: bool = ...,
Expand All @@ -196,7 +197,7 @@ class Index(IndexOpsMixin[S1]):
@overload
def __new__(
cls,
data: Iterable,
data: Axes,
*,
dtype: TimedeltaDtypeArg,
copy: bool = ...,
Expand All @@ -218,7 +219,7 @@ class Index(IndexOpsMixin[S1]):
@overload
def __new__(
cls,
data: Iterable,
data: Axes,
*,
dtype: Literal["Interval"],
copy: bool = ...,
Expand All @@ -241,7 +242,7 @@ class Index(IndexOpsMixin[S1]):
@overload
def __new__(
cls,
data: Iterable = ...,
data: Axes = ...,
*,
dtype: type[S1],
copy: bool = ...,
Expand All @@ -253,7 +254,7 @@ class Index(IndexOpsMixin[S1]):
@overload
def __new__(
cls,
data: Iterable,
data: Axes,
*,
dtype=...,
copy: bool = ...,
Expand Down
4 changes: 2 additions & 2 deletions pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from datetime import (
from typing import overload

from _typing import (
Axes,
Frequency,
TimeZones,
)
Expand All @@ -30,7 +31,6 @@ from pandas.core.series import (
from typing_extensions import Self

from pandas._typing import (
AnyArrayLike,
DateAndDatetimeLike,
Dtype,
IntervalClosedType,
Expand All @@ -44,7 +44,7 @@ from pandas.tseries.offsets import BaseOffset
class DatetimeIndex(DatetimeTimedeltaMixin[Timestamp], DatetimeIndexProperties):
def __init__(
self,
data: AnyArrayLike | list | tuple,
data: Axes,
freq: Frequency = ...,
tz: TimeZones = ...,
ambiguous: str = ...,
Expand Down
8 changes: 2 additions & 6 deletions pandas-stubs/core/indexes/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,15 @@ from pandas._libs import (
)
from pandas._libs.tslibs import BaseOffset
from pandas._typing import (
AnyArrayLike,
Axes,
TimedeltaConvertibleTypes,
num,
)

class TimedeltaIndex(DatetimeTimedeltaMixin[Timedelta], TimedeltaIndexProperties):
def __new__(
cls,
data: (
AnyArrayLike
| list[str]
| Sequence[dt.timedelta | Timedelta | np.timedelta64 | float]
) = ...,
data: Sequence[dt.timedelta | Timedelta | np.timedelta64 | float] | Axes = ...,
freq: str | BaseOffset = ...,
closed: object = ...,
dtype: Literal["<m8[ns]"] = ...,
Expand Down
22 changes: 22 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,3 +1321,25 @@ def test_index_delete() -> None:

dt_ind = pd.date_range("2023-01-01", "2023-02-01")
check(assert_type(dt_ind.delete(2), pd.DatetimeIndex), pd.DatetimeIndex)


def test_index_dict() -> None:
"""Test passing an ordered iterables to Index and subclasses constructor GH828."""
check(
assert_type(pd.Index({"Jan. 1, 2008": "New Year’s Day"}), "pd.Index[str]"),
pd.Index,
str,
)
check(
assert_type(
pd.DatetimeIndex({"Jan. 1, 2008": "New Year’s Day"}), pd.DatetimeIndex
),
pd.DatetimeIndex,
)
check(
assert_type(
pd.TimedeltaIndex({pd.Timedelta(days=1): "New Year’s Day"}),
pd.TimedeltaIndex,
),
pd.TimedeltaIndex,
)