Skip to content

type datetimeindex constructor #1120

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 7 commits into from
Feb 18, 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
40 changes: 20 additions & 20 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: Literal["int"] | type_t[int | np.integer] = ...,
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> Index[int]: ...
Expand All @@ -84,7 +84,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: Literal["int"] | type_t[int | np.integer],
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> Index[int]: ...
Expand All @@ -95,7 +95,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: Literal["float"] | type_t[float | np.floating] = ...,
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> Index[float]: ...
Expand All @@ -106,7 +106,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: Literal["float"] | type_t[float | np.floating],
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> Index[float]: ...
Expand All @@ -121,7 +121,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: Literal["complex"] | type_t[complex | np.complexfloating] = ...,
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> Index[complex]: ...
Expand All @@ -132,7 +132,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: Literal["complex"] | type_t[complex | np.complexfloating],
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> Index[complex]: ...
Expand All @@ -144,7 +144,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: TimestampDtypeArg = ...,
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> DatetimeIndex: ...
Expand All @@ -155,7 +155,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: TimestampDtypeArg,
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> DatetimeIndex: ...
Expand All @@ -166,7 +166,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: PeriodDtype = ...,
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> PeriodIndex: ...
Expand All @@ -177,7 +177,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: PeriodDtype,
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> PeriodIndex: ...
Expand All @@ -188,7 +188,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: TimedeltaDtypeArg = ...,
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> TimedeltaIndex: ...
Expand All @@ -199,7 +199,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: TimedeltaDtypeArg,
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> TimedeltaIndex: ...
Expand All @@ -210,7 +210,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: Literal["Interval"] = ...,
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> IntervalIndex[Interval[_OrderableT]]: ...
Expand All @@ -221,7 +221,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: Literal["Interval"],
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> IntervalIndex[Interval[Any]]: ...
Expand All @@ -233,7 +233,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: type[S1] = ...,
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> Self: ...
Expand All @@ -244,7 +244,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype: type[S1],
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> Self: ...
Expand All @@ -256,7 +256,7 @@ class Index(IndexOpsMixin[S1]):
*,
dtype=...,
copy: bool = ...,
name=...,
name: Hashable = ...,
tupleize_cols: bool = ...,
**kwargs,
) -> Self: ...
Expand All @@ -277,14 +277,14 @@ class Index(IndexOpsMixin[S1]):
self, indices, axis: int = ..., allow_fill: bool = ..., fill_value=..., **kwargs
): ...
def repeat(self, repeats, axis=...): ...
def copy(self, name=..., deep: bool = ...) -> Self: ...
def copy(self, name: Hashable = ..., deep: bool = ...) -> Self: ...
def __copy__(self, **kwargs): ...
def __deepcopy__(self, memo=...): ...
def format(
self, name: bool = ..., formatter: Callable | None = ..., na_rep: _str = ...
) -> list[_str]: ...
def to_flat_index(self): ...
def to_series(self, index=..., name=...) -> Series: ...
def to_series(self, index=..., name: Hashable = ...) -> Series: ...
def to_frame(self, index: bool = ..., name=...) -> DataFrame: ...
@property
def name(self): ...
Expand Down Expand Up @@ -341,7 +341,7 @@ class Index(IndexOpsMixin[S1]):
def intersection(self, other: list[S1] | Self, sort: bool = ...) -> Self: ...
def difference(self, other: list | Index, sort: bool | None = None) -> Self: ...
def symmetric_difference(
self, other: list[S1] | Self, result_name=..., sort=...
self, other: list[S1] | Self, result_name: Hashable = ..., sort=...
) -> Self: ...
def get_loc(
self,
Expand Down
7 changes: 5 additions & 2 deletions pandas-stubs/core/indexes/category.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from collections.abc import Iterable
from collections.abc import (
Hashable,
Iterable,
)
from typing import Literal

import numpy as np
Expand All @@ -22,7 +25,7 @@ class CategoricalIndex(ExtensionIndex[S1], accessor.PandasDelegate):
ordered=...,
dtype=...,
copy: bool = ...,
name=...,
name: Hashable = ...,
) -> Self: ...
def equals(self, other): ...
@property
Expand Down
16 changes: 10 additions & 6 deletions pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ from datetime import (
)
from typing import overload

from _typing import TimeZones
from _typing import (
Frequency,
TimeZones,
)
import numpy as np
from pandas import (
DataFrame,
Expand All @@ -29,6 +32,7 @@ from pandas._typing import (
AnyArrayLike,
ArrayLike,
DateAndDatetimeLike,
Dtype,
IntervalClosedType,
TimeUnit,
)
Expand All @@ -41,14 +45,14 @@ class DatetimeIndex(DatetimeTimedeltaMixin[Timestamp], DatetimeIndexProperties):
def __init__(
self,
data: ArrayLike | AnyArrayLike | list | tuple,
freq=...,
tz=...,
freq: Frequency = ...,
tz: TimeZones = ...,
ambiguous: str = ...,
dayfirst: bool = ...,
yearfirst: bool = ...,
dtype=...,
dtype: Dtype = ...,
copy: bool = ...,
name=...,
name: Hashable = ...,
) -> None: ...
def __array__(self, dtype=...) -> np.ndarray: ...
def __reduce__(self): ...
Expand All @@ -70,7 +74,7 @@ class DatetimeIndex(DatetimeTimedeltaMixin[Timestamp], DatetimeIndexProperties):
def __sub__(
self, other: datetime | Timestamp | DatetimeIndex
) -> TimedeltaIndex: ...
def to_series(self, index=..., name=...) -> TimestampSeries: ...
def to_series(self, index=..., name: Hashable = ...) -> TimestampSeries: ...
def snap(self, freq: str = ...): ...
def slice_indexer(self, start=..., end=..., step=...): ...
def searchsorted(self, value, side: str = ..., sorter=...): ...
Expand Down
33 changes: 15 additions & 18 deletions pandas-stubs/core/indexes/multi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ from typing import (
overload,
)

from _typing import SequenceNotStr
import numpy as np
import pandas as pd
from pandas.core.indexes.base import Index
Expand All @@ -29,33 +30,29 @@ class MultiIndex(Index[Any]):
levels=...,
codes=...,
sortorder=...,
names=...,
names: SequenceNotStr[Hashable] = ...,
dtype=...,
copy=...,
name=...,
name: SequenceNotStr[Hashable] = ...,
verify_integrity: bool = ...,
_set_identity: bool = ...,
) -> Self: ...
def __init__(
self,
levels=...,
codes=...,
sortorder=...,
names=...,
dtype=...,
copy=...,
name=...,
verify_integrity: bool = ...,
_set_identity: bool = ...,
) -> None: ...
@classmethod
def from_arrays(cls, arrays, sortorder=..., names=...) -> Self: ...
def from_arrays(
cls, arrays, sortorder=..., names: SequenceNotStr[Hashable] = ...
) -> Self: ...
@classmethod
def from_tuples(cls, tuples, sortorder=..., names=...) -> Self: ...
def from_tuples(
cls, tuples, sortorder=..., names: SequenceNotStr[Hashable] = ...
) -> Self: ...
@classmethod
def from_product(cls, iterables, sortorder=..., names=...) -> Self: ...
def from_product(
cls, iterables, sortorder=..., names: SequenceNotStr[Hashable] = ...
) -> Self: ...
@classmethod
def from_frame(cls, df, sortorder=..., names=...) -> Self: ...
def from_frame(
cls, df, sortorder=..., names: SequenceNotStr[Hashable] = ...
) -> Self: ...
@property
def shape(self): ...
@property # Should be read-only
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/period.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PeriodIndex(DatetimeIndexOpsMixin[pd.Period], PeriodIndexFieldOps):
tz=...,
dtype=...,
copy: bool = ...,
name=...,
name: Hashable = ...,
**fields,
): ...
@property
Expand Down
11 changes: 7 additions & 4 deletions pandas-stubs/core/indexes/range.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from collections.abc import Sequence
from collections.abc import (
Hashable,
Sequence,
)
from typing import overload

import numpy as np
Expand All @@ -19,10 +22,10 @@ class RangeIndex(Index[int]):
step: int = ...,
dtype=...,
copy: bool = ...,
name=...,
name: Hashable = ...,
): ...
@classmethod
def from_range(cls, data, name=..., dtype=...): ...
def from_range(cls, data, name: Hashable = ..., dtype=...): ...
def __reduce__(self): ...
@property
def start(self) -> int: ...
Expand All @@ -46,7 +49,7 @@ class RangeIndex(Index[int]):
def __contains__(self, key: int | np.integer) -> bool: ...
def get_indexer(self, target, method=..., limit=..., tolerance=...): ...
def tolist(self): ...
def copy(self, name=..., deep: bool = ..., dtype=..., **kwargs): ...
def copy(self, name: Hashable = ..., deep: bool = ..., dtype=..., **kwargs): ...
def min(self, axis=..., skipna: bool = ..., *args, **kwargs): ...
def max(self, axis=..., skipna: bool = ..., *args, **kwargs): ...
def argsort(self, *args, **kwargs): ...
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class TimedeltaIndex(DatetimeTimedeltaMixin[Timedelta], TimedeltaIndexProperties
@property
def inferred_type(self) -> str: ...
def insert(self, loc, item): ...
def to_series(self, index=..., name=...) -> TimedeltaSeries: ...
def to_series(self, index=..., name: Hashable = ...) -> TimedeltaSeries: ...

def timedelta_range(
start: TimedeltaConvertibleTypes = ...,
Expand Down
31 changes: 31 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ def test_multiindex_get_level_values() -> None:
check(assert_type(i1, pd.Index), pd.Index)


def test_multiindex_constructor() -> None:
check(
assert_type(
pd.MultiIndex([[1], [4]], codes=[[0], [0]], name=["a", "b"]), pd.MultiIndex
),
pd.MultiIndex,
)
check(
assert_type(
pd.MultiIndex([[1], [4]], codes=[[0], [0]], names=["a", "b"]), pd.MultiIndex
),
pd.MultiIndex,
)


def test_index_tolist() -> None:
i1 = pd.Index([1, 2, 3])
check(assert_type(i1.tolist(), list[int]), list, int)
Expand Down Expand Up @@ -1053,6 +1068,22 @@ def test_index_constructors():
pd.Index(flist, dtype=np.float16)


def test_datetime_index_constructor() -> None:
check(assert_type(pd.DatetimeIndex(["2020"]), pd.DatetimeIndex), pd.DatetimeIndex)
check(
assert_type(pd.DatetimeIndex(["2020"], name="ts"), pd.DatetimeIndex),
pd.DatetimeIndex,
)
check(
assert_type(pd.DatetimeIndex(["2020"], freq="D"), pd.DatetimeIndex),
pd.DatetimeIndex,
)
check(
assert_type(pd.DatetimeIndex(["2020"], tz="Asia/Kathmandu"), pd.DatetimeIndex),
pd.DatetimeIndex,
)


def test_iter() -> None:
# GH 723
with pytest_warns_bounded(
Expand Down