Skip to content

Commit 71e5d7b

Browse files
authored
TYP: assorted (#43216)
1 parent 425b5f8 commit 71e5d7b

File tree

12 files changed

+69
-68
lines changed

12 files changed

+69
-68
lines changed

pandas/_libs/algos.pyi

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ from typing import Any
33

44
import numpy as np
55

6+
from pandas._typing import npt
7+
68
class Infinity:
79
"""
810
Provide a positive Infinity comparison method for ranking.
@@ -30,7 +32,7 @@ class NegInfinity:
3032
def unique_deltas(
3133
arr: np.ndarray, # const int64_t[:]
3234
) -> np.ndarray: ... # np.ndarray[np.int64, ndim=1]
33-
def is_lexsorted(list_of_arrays: list[np.ndarray]) -> bool: ...
35+
def is_lexsorted(list_of_arrays: list[npt.NDArray[np.int64]]) -> bool: ...
3436
def groupsort_indexer(
3537
index: np.ndarray, # const int64_t[:]
3638
ngroups: int,
@@ -146,18 +148,18 @@ def diff_2d(
146148
axis: int,
147149
datetimelike: bool = ...,
148150
) -> None: ...
149-
def ensure_platform_int(arr: object) -> np.ndarray: ...
150-
def ensure_object(arr: object) -> np.ndarray: ...
151-
def ensure_float64(arr: object, copy=True) -> np.ndarray: ...
152-
def ensure_float32(arr: object, copy=True) -> np.ndarray: ...
153-
def ensure_int8(arr: object, copy=True) -> np.ndarray: ...
154-
def ensure_int16(arr: object, copy=True) -> np.ndarray: ...
155-
def ensure_int32(arr: object, copy=True) -> np.ndarray: ...
156-
def ensure_int64(arr: object, copy=True) -> np.ndarray: ...
157-
def ensure_uint8(arr: object, copy=True) -> np.ndarray: ...
158-
def ensure_uint16(arr: object, copy=True) -> np.ndarray: ...
159-
def ensure_uint32(arr: object, copy=True) -> np.ndarray: ...
160-
def ensure_uint64(arr: object, copy=True) -> np.ndarray: ...
151+
def ensure_platform_int(arr: object) -> npt.NDArray[np.intp]: ...
152+
def ensure_object(arr: object) -> npt.NDArray[np.object_]: ...
153+
def ensure_float64(arr: object, copy=True) -> npt.NDArray[np.float64]: ...
154+
def ensure_float32(arr: object, copy=True) -> npt.NDArray[np.float32]: ...
155+
def ensure_int8(arr: object, copy=True) -> npt.NDArray[np.int8]: ...
156+
def ensure_int16(arr: object, copy=True) -> npt.NDArray[np.int16]: ...
157+
def ensure_int32(arr: object, copy=True) -> npt.NDArray[np.int32]: ...
158+
def ensure_int64(arr: object, copy=True) -> npt.NDArray[np.int64]: ...
159+
def ensure_uint8(arr: object, copy=True) -> npt.NDArray[np.uint8]: ...
160+
def ensure_uint16(arr: object, copy=True) -> npt.NDArray[np.uint16]: ...
161+
def ensure_uint32(arr: object, copy=True) -> npt.NDArray[np.uint32]: ...
162+
def ensure_uint64(arr: object, copy=True) -> npt.NDArray[np.uint64]: ...
161163
def take_1d_int8_int8(
162164
values: np.ndarray, indexer: np.ndarray, out: np.ndarray, fill_value=...
163165
) -> None: ...

pandas/_libs/algos.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ cpdef ndarray[int64_t, ndim=1] unique_deltas(const int64_t[:] arr):
122122
123123
Parameters
124124
----------
125-
arr : ndarray[in64_t]
125+
arr : ndarray[int64_t]
126126
127127
Returns
128128
-------

pandas/_libs/hashing.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import numpy as np
22

3+
from pandas._typing import npt
4+
35
def hash_object_array(
4-
arr: np.ndarray, # np.ndarray[object]
6+
arr: npt.NDArray[np.object_],
57
key: str,
68
encoding: str = ...,
7-
) -> np.ndarray: ... # np.ndarray[np.uint64]
9+
) -> npt.NDArray[np.uint64]: ...

pandas/_libs/index.pyi

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import numpy as np
22

3+
from pandas._typing import npt
4+
35
class IndexEngine:
46
over_size_threshold: bool
57
def __init__(self, vgetter, n: int): ...
@@ -16,21 +18,18 @@ class IndexEngine:
1618
def is_monotonic_decreasing(self) -> bool: ...
1719
def get_backfill_indexer(
1820
self, other: np.ndarray, limit: int | None = ...
19-
) -> np.ndarray: ...
21+
) -> npt.NDArray[np.intp]: ...
2022
def get_pad_indexer(
2123
self, other: np.ndarray, limit: int | None = ...
22-
) -> np.ndarray: ...
24+
) -> npt.NDArray[np.intp]: ...
2325
@property
2426
def is_mapping_populated(self) -> bool: ...
2527
def clear_mapping(self): ...
26-
def get_indexer(self, values: np.ndarray) -> np.ndarray: ... # np.ndarray[np.intp]
28+
def get_indexer(self, values: np.ndarray) -> npt.NDArray[np.intp]: ...
2729
def get_indexer_non_unique(
2830
self,
2931
targets: np.ndarray,
30-
) -> tuple[
31-
np.ndarray, # np.ndarray[np.intp]
32-
np.ndarray, # np.ndarray[np.intp]
33-
]: ...
32+
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ...
3433

3534
class Float64Engine(IndexEngine): ...
3635
class Float32Engine(IndexEngine): ...
@@ -58,8 +57,8 @@ class BaseMultiIndexCodesEngine:
5857
): ...
5958
def get_indexer(
6059
self,
61-
target: np.ndarray, # np.ndarray[object]
62-
) -> np.ndarray: ... # np.ndarray[np.intp]
60+
target: npt.NDArray[np.object_],
61+
) -> npt.NDArray[np.intp]: ...
6362
def _extract_level_codes(self, target: object): ...
6463
def get_indexer_with_fill(
6564
self,

pandas/core/algorithms.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def unique(values):
433433
unique1d = unique
434434

435435

436-
def isin(comps: AnyArrayLike, values: AnyArrayLike) -> np.ndarray:
436+
def isin(comps: AnyArrayLike, values: AnyArrayLike) -> npt.NDArray[np.bool_]:
437437
"""
438438
Compute the isin boolean array.
439439
@@ -903,7 +903,7 @@ def value_counts_arraylike(values, dropna: bool):
903903

904904
def duplicated(
905905
values: ArrayLike, keep: Literal["first", "last", False] = "first"
906-
) -> np.ndarray:
906+
) -> npt.NDArray[np.bool_]:
907907
"""
908908
Return boolean ndarray denoting duplicate values.
909909
@@ -1032,8 +1032,8 @@ def rank(
10321032
def checked_add_with_arr(
10331033
arr: np.ndarray,
10341034
b,
1035-
arr_mask: np.ndarray | None = None,
1036-
b_mask: np.ndarray | None = None,
1035+
arr_mask: npt.NDArray[np.bool_] | None = None,
1036+
b_mask: npt.NDArray[np.bool_] | None = None,
10371037
) -> np.ndarray:
10381038
"""
10391039
Perform array addition that checks for underflow and overflow.

pandas/core/arrays/datetimelike.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
DtypeObj,
5050
NpDtype,
5151
PositionalIndexer2D,
52+
npt,
5253
)
5354
from pandas.compat.numpy import function as nv
5455
from pandas.errors import (
@@ -274,7 +275,7 @@ def __iter__(self):
274275
return (self._box_func(v) for v in self.asi8)
275276

276277
@property
277-
def asi8(self) -> np.ndarray:
278+
def asi8(self) -> npt.NDArray[np.int64]:
278279
"""
279280
Integer representation of the values.
280281
@@ -774,7 +775,7 @@ def map(self, mapper):
774775

775776
return Index(self).map(mapper).array
776777

777-
def isin(self, values) -> np.ndarray:
778+
def isin(self, values) -> npt.NDArray[np.bool_]:
778779
"""
779780
Compute boolean array of whether each value is found in the
780781
passed set of values.
@@ -830,11 +831,11 @@ def isin(self, values) -> np.ndarray:
830831
# ------------------------------------------------------------------
831832
# Null Handling
832833

833-
def isna(self) -> np.ndarray:
834+
def isna(self) -> npt.NDArray[np.bool_]:
834835
return self._isnan
835836

836837
@property # NB: override with cache_readonly in immutable subclasses
837-
def _isnan(self) -> np.ndarray:
838+
def _isnan(self) -> npt.NDArray[np.bool_]:
838839
"""
839840
return if each value is nan
840841
"""
@@ -1535,7 +1536,7 @@ class DatelikeOps(DatetimeLikeArrayMixin):
15351536
URL="https://docs.python.org/3/library/datetime.html"
15361537
"#strftime-and-strptime-behavior"
15371538
)
1538-
def strftime(self, date_format: str) -> np.ndarray:
1539+
def strftime(self, date_format: str) -> npt.NDArray[np.object_]:
15391540
"""
15401541
Convert to Index using specified date_format.
15411542
@@ -1551,7 +1552,7 @@ def strftime(self, date_format: str) -> np.ndarray:
15511552
15521553
Returns
15531554
-------
1554-
ndarray
1555+
ndarray[object]
15551556
NumPy ndarray of formatted strings.
15561557
15571558
See Also

pandas/core/arrays/datetimes.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
to_offset,
3939
tzconversion,
4040
)
41+
from pandas._typing import npt
4142
from pandas.errors import PerformanceWarning
4243

4344
from pandas.core.dtypes.cast import astype_dt64_to_dt64tz
@@ -656,7 +657,7 @@ def astype(self, dtype, copy: bool = True):
656657
@dtl.ravel_compat
657658
def _format_native_types(
658659
self, na_rep="NaT", date_format=None, **kwargs
659-
) -> np.ndarray:
660+
) -> npt.NDArray[np.object_]:
660661
from pandas.io.formats.format import get_format_datetime64_from_values
661662

662663
fmt = get_format_datetime64_from_values(self, date_format)
@@ -1045,7 +1046,7 @@ def tz_localize(self, tz, ambiguous="raise", nonexistent="raise") -> DatetimeArr
10451046
# ----------------------------------------------------------------
10461047
# Conversion Methods - Vectorized analogues of Timestamp methods
10471048

1048-
def to_pydatetime(self) -> np.ndarray:
1049+
def to_pydatetime(self) -> npt.NDArray[np.object_]:
10491050
"""
10501051
Return Datetime Array/Index as object ndarray of datetime.datetime
10511052
objects.
@@ -1262,7 +1263,7 @@ def day_name(self, locale=None):
12621263
return result
12631264

12641265
@property
1265-
def time(self) -> np.ndarray:
1266+
def time(self) -> npt.NDArray[np.object_]:
12661267
"""
12671268
Returns numpy array of datetime.time. The time part of the Timestamps.
12681269
"""
@@ -1274,15 +1275,15 @@ def time(self) -> np.ndarray:
12741275
return ints_to_pydatetime(timestamps, box="time")
12751276

12761277
@property
1277-
def timetz(self) -> np.ndarray:
1278+
def timetz(self) -> npt.NDArray[np.object_]:
12781279
"""
12791280
Returns numpy array of datetime.time also containing timezone
12801281
information. The time part of the Timestamps.
12811282
"""
12821283
return ints_to_pydatetime(self.asi8, self.tz, box="time")
12831284

12841285
@property
1285-
def date(self) -> np.ndarray:
1286+
def date(self) -> npt.NDArray[np.object_]:
12861287
"""
12871288
Returns numpy array of python datetime.date objects (namely, the date
12881289
part of Timestamps without timezone information).

pandas/core/groupby/ops.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -947,9 +947,7 @@ def agg_series(
947947
out = npvalues
948948
return out
949949

950-
def _aggregate_series_fast(self, obj: Series, func: F) -> np.ndarray:
951-
# -> np.ndarray[object]
952-
950+
def _aggregate_series_fast(self, obj: Series, func: F) -> npt.NDArray[np.object_]:
953951
# At this point we have already checked that
954952
# - obj.index is not a MultiIndex
955953
# - obj is backed by an ndarray, not ExtensionArray
@@ -967,8 +965,9 @@ def _aggregate_series_fast(self, obj: Series, func: F) -> np.ndarray:
967965
return result
968966

969967
@final
970-
def _aggregate_series_pure_python(self, obj: Series, func: F) -> np.ndarray:
971-
# -> np.ndarray[object]
968+
def _aggregate_series_pure_python(
969+
self, obj: Series, func: F
970+
) -> npt.NDArray[np.object_]:
972971
ids, _, ngroups = self.group_info
973972

974973
counts = np.zeros(ngroups, dtype=int)
@@ -1190,12 +1189,12 @@ def __init__(
11901189
assert isinstance(axis, int), axis
11911190

11921191
@cache_readonly
1193-
def slabels(self) -> np.ndarray: # np.ndarray[np.intp]
1192+
def slabels(self) -> npt.NDArray[np.intp]:
11941193
# Sorted labels
11951194
return self.labels.take(self._sort_idx)
11961195

11971196
@cache_readonly
1198-
def _sort_idx(self) -> np.ndarray: # np.ndarray[np.intp]
1197+
def _sort_idx(self) -> npt.NDArray[np.intp]:
11991198
# Counting sort indexer
12001199
return get_group_index_sorter(self.labels, self.ngroups)
12011200

pandas/core/indexes/numeric.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pandas._typing import (
1616
Dtype,
1717
DtypeObj,
18+
npt,
1819
)
1920
from pandas.util._decorators import (
2021
cache_readonly,
@@ -411,7 +412,7 @@ class IntegerIndex(NumericIndex):
411412
_is_backward_compat_public_numeric_index: bool = False
412413

413414
@property
414-
def asi8(self) -> np.ndarray:
415+
def asi8(self) -> npt.NDArray[np.int64]:
415416
# do not cache or you'll create a memory leak
416417
warnings.warn(
417418
"Index.asi8 is deprecated and will be removed in a future version.",

pandas/core/missing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
ArrayLike,
2424
Axis,
2525
F,
26+
npt,
2627
)
2728
from pandas.compat._optional import import_optional_dependency
2829

@@ -57,7 +58,7 @@ def check_value_size(value, mask: np.ndarray, length: int):
5758
return value
5859

5960

60-
def mask_missing(arr: ArrayLike, values_to_mask) -> np.ndarray:
61+
def mask_missing(arr: ArrayLike, values_to_mask) -> npt.NDArray[np.bool_]:
6162
"""
6263
Return a masking array of same size/shape as arr
6364
with entries equaling any member of values_to_mask set to True

pandas/core/reshape/merge.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -931,17 +931,16 @@ def _maybe_add_join_keys(
931931
else:
932932
result.insert(i, name or f"key_{i}", key_col)
933933

934-
def _get_join_indexers(self) -> tuple[np.ndarray, np.ndarray]:
934+
def _get_join_indexers(self) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
935935
"""return the join indexers"""
936-
# Both returned ndarrays are np.intp
937936
return get_join_indexers(
938937
self.left_join_keys, self.right_join_keys, sort=self.sort, how=self.how
939938
)
940939

941940
def _get_join_info(
942941
self,
943-
) -> tuple[Index, np.ndarray | None, np.ndarray | None]:
944-
# Both returned ndarrays are np.intp (if not None)
942+
) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
943+
945944
left_ax = self.left.axes[self.axis]
946945
right_ax = self.right.axes[self.axis]
947946

@@ -1892,8 +1891,7 @@ def _get_merge_keys(self):
18921891

18931892
return left_join_keys, right_join_keys, join_names
18941893

1895-
def _get_join_indexers(self) -> tuple[np.ndarray, np.ndarray]:
1896-
# Both returned ndarrays are np.intp
1894+
def _get_join_indexers(self) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
18971895
"""return the join indexers"""
18981896

18991897
def flip(xs) -> np.ndarray:
@@ -1987,8 +1985,7 @@ def flip(xs) -> np.ndarray:
19871985

19881986
def _get_multiindex_indexer(
19891987
join_keys, index: MultiIndex, sort: bool
1990-
) -> tuple[np.ndarray, np.ndarray]:
1991-
# Both returned ndarrays are np.intp
1988+
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
19921989

19931990
# left & right join labels and num. of levels at each location
19941991
mapped = (
@@ -2026,17 +2023,15 @@ def _get_multiindex_indexer(
20262023

20272024
def _get_single_indexer(
20282025
join_key, index: Index, sort: bool = False
2029-
) -> tuple[np.ndarray, np.ndarray]:
2030-
# Both returned ndarrays are np.intp
2026+
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
20312027
left_key, right_key, count = _factorize_keys(join_key, index._values, sort=sort)
20322028

20332029
return libjoin.left_outer_join(left_key, right_key, count, sort=sort)
20342030

20352031

20362032
def _left_join_on_index(
20372033
left_ax: Index, right_ax: Index, join_keys, sort: bool = False
2038-
) -> tuple[Index, np.ndarray | None, np.ndarray]:
2039-
# Both returned ndarrays are np.intp (if not None)
2034+
) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp]]:
20402035
if len(join_keys) > 1:
20412036
if not (
20422037
isinstance(right_ax, MultiIndex) and len(join_keys) == right_ax.nlevels
@@ -2205,8 +2200,7 @@ def _factorize_keys(
22052200

22062201
def _sort_labels(
22072202
uniques: np.ndarray, left: np.ndarray, right: np.ndarray
2208-
) -> tuple[np.ndarray, np.ndarray]:
2209-
# Both returned ndarrays are np.intp
2203+
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
22102204

22112205
llength = len(left)
22122206
labels = np.concatenate([left, right])

0 commit comments

Comments
 (0)