Skip to content

Commit ff8fd9b

Browse files
committed
Updates several type hints where incomplete or inaccurate
1 parent e4b9500 commit ff8fd9b

File tree

10 files changed

+86
-43
lines changed

10 files changed

+86
-43
lines changed

pandas/_typing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@
122122
JSONSerializable = Optional[Union[PythonScalar, List, Dict]]
123123
Frequency = Union[str, "DateOffset"]
124124
Axes = Collection[Any]
125+
MergeTypes = Literal['inner', 'outer', 'left', 'right', 'cross']
126+
ConcatTypes = Literal['inner', 'outer']
125127

126128
# dtypes
127129
NpDtype = Union[str, np.dtype]

pandas/core/apply.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Hashable,
1010
Iterator,
1111
List,
12+
Literal,
1213
cast,
1314
)
1415
import warnings
@@ -504,7 +505,10 @@ def apply_multiple(self) -> FrameOrSeriesUnion:
504505
return self.obj.aggregate(self.f, self.axis, *self.args, **self.kwargs)
505506

506507
def normalize_dictlike_arg(
507-
self, how: str, obj: FrameOrSeriesUnion, func: AggFuncTypeDict
508+
self,
509+
how: Literal['apply', 'agg', 'transform'],
510+
obj: FrameOrSeriesUnion,
511+
func: AggFuncTypeDict,
508512
) -> AggFuncTypeDict:
509513
"""
510514
Handler for dict-like argument.

pandas/core/arrays/_ranges.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
(and possibly TimedeltaArray/PeriodArray)
44
"""
55

6-
from typing import Union
6+
from typing import (
7+
Literal,
8+
Union,
9+
)
710

811
import numpy as np
912

@@ -75,7 +78,7 @@ def generate_regular_range(
7578

7679

7780
def _generate_range_overflow_safe(
78-
endpoint: int, periods: int, stride: int, side: str = "start"
81+
endpoint: int, periods: int, stride: int, side: Literal['start', 'end'] = "start"
7982
) -> int:
8083
"""
8184
Calculate the second endpoint for passing to np.arange, checking
@@ -143,7 +146,7 @@ def _generate_range_overflow_safe(
143146

144147

145148
def _generate_range_overflow_safe_signed(
146-
endpoint: int, periods: int, stride: int, side: str
149+
endpoint: int, periods: int, stride: int, side: Literal['start', 'end']
147150
) -> int:
148151
"""
149152
A special case for _generate_range_overflow_safe where `periods * stride`
@@ -154,7 +157,7 @@ def _generate_range_overflow_safe_signed(
154157
if side == 'end':
155158
stride *= -1
156159

157-
with np.errstate(over="raise"):
160+
with np.errstate(over='raise'):
158161
addend = np.int64(periods) * np.int64(stride)
159162
try:
160163
# easy case with no overflows

pandas/core/arrays/datetimes.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,12 +1958,12 @@ def sequence_to_datetimes(
19581958

19591959
def sequence_to_dt64ns(
19601960
data,
1961-
dtype=None,
1962-
copy=False,
1963-
tz=None,
1964-
dayfirst=False,
1965-
yearfirst=False,
1966-
ambiguous="raise",
1961+
dtype: str = None,
1962+
copy: bool = False,
1963+
tz: tzinfo | str = None,
1964+
dayfirst: bool = False,
1965+
yearfirst: bool = False,
1966+
ambiguous: str | bool = "raise",
19671967
*,
19681968
allow_object: bool = False,
19691969
allow_mixed: bool = False,
@@ -2117,10 +2117,10 @@ def sequence_to_dt64ns(
21172117

21182118
def objects_to_datetime64ns(
21192119
data: np.ndarray,
2120-
dayfirst,
2121-
yearfirst,
2122-
utc=False,
2123-
errors="raise",
2120+
dayfirst: bool,
2121+
yearfirst: bool,
2122+
utc: bool = False,
2123+
errors: Literal["raise", "coerce", "ignore"] = "raise",
21242124
require_iso8601: bool = False,
21252125
allow_object: bool = False,
21262126
allow_mixed: bool = False,

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9165,7 +9165,7 @@ def merge(
91659165
sort: bool = False,
91669166
suffixes: Suffixes = ("_x", "_y"),
91679167
copy: bool = True,
9168-
indicator: bool = False,
9168+
indicator: bool | str = False,
91699169
validate: str | None = None,
91709170
) -> DataFrame:
91719171
from pandas.core.reshape.merge import merge

pandas/core/indexes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5694,7 +5694,7 @@ def _validate_indexer(self, form: str_t, key, kind: str_t):
56945694
if key is not None and not is_integer(key):
56955695
raise self._invalid_indexer(form, key)
56965696

5697-
def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
5697+
def _maybe_cast_slice_bound(self, label, side: str_t, kind: Literal["loc", "getitem"] = no_default):
56985698
"""
56995699
This function should be overloaded in subclasses that allow non-trivial
57005700
casting on label-slice bounds, e.g. datetime-like indices allowing

pandas/core/missing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
TYPE_CHECKING,
1212
Any,
1313
cast,
14+
Literal,
1415
)
1516

1617
import numpy as np
@@ -164,7 +165,7 @@ def clean_interp_method(method: str, index: Index, **kwargs) -> str:
164165
return method
165166

166167

167-
def find_valid_index(values, *, how: str) -> int | None:
168+
def find_valid_index(values, *, how: Literal['first', 'last']) -> int | None:
168169
"""
169170
Retrieves the index of the first valid value.
170171

pandas/core/reshape/merge.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
FrameOrSeries,
3030
IndexLabel,
3131
Suffixes,
32+
MergeTypes,
33+
TimedeltaConvertibleTypes,
3234
)
3335
from pandas.errors import MergeError
3436
from pandas.util._decorators import (
@@ -92,7 +94,7 @@
9294
def merge(
9395
left: DataFrame | Series,
9496
right: DataFrame | Series,
95-
how: str = "inner",
97+
how: MergeTypes = "inner",
9698
on: IndexLabel | None = None,
9799
left_on: IndexLabel | None = None,
98100
right_on: IndexLabel | None = None,
@@ -101,7 +103,7 @@ def merge(
101103
sort: bool = False,
102104
suffixes: Suffixes = ("_x", "_y"),
103105
copy: bool = True,
104-
indicator: bool = False,
106+
indicator: bool | str = False,
105107
validate: str | None = None,
106108
) -> DataFrame:
107109
op = _MergeOperation(
@@ -331,11 +333,11 @@ def merge_asof(
331333
right_on: IndexLabel | None = None,
332334
left_index: bool = False,
333335
right_index: bool = False,
334-
by=None,
335-
left_by=None,
336-
right_by=None,
336+
by: IndexLabel | None = None,
337+
left_by: Hashable | None = None,
338+
right_by: Hashable | None = None,
337339
suffixes: Suffixes = ("_x", "_y"),
338-
tolerance=None,
340+
tolerance: None | TimedeltaConvertibleTypes = None,
339341
allow_exact_matches: bool = True,
340342
direction: str = "backward",
341343
) -> DataFrame:
@@ -622,7 +624,7 @@ def __init__(
622624
sort: bool = True,
623625
suffixes: Suffixes = ("_x", "_y"),
624626
copy: bool = True,
625-
indicator: bool = False,
627+
indicator: bool | str = False,
626628
validate: str | None = None,
627629
):
628630
_left = _validate_operand(left)

pandas/tseries/frequencies.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def _maybe_add_count(base: str, count: float) -> str:
445445
# Frequency comparison
446446

447447

448-
def is_subperiod(source, target) -> bool:
448+
def is_subperiod(source: str | DateOffset, target: str | DateOffset) -> bool:
449449
"""
450450
Returns True if downsampling is possible between source and target
451451
frequencies
@@ -501,7 +501,7 @@ def is_subperiod(source, target) -> bool:
501501
return False
502502

503503

504-
def is_superperiod(source, target) -> bool:
504+
def is_superperiod(source: str | DateOffset, target: str | DateOffset) -> bool:
505505
"""
506506
Returns True if upsampling is possible between source and target
507507
frequencies
@@ -559,7 +559,7 @@ def is_superperiod(source, target) -> bool:
559559
return False
560560

561561

562-
def _maybe_coerce_freq(code) -> str:
562+
def _maybe_coerce_freq(code: str | DateOffset) -> str:
563563
"""we might need to coerce a code to a rule_code
564564
and uppercase it
565565

pandas/tseries/holiday.py

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
from __future__ import annotations
2+
from typing import (
3+
Any,
4+
Callable,
5+
Tuple,
6+
)
27

38
from datetime import (
49
datetime,
@@ -33,6 +38,10 @@
3338
Easter,
3439
)
3540

41+
from pandas._typing import (
42+
TimestampConvertibleTypes
43+
)
44+
3645

3746
def next_monday(dt: datetime) -> datetime:
3847
"""
@@ -151,27 +160,39 @@ class Holiday:
151160

152161
def __init__(
153162
self,
154-
name,
155-
year=None,
156-
month=None,
157-
day=None,
158-
offset=None,
159-
observance=None,
160-
start_date=None,
161-
end_date=None,
162-
days_of_week=None,
163+
name: str,
164+
year: int | None = None,
165+
month: int | None = None,
166+
day: int | None = None,
167+
offset: list[Any] | Any = None,
168+
observance: Callable[..., DatetimeIndex] = None,
169+
start_date: TimestampConvertibleTypes = None,
170+
end_date: TimestampConvertibleTypes = None,
171+
days_of_week: Tuple[int, ...] = None,
163172
):
164173
"""
165174
Parameters
166175
----------
167176
name : str
168177
Name of the holiday , defaults to class name
178+
year : int, optional
179+
the year in which the holiday occurs
180+
month : int, optional
181+
the month in which the holiday occurs
182+
day : int, optional
183+
the day on which the holiday occurs
169184
offset : array of pandas.tseries.offsets or
170185
class from pandas.tseries.offsets
171186
computes offset from date
172187
observance: function
173188
computes when holiday is given a pandas Timestamp
174-
days_of_week:
189+
start_date : datetime-like
190+
optionally constrain the period in which the holdiday occurs
191+
can be any valid value for a pandas Timestamp
192+
end_date : datetime-like
193+
optionally constrain the period in which the holdiday occurs
194+
can be any valid value for a pandas Timestamp
195+
days_of_week: tuple(int, ...)
175196
provide a tuple of days e.g (0,1,2,3,) for Monday Through Thursday
176197
Monday=0,..,Sunday=6
177198
@@ -239,7 +260,12 @@ def __repr__(self) -> str:
239260
repr = f"Holiday: {self.name} ({info})"
240261
return repr
241262

242-
def dates(self, start_date, end_date, return_name=False):
263+
def dates(
264+
self,
265+
start_date: TimestampConvertibleTypes,
266+
end_date: TimestampConvertibleTypes,
267+
return_name: bool = False,
268+
):
243269
"""
244270
Calculate holidays observed between start date and end date
245271
@@ -286,7 +312,7 @@ def dates(self, start_date, end_date, return_name=False):
286312
return Series(self.name, index=holiday_dates)
287313
return holiday_dates
288314

289-
def _reference_dates(self, start_date, end_date):
315+
def _reference_dates(self, start_date: TimestampConvertibleTypes, end_date: TimestampConvertibleTypes):
290316
"""
291317
Get reference dates for the holiday.
292318
@@ -319,7 +345,7 @@ def _reference_dates(self, start_date, end_date):
319345

320346
return dates
321347

322-
def _apply_rule(self, dates):
348+
def _apply_rule(self, dates: DatetimeIndex):
323349
"""
324350
Apply the given offset/observance to a DatetimeIndex of dates.
325351
@@ -390,7 +416,7 @@ class AbstractHolidayCalendar(metaclass=HolidayCalendarMetaClass):
390416
end_date = Timestamp(datetime(2200, 12, 31))
391417
_cache = None
392418

393-
def __init__(self, name=None, rules=None):
419+
def __init__(self, name: str = None, rules: list[Holiday] = None):
394420
"""
395421
Initializes holiday object with a given set a rules. Normally
396422
classes just have the rules defined within them.
@@ -417,7 +443,12 @@ def rule_from_name(self, name):
417443

418444
return None
419445

420-
def holidays(self, start=None, end=None, return_name=False):
446+
def holidays(
447+
self,
448+
start: TimestampConvertibleTypes = None,
449+
end: TimestampConvertibleTypes = None,
450+
return_name: bool = False
451+
) -> DatetimeIndex:
421452
"""
422453
Returns a curve with holidays between start_date and end_date
423454

0 commit comments

Comments
 (0)