Skip to content

Commit cd5eaf5

Browse files
jbrockmendelgasparitiago
authored andcommitted
CLN/TYP: assorted (pandas-dev#43810)
1 parent 515eb19 commit cd5eaf5

File tree

8 files changed

+69
-50
lines changed

8 files changed

+69
-50
lines changed

asv_bench/benchmarks/indexing_engines.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Benchmarks in this fiel depend exclusively on code in _libs/
2+
Benchmarks in this file depend exclusively on code in _libs/
33
44
If a PR does not edit anything in _libs, it is very unlikely that benchmarks
55
in this file will be affected.

pandas/_libs/groupby.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ from pandas._typing import npt
66

77
def group_median_float64(
88
out: np.ndarray, # ndarray[float64_t, ndim=2]
9-
counts: np.ndarray, # ndarray[int64_t]
9+
counts: npt.NDArray[np.int64],
1010
values: np.ndarray, # ndarray[float64_t, ndim=2]
11-
labels: np.ndarray, # ndarray[int64_t]
11+
labels: npt.NDArray[np.int64],
1212
min_count: int = ..., # Py_ssize_t
1313
) -> None: ...
1414
def group_cumprod_float64(
@@ -37,7 +37,7 @@ def group_fillna_indexer(
3737
out: np.ndarray, # ndarray[intp_t]
3838
labels: np.ndarray, # ndarray[int64_t]
3939
sorted_labels: npt.NDArray[np.intp],
40-
mask: np.ndarray, # ndarray[uint8_t]
40+
mask: npt.NDArray[np.uint8],
4141
direction: Literal["ffill", "bfill"],
4242
limit: int, # int64_t
4343
dropna: bool,

pandas/_libs/hashtable.pyi

+31-35
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ from typing import (
55

66
import numpy as np
77

8+
from pandas._typing import npt
9+
810
def unique_label_indices(
911
labels: np.ndarray, # const int64_t[:]
1012
) -> np.ndarray: ...
@@ -19,11 +21,11 @@ class ObjectFactorizer(Factorizer):
1921
uniques: ObjectVector
2022
def factorize(
2123
self,
22-
values: np.ndarray, # ndarray[object]
24+
values: npt.NDArray[np.object_],
2325
sort: bool = ...,
2426
na_sentinel=...,
2527
na_value=...,
26-
) -> np.ndarray: ... # np.ndarray[intp]
28+
) -> npt.NDArray[np.intp]: ...
2729

2830
class Int64Factorizer(Factorizer):
2931
table: Int64HashTable
@@ -34,77 +36,77 @@ class Int64Factorizer(Factorizer):
3436
sort: bool = ...,
3537
na_sentinel=...,
3638
na_value=...,
37-
) -> np.ndarray: ... # np.ndarray[intp]
39+
) -> npt.NDArray[np.intp]: ...
3840

3941
class Int64Vector:
4042
def __init__(self): ...
4143
def __len__(self) -> int: ...
42-
def to_array(self) -> np.ndarray: ... # np.ndarray[np.int64]
44+
def to_array(self) -> npt.NDArray[np.int64]: ...
4345

4446
class Int32Vector:
4547
def __init__(self): ...
4648
def __len__(self) -> int: ...
47-
def to_array(self) -> np.ndarray: ... # np.ndarray[np.int32]
49+
def to_array(self) -> npt.NDArray[np.int32]: ...
4850

4951
class Int16Vector:
5052
def __init__(self): ...
5153
def __len__(self) -> int: ...
52-
def to_array(self) -> np.ndarray: ... # np.ndarray[np.int16]
54+
def to_array(self) -> npt.NDArray[np.int16]: ...
5355

5456
class Int8Vector:
5557
def __init__(self): ...
5658
def __len__(self) -> int: ...
57-
def to_array(self) -> np.ndarray: ... # np.ndarray[np.int8]
59+
def to_array(self) -> npt.NDArray[np.int8]: ...
5860

5961
class UInt64Vector:
6062
def __init__(self): ...
6163
def __len__(self) -> int: ...
62-
def to_array(self) -> np.ndarray: ... # np.ndarray[np.uint64]
64+
def to_array(self) -> npt.NDArray[np.uint64]: ...
6365

6466
class UInt32Vector:
6567
def __init__(self): ...
6668
def __len__(self) -> int: ...
67-
def to_array(self) -> np.ndarray: ... # np.ndarray[np.uint32]
69+
def to_array(self) -> npt.NDArray[np.uint32]: ...
6870

6971
class UInt16Vector:
7072
def __init__(self): ...
7173
def __len__(self) -> int: ...
72-
def to_array(self) -> np.ndarray: ... # np.ndarray[np.uint16]
74+
def to_array(self) -> npt.NDArray[np.uint16]: ...
7375

7476
class UInt8Vector:
7577
def __init__(self): ...
7678
def __len__(self) -> int: ...
77-
def to_array(self) -> np.ndarray: ... # np.ndarray[np.uint8]
79+
def to_array(self) -> npt.NDArray[np.uint8]: ...
7880

7981
class Float64Vector:
8082
def __init__(self): ...
8183
def __len__(self) -> int: ...
82-
def to_array(self) -> np.ndarray: ... # np.ndarray[np.float64]
84+
def to_array(self) -> npt.NDArray[np.float64]: ...
8385

8486
class Float32Vector:
8587
def __init__(self): ...
8688
def __len__(self) -> int: ...
87-
def to_array(self) -> np.ndarray: ... # np.ndarray[np.float32]
89+
def to_array(self) -> npt.NDArray[np.float32]: ...
8890

8991
class Complex128Vector:
9092
def __init__(self): ...
9193
def __len__(self) -> int: ...
92-
def to_array(self) -> np.ndarray: ... # np.ndarray[np.complex128]
94+
def to_array(self) -> npt.NDArray[np.complex128]: ...
9395

9496
class Complex64Vector:
9597
def __init__(self): ...
9698
def __len__(self) -> int: ...
97-
def to_array(self) -> np.ndarray: ... # np.ndarray[np.complex64]
99+
def to_array(self) -> npt.NDArray[np.complex64]: ...
98100

99101
class StringVector:
100102
def __init__(self): ...
101103
def __len__(self) -> int: ...
102-
def to_array(self) -> np.ndarray: ... # np.ndarray[object]
104+
def to_array(self) -> npt.NDArray[np.object_]: ...
103105

104106
class ObjectVector:
105107
def __init__(self): ...
106108
def __len__(self) -> int: ...
107-
def to_array(self) -> np.ndarray: ... # np.ndarray[object]
109+
def to_array(self) -> npt.NDArray[np.object_]: ...
108110

109111
class HashTable:
110112
# NB: The base HashTable class does _not_ actually have these methods;
@@ -131,22 +133,22 @@ class HashTable:
131133
def lookup(
132134
self,
133135
values: np.ndarray, # np.ndarray[subclass-specific]
134-
) -> np.ndarray: ... # np.ndarray[np.intp]
136+
) -> npt.NDArray[np.intp]: ...
135137
def get_labels(
136138
self,
137139
values: np.ndarray, # np.ndarray[subclass-specific]
138140
uniques, # SubclassTypeVector
139141
count_prior: int = ...,
140142
na_sentinel: int = ...,
141143
na_value: object = ...,
142-
) -> np.ndarray: ... # np.ndarray[intp_t]
144+
) -> npt.NDArray[np.intp]: ...
143145
def unique(
144146
self,
145147
values: np.ndarray, # np.ndarray[subclass-specific]
146148
return_inverse: bool = ...,
147149
) -> tuple[
148150
np.ndarray, # np.ndarray[subclass-specific]
149-
np.ndarray, # np.ndarray[np.intp],
151+
npt.NDArray[np.intp],
150152
] | np.ndarray: ... # np.ndarray[subclass-specific]
151153
def _unique(
152154
self,
@@ -159,18 +161,15 @@ class HashTable:
159161
return_inverse: bool = ...,
160162
) -> tuple[
161163
np.ndarray, # np.ndarray[subclass-specific]
162-
np.ndarray, # np.ndarray[np.intp],
164+
npt.NDArray[np.intp],
163165
] | np.ndarray: ... # np.ndarray[subclass-specific]
164166
def factorize(
165167
self,
166168
values: np.ndarray, # np.ndarray[subclass-specific]
167169
na_sentinel: int = ...,
168170
na_value: object = ...,
169171
mask=...,
170-
) -> tuple[
171-
np.ndarray, # np.ndarray[subclass-specific]
172-
np.ndarray, # np.ndarray[np.intp],
173-
]: ...
172+
) -> tuple[np.ndarray, npt.NDArray[np.intp],]: ... # np.ndarray[subclass-specific]
174173

175174
class Complex128HashTable(HashTable): ...
176175
class Complex64HashTable(HashTable): ...
@@ -182,10 +181,7 @@ class Int64HashTable(HashTable):
182181
def get_labels_groupby(
183182
self,
184183
values: np.ndarray, # const int64_t[:]
185-
) -> tuple[
186-
np.ndarray, # np.ndarray[np.intp]
187-
np.ndarray, # np.ndarray[np.int64]
188-
]: ...
184+
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.int64],]: ...
189185

190186
class Int32HashTable(HashTable): ...
191187
class Int16HashTable(HashTable): ...
@@ -200,32 +196,32 @@ class PyObjectHashTable(HashTable): ...
200196
def duplicated_int64(
201197
values: np.ndarray, # const int64_t[:] values
202198
keep: Literal["last", "first", False] = ...,
203-
) -> np.ndarray: ... # np.ndarray[bool]
199+
) -> npt.NDArray[np.bool_]: ...
204200

205201
# TODO: Is it actually bool or is it uint8?
206202

207203
def mode_int64(
208204
values: np.ndarray, # const int64_t[:] values
209205
dropna: bool,
210-
) -> np.ndarray: ... # np.ndarray[np.int64]
206+
) -> npt.NDArray[np.int64]: ...
211207
def value_count_int64(
212208
values: np.ndarray, # const int64_t[:]
213209
dropna: bool,
214-
) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.int64] # np.ndarray[np.int64]
210+
) -> tuple[npt.NDArray[np.int64], npt.NDArray[np.int64]]: ...
215211
def duplicated(
216212
values: np.ndarray,
217213
keep: Literal["last", "first", False] = ...,
218-
) -> np.ndarray: ... # np.ndarray[bool]
214+
) -> npt.NDArray[np.bool_]: ...
219215
def mode(values: np.ndarray, dropna: bool) -> np.ndarray: ...
220216
def value_count(
221217
values: np.ndarray,
222218
dropna: bool,
223-
) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.int64]
219+
) -> tuple[np.ndarray, npt.NDArray[np.int64],]: ... # np.ndarray[same-as-values]
224220

225221
# arr and values should have same dtype
226222
def ismember(
227223
arr: np.ndarray,
228224
values: np.ndarray,
229-
) -> np.ndarray: ... # np.ndarray[bool]
225+
) -> npt.NDArray[np.bool_]: ...
230226
def object_hash(obj) -> int: ...
231227
def objects_are_equal(a, b) -> bool: ...

pandas/_libs/index.pyx

-2
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ cdef class IndexEngine:
322322
Py_ssize_t i, j, n, n_t, n_alloc
323323
bint d_has_nan = False, stargets_has_nan = False, need_nan_check = True
324324

325-
self._ensure_mapping_populated()
326325
values = self.values
327326
stargets = set(targets)
328327

@@ -745,7 +744,6 @@ cdef class BaseMultiIndexCodesEngine:
745744
return self._base.get_loc(self, lab_int)
746745

747746
def get_indexer_non_unique(self, target: np.ndarray) -> np.ndarray:
748-
# target: MultiIndex
749747
indexer = self._base.get_indexer_non_unique(self, target)
750748

751749
return indexer

pandas/core/internals/blocks.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
DtypeObj,
2929
F,
3030
Shape,
31+
npt,
3132
)
3233
from pandas.util._decorators import cache_readonly
3334
from pandas.util._validators import validate_bool_kwarg
@@ -1278,7 +1279,13 @@ def where(self, other, cond, errors="raise") -> list[Block]:
12781279

12791280
return result_blocks
12801281

1281-
def _unstack(self, unstacker, fill_value, new_placement, allow_fill: bool):
1282+
def _unstack(
1283+
self,
1284+
unstacker,
1285+
fill_value,
1286+
new_placement: npt.NDArray[np.intp],
1287+
allow_fill: bool,
1288+
):
12821289
"""
12831290
Return a list of unstacked blocks of self
12841291
@@ -1668,7 +1675,13 @@ def where(self, other, cond, errors="raise") -> list[Block]:
16681675

16691676
return [self.make_block_same_class(result)]
16701677

1671-
def _unstack(self, unstacker, fill_value, new_placement, allow_fill: bool):
1678+
def _unstack(
1679+
self,
1680+
unstacker,
1681+
fill_value,
1682+
new_placement: npt.NDArray[np.intp],
1683+
allow_fill: bool,
1684+
):
16721685
# ExtensionArray-safe unstack.
16731686
# We override ObjectBlock._unstack, which unstacks directly on the
16741687
# values of the array. For EA-backed blocks, this would require

pandas/core/reshape/merge.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -2172,10 +2172,16 @@ def _factorize_keys(
21722172

21732173
rizer = klass(max(len(lk), len(rk)))
21742174

2175-
llab = rizer.factorize(lk)
2176-
rlab = rizer.factorize(rk)
2177-
assert llab.dtype == np.intp, llab.dtype
2178-
assert rlab.dtype == np.intp, rlab.dtype
2175+
# Argument 1 to "factorize" of "ObjectFactorizer" has incompatible type
2176+
# "Union[ndarray[Any, dtype[signedinteger[_64Bit]]],
2177+
# ndarray[Any, dtype[object_]]]"; expected "ndarray[Any, dtype[object_]]"
2178+
llab = rizer.factorize(lk) # type: ignore[arg-type]
2179+
# Argument 1 to "factorize" of "ObjectFactorizer" has incompatible type
2180+
# "Union[ndarray[Any, dtype[signedinteger[_64Bit]]],
2181+
# ndarray[Any, dtype[object_]]]"; expected "ndarray[Any, dtype[object_]]"
2182+
rlab = rizer.factorize(rk) # type: ignore[arg-type]
2183+
assert llab.dtype == np.dtype(np.intp), llab.dtype
2184+
assert rlab.dtype == np.dtype(np.intp), rlab.dtype
21792185

21802186
count = rizer.get_count()
21812187

pandas/core/series.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,9 @@ def __init__(
449449
self.name = name
450450
self._set_axis(0, index, fastpath=True)
451451

452-
def _init_dict(self, data, index=None, dtype: Dtype | None = None):
452+
def _init_dict(
453+
self, data, index: Index | None = None, dtype: DtypeObj | None = None
454+
):
453455
"""
454456
Derive the "_mgr" and "index" attributes of a new Series from a
455457
dictionary input.
@@ -458,16 +460,18 @@ def _init_dict(self, data, index=None, dtype: Dtype | None = None):
458460
----------
459461
data : dict or dict-like
460462
Data used to populate the new Series.
461-
index : Index or index-like, default None
463+
index : Index or None, default None
462464
Index for the new Series: if None, use dict keys.
463-
dtype : dtype, default None
465+
dtype : np.dtype, ExtensionDtype, or None, default None
464466
The dtype for the new Series: if None, infer from data.
465467
466468
Returns
467469
-------
468470
_data : BlockManager for the new Series
469471
index : index for the new Series
470472
"""
473+
keys: Index | tuple
474+
471475
# Looking for NaN in dict doesn't work ({np.nan : 1}[float('nan')]
472476
# raises KeyError), so we iterate the entire dict, and align
473477
if data:

pandas/tests/test_downstream.py

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def df():
3232
# TODO(ArrayManager) dask is still accessing the blocks
3333
# https://github.com/dask/dask/pull/7318
3434
@td.skip_array_manager_not_yet_implemented
35+
@pytest.mark.filterwarnings("ignore:.*64Index is deprecated:FutureWarning")
3536
def test_dask(df):
3637

3738
toolz = import_module("toolz") # noqa
@@ -92,6 +93,7 @@ def test_oo_optimized_datetime_index_unpickle():
9293
# Cython import warning
9394
@pytest.mark.filterwarnings("ignore:pandas.util.testing is deprecated")
9495
@pytest.mark.filterwarnings("ignore:can't:ImportWarning")
96+
@pytest.mark.filterwarnings("ignore:.*64Index is deprecated:FutureWarning")
9597
@pytest.mark.filterwarnings(
9698
# patsy needs to update their imports
9799
"ignore:Using or importing the ABCs from 'collections:DeprecationWarning"

0 commit comments

Comments
 (0)