Skip to content

Commit 4b54433

Browse files
jbrockmendelJulianWgs
authored andcommitted
TYP: annotations (pandas-dev#40955)
1 parent 0a0c487 commit 4b54433

15 files changed

+79
-63
lines changed

pandas/_libs/hashtable_class_helper.pxi.in

+2-1
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,8 @@ cdef class StringHashTable(HashTable):
791791
raise KeyError(key)
792792

793793
@cython.boundscheck(False)
794-
def get_indexer(self, ndarray[object] values):
794+
def get_indexer(self, ndarray[object] values) -> ndarray:
795+
# -> np.ndarray[np.intp]
795796
cdef:
796797
Py_ssize_t i, n = len(values)
797798
ndarray[intp_t] labels = np.empty(n, dtype=np.intp)

pandas/_libs/lib.pyi

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import (
55
Any,
66
Callable,
7+
Generator,
78
)
89

910
import numpy as np
@@ -52,8 +53,7 @@ def is_bool_array(values: np.ndarray, skipna: bool = False): ...
5253

5354
def fast_multiget(mapping: dict, keys: np.ndarray, default=np.nan) -> ArrayLike: ...
5455

55-
# TODO: gen: Generator?
56-
def fast_unique_multiple_list_gen(gen: object, sort: bool = True) -> list: ...
56+
def fast_unique_multiple_list_gen(gen: Generator, sort: bool = True) -> list: ...
5757
def fast_unique_multiple_list(lists: list, sort: bool = True) -> list: ...
5858
def fast_unique_multiple(arrays: list, sort: bool = True) -> list: ...
5959

@@ -90,10 +90,9 @@ def infer_datetimelike_array(
9090
arr: np.ndarray # np.ndarray[object]
9191
) -> str: ...
9292

93-
# TODO: new_dtype -> np.dtype?
9493
def astype_intsafe(
9594
arr: np.ndarray, # np.ndarray[object]
96-
new_dtype,
95+
new_dtype: np.dtype,
9796
) -> np.ndarray: ...
9897

9998
def fast_zip(ndarrays: list) -> np.ndarray: ... # np.ndarray[object]
@@ -134,15 +133,13 @@ def memory_usage_of_objects(
134133
) -> int: ... # np.int64
135134

136135

137-
# TODO: f: Callable?
138-
# TODO: dtype -> DtypeObj?
139136
def map_infer_mask(
140137
arr: np.ndarray,
141138
f: Callable[[Any], Any],
142139
mask: np.ndarray, # const uint8_t[:]
143140
convert: bool = ...,
144141
na_value: Any = ...,
145-
dtype: Any = ...,
142+
dtype: np.dtype = ...,
146143
) -> ArrayLike: ...
147144

148145
def indices_fast(

pandas/_libs/lib.pyx

+7-5
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ def array_equivalent_object(left: object[:], right: object[:]) -> bool:
633633

634634
@cython.wraparound(False)
635635
@cython.boundscheck(False)
636-
def astype_intsafe(ndarray[object] arr, new_dtype) -> ndarray:
636+
def astype_intsafe(ndarray[object] arr, cnp.dtype new_dtype) -> ndarray:
637637
cdef:
638638
Py_ssize_t i, n = len(arr)
639639
object val
@@ -661,7 +661,8 @@ cpdef ndarray[object] ensure_string_array(
661661
bint copy=True,
662662
bint skipna=True,
663663
):
664-
"""Returns a new numpy array with object dtype and only strings and na values.
664+
"""
665+
Returns a new numpy array with object dtype and only strings and na values.
665666
666667
Parameters
667668
----------
@@ -679,7 +680,7 @@ cpdef ndarray[object] ensure_string_array(
679680
680681
Returns
681682
-------
682-
ndarray
683+
np.ndarray[object]
683684
An array with the input array's elements casted to str or nan-like.
684685
"""
685686
cdef:
@@ -2452,7 +2453,8 @@ no_default = NoDefault.no_default # Sentinel indicating the default value.
24522453
@cython.boundscheck(False)
24532454
@cython.wraparound(False)
24542455
def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask, bint convert=True,
2455-
object na_value=no_default, object dtype=object) -> "ArrayLike":
2456+
object na_value=no_default, cnp.dtype dtype=np.dtype(object)
2457+
) -> "ArrayLike":
24562458
"""
24572459
Substitute for np.vectorize with pandas-friendly dtype inference.
24582460

@@ -2472,7 +2474,7 @@ def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask, bint convert=Tr
24722474

24732475
Returns
24742476
-------
2475-
ndarray
2477+
np.ndarray or ExtensionArray
24762478
"""
24772479
cdef:
24782480
Py_ssize_t i, n

pandas/_libs/tslibs/fields.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def build_field_sarray(const int64_t[:] dtindex):
9393
return out
9494

9595

96-
def month_position_check(fields, weekdays):
96+
def month_position_check(fields, weekdays) -> str | None:
9797
cdef:
9898
int32_t daysinmonth, y, m, d
9999
bint calendar_end = True
@@ -755,7 +755,7 @@ cdef inline ndarray[int64_t] _roundup_int64(values, int64_t unit):
755755
return _floor_int64(values + unit // 2, unit)
756756

757757

758-
def round_nsint64(values: np.ndarray, mode: RoundTo, nanos) -> np.ndarray:
758+
def round_nsint64(values: np.ndarray, mode: RoundTo, nanos: int) -> np.ndarray:
759759
"""
760760
Applies rounding mode at given frequency
761761

pandas/core/arrays/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ def argsort(
609609
610610
Returns
611611
-------
612-
ndarray
612+
np.ndarray[np.intp]
613613
Array of indices that sort ``self``. If NaN values are contained,
614614
NaN values are placed at the end.
615615

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ def argsort(self, ascending=True, kind="quicksort", **kwargs):
15991599
16001600
Returns
16011601
-------
1602-
numpy.array
1602+
np.ndarray[np.intp]
16031603
16041604
See Also
16051605
--------

pandas/core/arrays/timedeltas.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ class TimedeltaArray(dtl.TimelikeOps):
135135
# define my properties & methods for delegation
136136
_other_ops: list[str] = []
137137
_bool_ops: list[str] = []
138-
_object_ops = ["freq"]
139-
_field_ops = ["days", "seconds", "microseconds", "nanoseconds"]
140-
_datetimelike_ops = _field_ops + _object_ops + _bool_ops
141-
_datetimelike_methods = [
138+
_object_ops: list[str] = ["freq"]
139+
_field_ops: list[str] = ["days", "seconds", "microseconds", "nanoseconds"]
140+
_datetimelike_ops: list[str] = _field_ops + _object_ops + _bool_ops
141+
_datetimelike_methods: list[str] = [
142142
"to_pytimedelta",
143143
"total_seconds",
144144
"round",

pandas/core/indexes/api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _get_combined_index(
164164
return index
165165

166166

167-
def union_indexes(indexes, sort=True) -> Index:
167+
def union_indexes(indexes, sort: bool = True) -> Index:
168168
"""
169169
Return the union of indexes.
170170
@@ -273,7 +273,7 @@ def _sanitize_and_check(indexes):
273273
return indexes, "array"
274274

275275

276-
def all_indexes_same(indexes):
276+
def all_indexes_same(indexes) -> bool:
277277
"""
278278
Determine if all indexes contain the same elements.
279279

pandas/core/indexes/base.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def join(
215215
return cast(F, join)
216216

217217

218-
def disallow_kwargs(kwargs: dict[str, Any]):
218+
def disallow_kwargs(kwargs: dict[str, Any]) -> None:
219219
if kwargs:
220220
raise TypeError(f"Unexpected keyword arguments {repr(set(kwargs))}")
221221

@@ -626,7 +626,7 @@ def _maybe_check_unique(self) -> None:
626626
raise DuplicateLabelError(msg)
627627

628628
@final
629-
def _format_duplicate_message(self):
629+
def _format_duplicate_message(self) -> DataFrame:
630630
"""
631631
Construct the DataFrame for a DuplicateLabelError.
632632
@@ -789,7 +789,7 @@ def __array_wrap__(self, result, context=None):
789789
return Index(result, **attrs)
790790

791791
@cache_readonly
792-
def dtype(self):
792+
def dtype(self) -> DtypeObj:
793793
"""
794794
Return the dtype object of the underlying data.
795795
"""
@@ -1064,11 +1064,11 @@ def copy(
10641064
return new_index
10651065

10661066
@final
1067-
def __copy__(self, **kwargs):
1067+
def __copy__(self: _IndexT, **kwargs) -> _IndexT:
10681068
return self.copy(**kwargs)
10691069

10701070
@final
1071-
def __deepcopy__(self, memo=None):
1071+
def __deepcopy__(self: _IndexT, memo=None) -> _IndexT:
10721072
"""
10731073
Parameters
10741074
----------
@@ -1354,7 +1354,7 @@ def to_series(self, index=None, name: Hashable = None) -> Series:
13541354

13551355
return Series(self._values.copy(), index=index, name=name)
13561356

1357-
def to_frame(self, index: bool = True, name=None) -> DataFrame:
1357+
def to_frame(self, index: bool = True, name: Hashable = None) -> DataFrame:
13581358
"""
13591359
Create a DataFrame with a column containing the Index.
13601360
@@ -1426,7 +1426,7 @@ def name(self):
14261426
return self._name
14271427

14281428
@name.setter
1429-
def name(self, value):
1429+
def name(self, value: Hashable):
14301430
if self._no_setting_name:
14311431
# Used in MultiIndex.levels to avoid silently ignoring name updates.
14321432
raise RuntimeError(
@@ -2367,7 +2367,7 @@ def _is_all_dates(self) -> bool:
23672367

23682368
@cache_readonly
23692369
@final
2370-
def is_all_dates(self):
2370+
def is_all_dates(self) -> bool:
23712371
"""
23722372
Whether or not the index values only consist of dates.
23732373
"""
@@ -3380,7 +3380,7 @@ def get_loc(self, key, method=None, tolerance=None):
33803380
33813381
Returns
33823382
-------
3383-
indexer : ndarray of int
3383+
indexer : np.ndarray[np.intp]
33843384
Integers from 0 to n - 1 indicating that the index at these
33853385
positions matches the corresponding target values. Missing values
33863386
in the target are marked by -1.
@@ -4610,7 +4610,7 @@ def _can_hold_identifiers_and_holds_name(self, name) -> bool:
46104610
return name in self
46114611
return False
46124612

4613-
def append(self, other) -> Index:
4613+
def append(self, other: Index | Sequence[Index]) -> Index:
46144614
"""
46154615
Append a collection of Index options together.
46164616
@@ -4627,7 +4627,9 @@ def append(self, other) -> Index:
46274627
if isinstance(other, (list, tuple)):
46284628
to_concat += list(other)
46294629
else:
4630-
to_concat.append(other)
4630+
# error: Argument 1 to "append" of "list" has incompatible type
4631+
# "Union[Index, Sequence[Index]]"; expected "Index"
4632+
to_concat.append(other) # type: ignore[arg-type]
46314633

46324634
for obj in to_concat:
46334635
if not isinstance(obj, Index):
@@ -5181,11 +5183,11 @@ def set_value(self, arr, key, value):
51815183
51825184
Returns
51835185
-------
5184-
indexer : ndarray of int
5186+
indexer : np.ndarray[np.intp]
51855187
Integers from 0 to n - 1 indicating that the index at these
51865188
positions matches the corresponding target values. Missing values
51875189
in the target are marked by -1.
5188-
missing : ndarray of int
5190+
missing : np.ndarray[np.intp]
51895191
An indexer into the target of the values not found.
51905192
These correspond to the -1 in the indexer array.
51915193
"""
@@ -5227,7 +5229,7 @@ def get_indexer_for(self, target, **kwargs) -> np.ndarray:
52275229
52285230
Returns
52295231
-------
5230-
numpy.ndarray
5232+
np.ndarray[np.intp]
52315233
List of indices.
52325234
"""
52335235
if self._index_as_unique:

pandas/core/indexes/category.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ def reindex(
457457
# in which case we are going to conform to the passed Categorical
458458
new_target = np.asarray(new_target)
459459
if is_categorical_dtype(target):
460-
new_target = Categorical(new_target, dtype=target.dtype)
461-
new_target = type(self)._simple_new(new_target, name=self.name)
460+
cat = Categorical(new_target, dtype=target.dtype)
461+
new_target = type(self)._simple_new(cat, name=self.name)
462462
else:
463463
new_target = Index(new_target, name=self.name)
464464

pandas/core/indexes/datetimes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
391391
# --------------------------------------------------------------------
392392
# Rendering Methods
393393

394-
def _mpl_repr(self):
394+
def _mpl_repr(self) -> np.ndarray:
395395
# how to represent ourselves to matplotlib
396396
return ints_to_pydatetime(self.asi8, self.tz)
397397

@@ -448,7 +448,7 @@ def _maybe_utc_convert(self, other: Index) -> tuple[DatetimeIndex, Index]:
448448

449449
# --------------------------------------------------------------------
450450

451-
def _get_time_micros(self):
451+
def _get_time_micros(self) -> np.ndarray:
452452
"""
453453
Return the number of microseconds since midnight.
454454
@@ -541,7 +541,7 @@ def to_series(self, keep_tz=lib.no_default, index=None, name=None):
541541

542542
return Series(values, index=index, name=name)
543543

544-
def snap(self, freq="S"):
544+
def snap(self, freq="S") -> DatetimeIndex:
545545
"""
546546
Snap time stamps to nearest occurring frequency.
547547
@@ -891,7 +891,7 @@ def indexer_at_time(self, time, asof: bool = False) -> np.ndarray:
891891
else:
892892
time_micros = self._get_time_micros()
893893
micros = _time_to_micros(time)
894-
return (micros == time_micros).nonzero()[0]
894+
return (time_micros == micros).nonzero()[0]
895895

896896
def indexer_between_time(
897897
self, start_time, end_time, include_start: bool = True, include_end: bool = True

pandas/core/indexes/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def from_tuples(
390390
# --------------------------------------------------------------------
391391

392392
@cache_readonly
393-
def _engine(self):
393+
def _engine(self) -> IntervalTree:
394394
left = self._maybe_convert_i8(self.left)
395395
right = self._maybe_convert_i8(self.right)
396396
return IntervalTree(left, right, closed=self.closed)

pandas/core/indexes/multi.py

+1
Original file line numberDiff line numberDiff line change
@@ -2673,6 +2673,7 @@ def _get_indexer(
26732673
limit: int | None = None,
26742674
tolerance=None,
26752675
) -> np.ndarray:
2676+
# returned ndarray is np.intp
26762677

26772678
# empty indexer
26782679
if not len(target):

0 commit comments

Comments
 (0)