Skip to content

Commit 11424d5

Browse files
authored
type PeriodIndex.__new__, PeriodIndex.asof_locs, remove some inherited methods (#1331)
* type ``PeriodIndex.__new__`` * shift and asof_locs too * fixup return type * np.integer -> np.intp * fixup generics * __new__ return type too * use np_1darray alias * one more
1 parent 82d1a65 commit 11424d5

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

pandas-stubs/_typing.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ IndexingInt: TypeAlias = (
893893
)
894894

895895
# AxesData is used for data for Index
896-
AxesData: TypeAlias = Mapping[S3, Any] | Axes | KeysView
896+
AxesData: TypeAlias = Mapping[S3, Any] | Axes | KeysView[S3]
897897

898898
# Any plain Python or numpy function
899899
Function: TypeAlias = np.ufunc | Callable[..., Any]

pandas-stubs/core/indexes/period.pyi

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections.abc import Hashable
22
import datetime
33
from typing import (
4-
final,
4+
Any,
55
overload,
66
)
77

@@ -14,27 +14,28 @@ from pandas.core.indexes.timedeltas import TimedeltaIndex
1414
from typing_extensions import Self
1515

1616
from pandas._libs.tslibs import (
17-
BaseOffset,
1817
NaTType,
1918
Period,
2019
)
2120
from pandas._libs.tslibs.period import _PeriodAddSub
21+
from pandas._typing import (
22+
AxesData,
23+
Dtype,
24+
Frequency,
25+
np_1darray,
26+
)
2227

2328
class PeriodIndex(DatetimeIndexOpsMixin[pd.Period, np.object_], PeriodIndexFieldOps):
2429
def __new__(
2530
cls,
26-
data=...,
27-
ordinal=...,
28-
freq=...,
29-
tz=...,
30-
dtype=...,
31-
copy: bool = ...,
32-
name: Hashable = ...,
33-
**fields,
34-
): ...
31+
data: AxesData[Any] | None = None,
32+
freq: Frequency | None = None,
33+
dtype: Dtype | None = None,
34+
copy: bool = False,
35+
name: Hashable | None = None,
36+
) -> Self: ...
3537
@property
36-
def values(self): ...
37-
def __contains__(self, key) -> bool: ...
38+
def values(self) -> np_1darray[np.object_]: ...
3839
@overload
3940
def __sub__(self, other: Period) -> Index: ...
4041
@overload
@@ -53,31 +54,18 @@ class PeriodIndex(DatetimeIndexOpsMixin[pd.Period, np.object_], PeriodIndexField
5354
def __rsub__( # pyright: ignore[reportIncompatibleMethodOverride]
5455
self, other: NaTType
5556
) -> NaTType: ...
56-
@final
57-
def __array_wrap__(self, result, context=...): ...
58-
def asof_locs(self, where, mask): ...
59-
def searchsorted(self, value, side: str = ..., sorter=...): ...
57+
def asof_locs(
58+
self,
59+
where: pd.DatetimeIndex | PeriodIndex,
60+
mask: np_1darray[np.bool_],
61+
) -> np_1darray[np.intp]: ...
6062
@property
6163
def is_full(self) -> bool: ...
6264
@property
6365
def inferred_type(self) -> str: ...
64-
@final
65-
def get_indexer(self, target, method=..., limit=..., tolerance=...): ...
66-
def get_indexer_non_unique(self, target): ...
67-
def insert(self, loc, item): ...
68-
@final
69-
def join(
70-
self,
71-
other,
72-
*,
73-
how: str = ...,
74-
level=...,
75-
return_indexers: bool = ...,
76-
sort: bool = ...,
77-
): ...
7866
@property
7967
def freqstr(self) -> str: ...
80-
def shift(self, periods: int = 1, freq=...) -> Self: ...
68+
def shift(self, periods: int = 1, freq: Frequency | None = None) -> Self: ...
8169

8270
def period_range(
8371
start: (
@@ -87,6 +75,6 @@ def period_range(
8775
str | datetime.datetime | datetime.date | pd.Timestamp | pd.Period | None
8876
) = None,
8977
periods: int | None = None,
90-
freq: str | BaseOffset | None = None,
78+
freq: Frequency | None = None,
9179
name: Hashable | None = None,
9280
) -> PeriodIndex: ...

tests/test_indexes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,3 +1489,26 @@ def test_index_naming() -> None:
14891489
check(assert_type(df.index.names, list[Hashable | None]), list)
14901490
df.index.names = (None,)
14911491
check(assert_type(df.index.names, list[Hashable | None]), list)
1492+
1493+
1494+
def test_period_index_constructor() -> None:
1495+
check(
1496+
assert_type(pd.PeriodIndex(["2000"], dtype="period[D]"), pd.PeriodIndex),
1497+
pd.PeriodIndex,
1498+
)
1499+
check(
1500+
assert_type(
1501+
pd.PeriodIndex(["2000"], freq="D", name="foo", copy=True), pd.PeriodIndex
1502+
),
1503+
pd.PeriodIndex,
1504+
)
1505+
1506+
1507+
def test_period_index_asof_locs() -> None:
1508+
idx = pd.PeriodIndex(["2000", "2001"], freq="D")
1509+
where = pd.DatetimeIndex(["2023-05-30 00:12:00", "2023-06-01 00:00:00"])
1510+
mask = np.ones(2, dtype=bool)
1511+
check(
1512+
assert_type(idx.asof_locs(where, mask), np_1darray[np.intp]),
1513+
np_1darray[np.intp],
1514+
)

0 commit comments

Comments
 (0)