Skip to content

Commit 0305571

Browse files
twoertweinmroeschke
authored andcommitted
TYP: Misc type corrections (pandas-dev#55078)
1 parent 50cb682 commit 0305571

File tree

6 files changed

+64
-29
lines changed

6 files changed

+64
-29
lines changed

pandas/_libs/tslibs/period.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Period(PeriodMixin):
8989
@classmethod
9090
def _from_ordinal(cls, ordinal: int, freq) -> Period: ...
9191
@classmethod
92-
def now(cls, freq: BaseOffset = ...) -> Period: ...
92+
def now(cls, freq: Frequency = ...) -> Period: ...
9393
def strftime(self, fmt: str) -> str: ...
9494
def to_timestamp(
9595
self,

pandas/_libs/tslibs/timedeltas.pyi

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ from pandas._libs.tslibs import (
1414
Tick,
1515
)
1616
from pandas._typing import (
17+
Frequency,
1718
Self,
1819
npt,
1920
)
@@ -117,9 +118,9 @@ class Timedelta(timedelta):
117118
@property
118119
def asm8(self) -> np.timedelta64: ...
119120
# TODO: round/floor/ceil could return NaT?
120-
def round(self, freq: str) -> Self: ...
121-
def floor(self, freq: str) -> Self: ...
122-
def ceil(self, freq: str) -> Self: ...
121+
def round(self, freq: Frequency) -> Self: ...
122+
def floor(self, freq: Frequency) -> Self: ...
123+
def ceil(self, freq: Frequency) -> Self: ...
123124
@property
124125
def resolution_string(self) -> str: ...
125126
def __add__(self, other: timedelta) -> Timedelta: ...

pandas/_libs/tslibs/timestamps.pyi

+16-13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ from datetime import (
88
from time import struct_time
99
from typing import (
1010
ClassVar,
11+
Literal,
12+
TypeAlias,
1113
TypeVar,
1214
overload,
1315
)
@@ -27,6 +29,7 @@ from pandas._typing import (
2729
)
2830

2931
_DatetimeT = TypeVar("_DatetimeT", bound=datetime)
32+
_TimeZones: TypeAlias = str | _tzinfo | None | int
3033

3134
def integer_op_not_supported(obj: object) -> TypeError: ...
3235

@@ -51,13 +54,13 @@ class Timestamp(datetime):
5154
tzinfo: _tzinfo | None = ...,
5255
*,
5356
nanosecond: int | None = ...,
54-
tz: str | _tzinfo | None | int = ...,
57+
tz: _TimeZones = ...,
5558
unit: str | int | None = ...,
5659
fold: int | None = ...,
5760
) -> _DatetimeT | NaTType: ...
5861
@classmethod
5962
def _from_value_and_reso(
60-
cls, value: int, reso: int, tz: _tzinfo | None
63+
cls, value: int, reso: int, tz: _TimeZones
6164
) -> Timestamp: ...
6265
@property
6366
def value(self) -> int: ... # np.int64
@@ -84,19 +87,19 @@ class Timestamp(datetime):
8487
@property
8588
def fold(self) -> int: ...
8689
@classmethod
87-
def fromtimestamp(cls, ts: float, tz: _tzinfo | None = ...) -> Self: ...
90+
def fromtimestamp(cls, ts: float, tz: _TimeZones = ...) -> Self: ...
8891
@classmethod
8992
def utcfromtimestamp(cls, ts: float) -> Self: ...
9093
@classmethod
91-
def today(cls, tz: _tzinfo | str | None = ...) -> Self: ...
94+
def today(cls, tz: _TimeZones = ...) -> Self: ...
9295
@classmethod
9396
def fromordinal(
9497
cls,
9598
ordinal: int,
96-
tz: _tzinfo | str | None = ...,
99+
tz: _TimeZones = ...,
97100
) -> Self: ...
98101
@classmethod
99-
def now(cls, tz: _tzinfo | str | None = ...) -> Self: ...
102+
def now(cls, tz: _TimeZones = ...) -> Self: ...
100103
@classmethod
101104
def utcnow(cls) -> Self: ...
102105
# error: Signature of "combine" incompatible with supertype "datetime"
@@ -131,7 +134,7 @@ class Timestamp(datetime):
131134
fold: int | None = ...,
132135
) -> Self: ...
133136
# LSP violation: datetime.datetime.astimezone has a default value for tz
134-
def astimezone(self, tz: _tzinfo | None) -> Self: ... # type: ignore[override]
137+
def astimezone(self, tz: _TimeZones) -> Self: ... # type: ignore[override]
135138
def ctime(self) -> str: ...
136139
def isoformat(self, sep: str = ..., timespec: str = ...) -> str: ...
137140
@classmethod
@@ -184,32 +187,32 @@ class Timestamp(datetime):
184187
def to_julian_date(self) -> np.float64: ...
185188
@property
186189
def asm8(self) -> np.datetime64: ...
187-
def tz_convert(self, tz: _tzinfo | str | None) -> Self: ...
190+
def tz_convert(self, tz: _TimeZones) -> Self: ...
188191
# TODO: could return NaT?
189192
def tz_localize(
190193
self,
191-
tz: _tzinfo | str | None,
192-
ambiguous: str = ...,
194+
tz: _TimeZones,
195+
ambiguous: bool | Literal["raise", "NaT"] = ...,
193196
nonexistent: TimestampNonexistent = ...,
194197
) -> Self: ...
195198
def normalize(self) -> Self: ...
196199
# TODO: round/floor/ceil could return NaT?
197200
def round(
198201
self,
199202
freq: str,
200-
ambiguous: bool | str = ...,
203+
ambiguous: bool | Literal["raise", "NaT"] = ...,
201204
nonexistent: TimestampNonexistent = ...,
202205
) -> Self: ...
203206
def floor(
204207
self,
205208
freq: str,
206-
ambiguous: bool | str = ...,
209+
ambiguous: bool | Literal["raise", "NaT"] = ...,
207210
nonexistent: TimestampNonexistent = ...,
208211
) -> Self: ...
209212
def ceil(
210213
self,
211214
freq: str,
212-
ambiguous: bool | str = ...,
215+
ambiguous: bool | Literal["raise", "NaT"] = ...,
213216
nonexistent: TimestampNonexistent = ...,
214217
) -> Self: ...
215218
def day_name(self, locale: str | None = ...) -> str: ...

pandas/_typing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
# Cannot use `Sequence` because a string is a sequence, and we don't want to
113113
# accept that. Could refine if https://github.com/python/typing/issues/256 is
114114
# resolved to differentiate between Sequence[str] and str
115-
ListLike = Union[AnyArrayLike, list, range]
115+
ListLike = Union[AnyArrayLike, list, tuple, range]
116116

117117
# scalars
118118

pandas/core/reshape/pivot.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import (
88
TYPE_CHECKING,
99
Callable,
10+
Literal,
1011
cast,
1112
)
1213

@@ -569,7 +570,7 @@ def crosstab(
569570
margins: bool = False,
570571
margins_name: Hashable = "All",
571572
dropna: bool = True,
572-
normalize: bool = False,
573+
normalize: bool | Literal[0, 1, "all", "index", "columns"] = False,
573574
) -> DataFrame:
574575
"""
575576
Compute a simple cross tabulation of two (or more) factors.

pandas/io/parsers/readers.py

+40-10
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,10 @@ def read_csv(
638638
header: int | Sequence[int] | None | Literal["infer"] = ...,
639639
names: Sequence[Hashable] | None | lib.NoDefault = ...,
640640
index_col: IndexLabel | Literal[False] | None = ...,
641-
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
641+
usecols: list[HashableT]
642+
| tuple[HashableT]
643+
| Callable[[Hashable], bool]
644+
| None = ...,
642645
dtype: DtypeArg | None = ...,
643646
engine: CSVEngine | None = ...,
644647
converters: Mapping[Hashable, Callable] | None = ...,
@@ -697,7 +700,10 @@ def read_csv(
697700
header: int | Sequence[int] | None | Literal["infer"] = ...,
698701
names: Sequence[Hashable] | None | lib.NoDefault = ...,
699702
index_col: IndexLabel | Literal[False] | None = ...,
700-
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
703+
usecols: list[HashableT]
704+
| tuple[HashableT]
705+
| Callable[[Hashable], bool]
706+
| None = ...,
701707
dtype: DtypeArg | None = ...,
702708
engine: CSVEngine | None = ...,
703709
converters: Mapping[Hashable, Callable] | None = ...,
@@ -757,7 +763,10 @@ def read_csv(
757763
header: int | Sequence[int] | None | Literal["infer"] = ...,
758764
names: Sequence[Hashable] | None | lib.NoDefault = ...,
759765
index_col: IndexLabel | Literal[False] | None = ...,
760-
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
766+
usecols: list[HashableT]
767+
| tuple[HashableT]
768+
| Callable[[Hashable], bool]
769+
| None = ...,
761770
dtype: DtypeArg | None = ...,
762771
engine: CSVEngine | None = ...,
763772
converters: Mapping[Hashable, Callable] | None = ...,
@@ -817,7 +826,10 @@ def read_csv(
817826
header: int | Sequence[int] | None | Literal["infer"] = ...,
818827
names: Sequence[Hashable] | None | lib.NoDefault = ...,
819828
index_col: IndexLabel | Literal[False] | None = ...,
820-
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
829+
usecols: list[HashableT]
830+
| tuple[HashableT]
831+
| Callable[[Hashable], bool]
832+
| None = ...,
821833
dtype: DtypeArg | None = ...,
822834
engine: CSVEngine | None = ...,
823835
converters: Mapping[Hashable, Callable] | None = ...,
@@ -888,7 +900,10 @@ def read_csv(
888900
header: int | Sequence[int] | None | Literal["infer"] = "infer",
889901
names: Sequence[Hashable] | None | lib.NoDefault = lib.no_default,
890902
index_col: IndexLabel | Literal[False] | None = None,
891-
usecols: list[HashableT] | Callable[[Hashable], bool] | None = None,
903+
usecols: list[HashableT]
904+
| tuple[HashableT]
905+
| Callable[[Hashable], bool]
906+
| None = None,
892907
# General Parsing Configuration
893908
dtype: DtypeArg | None = None,
894909
engine: CSVEngine | None = None,
@@ -983,7 +998,10 @@ def read_table(
983998
header: int | Sequence[int] | None | Literal["infer"] = ...,
984999
names: Sequence[Hashable] | None | lib.NoDefault = ...,
9851000
index_col: IndexLabel | Literal[False] | None = ...,
986-
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
1001+
usecols: list[HashableT]
1002+
| tuple[HashableT]
1003+
| Callable[[Hashable], bool]
1004+
| None = ...,
9871005
dtype: DtypeArg | None = ...,
9881006
engine: CSVEngine | None = ...,
9891007
converters: Mapping[Hashable, Callable] | None = ...,
@@ -1040,7 +1058,10 @@ def read_table(
10401058
header: int | Sequence[int] | None | Literal["infer"] = ...,
10411059
names: Sequence[Hashable] | None | lib.NoDefault = ...,
10421060
index_col: IndexLabel | Literal[False] | None = ...,
1043-
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
1061+
usecols: list[HashableT]
1062+
| tuple[HashableT]
1063+
| Callable[[Hashable], bool]
1064+
| None = ...,
10441065
dtype: DtypeArg | None = ...,
10451066
engine: CSVEngine | None = ...,
10461067
converters: Mapping[Hashable, Callable] | None = ...,
@@ -1097,7 +1118,10 @@ def read_table(
10971118
header: int | Sequence[int] | None | Literal["infer"] = ...,
10981119
names: Sequence[Hashable] | None | lib.NoDefault = ...,
10991120
index_col: IndexLabel | Literal[False] | None = ...,
1100-
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
1121+
usecols: list[HashableT]
1122+
| tuple[HashableT]
1123+
| Callable[[Hashable], bool]
1124+
| None = ...,
11011125
dtype: DtypeArg | None = ...,
11021126
engine: CSVEngine | None = ...,
11031127
converters: Mapping[Hashable, Callable] | None = ...,
@@ -1154,7 +1178,10 @@ def read_table(
11541178
header: int | Sequence[int] | None | Literal["infer"] = ...,
11551179
names: Sequence[Hashable] | None | lib.NoDefault = ...,
11561180
index_col: IndexLabel | Literal[False] | None = ...,
1157-
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
1181+
usecols: list[HashableT]
1182+
| tuple[HashableT]
1183+
| Callable[[Hashable], bool]
1184+
| None = ...,
11581185
dtype: DtypeArg | None = ...,
11591186
engine: CSVEngine | None = ...,
11601187
converters: Mapping[Hashable, Callable] | None = ...,
@@ -1224,7 +1251,10 @@ def read_table(
12241251
header: int | Sequence[int] | None | Literal["infer"] = "infer",
12251252
names: Sequence[Hashable] | None | lib.NoDefault = lib.no_default,
12261253
index_col: IndexLabel | Literal[False] | None = None,
1227-
usecols: list[HashableT] | Callable[[Hashable], bool] | None = None,
1254+
usecols: list[HashableT]
1255+
| tuple[HashableT]
1256+
| Callable[[Hashable], bool]
1257+
| None = None,
12281258
# General Parsing Configuration
12291259
dtype: DtypeArg | None = None,
12301260
engine: CSVEngine | None = None,

0 commit comments

Comments
 (0)