Skip to content

Commit d603d43

Browse files
authored
TYP: Ignore numpy related issues (pandas-dev#45244)
1 parent e5cf25b commit d603d43

27 files changed

+147
-64
lines changed

environment.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ channels:
33
- conda-forge
44
dependencies:
55
# required
6-
- numpy>=1.18.5, <1.22.0
6+
- numpy>=1.18.5
77
- python=3.8
88
- python-dateutil>=2.8.1
99
- pytz

pandas/core/algorithms.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,12 @@ def factorize(
770770
# na_value is set based on the dtype of uniques, and compat set to False is
771771
# because we do not want na_value to be 0 for integers
772772
na_value = na_value_for_dtype(uniques.dtype, compat=False)
773-
uniques = np.append(uniques, [na_value])
773+
# Argument 2 to "append" has incompatible type "List[Union[str, float, Period,
774+
# Timestamp, Timedelta, Any]]"; expected "Union[_SupportsArray[dtype[Any]],
775+
# _NestedSequence[_SupportsArray[dtype[Any]]]
776+
# , bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int,
777+
# float, complex, str, bytes]]]" [arg-type]
778+
uniques = np.append(uniques, [na_value]) # type: ignore[arg-type]
774779
codes = np.where(code_is_na, len(uniques) - 1, codes)
775780

776781
uniques = _reconstruct_data(uniques, dtype, original)
@@ -1069,7 +1074,12 @@ def checked_add_with_arr(
10691074
elif arr_mask is not None:
10701075
not_nan = np.logical_not(arr_mask)
10711076
elif b_mask is not None:
1072-
not_nan = np.logical_not(b2_mask)
1077+
# Argument 1 to "__call__" of "_UFunc_Nin1_Nout1" has incompatible type
1078+
# "Optional[ndarray[Any, dtype[bool_]]]"; expected
1079+
# "Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[An
1080+
# y]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool,
1081+
# int, float, complex, str, bytes]]]" [arg-type]
1082+
not_nan = np.logical_not(b2_mask) # type: ignore[arg-type]
10731083
else:
10741084
not_nan = np.empty(arr.shape, dtype=bool)
10751085
not_nan.fill(True)

pandas/core/arraylike.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,7 @@ def array_ufunc(self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any)
265265
return result
266266

267267
# Determine if we should defer.
268-
269-
# error: "Type[ndarray]" has no attribute "__array_ufunc__"
270-
no_defer = (
271-
np.ndarray.__array_ufunc__, # type: ignore[attr-defined]
272-
cls.__array_ufunc__,
273-
)
268+
no_defer = (np.ndarray.__array_ufunc__, cls.__array_ufunc__)
274269

275270
for item in inputs:
276271
higher_priority = (

pandas/core/arrays/interval.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,13 @@ def isin(self, values) -> np.ndarray:
16391639
# complex128 ndarray is much more performant.
16401640
left = self._combined.view("complex128")
16411641
right = values._combined.view("complex128")
1642-
return np.in1d(left, right)
1642+
# Argument 1 to "in1d" has incompatible type "Union[ExtensionArray,
1643+
# ndarray[Any, Any], ndarray[Any, dtype[Any]]]"; expected
1644+
# "Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[
1645+
# dtype[Any]]], bool, int, float, complex, str, bytes,
1646+
# _NestedSequence[Union[bool, int, float, complex, str, bytes]]]"
1647+
# [arg-type]
1648+
return np.in1d(left, right) # type: ignore[arg-type]
16431649

16441650
elif needs_i8_conversion(self.left.dtype) ^ needs_i8_conversion(
16451651
values.left.dtype

pandas/core/arrays/masked.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,11 @@ def any(self, *, skipna: bool = True, **kwargs):
943943
nv.validate_any((), kwargs)
944944

945945
values = self._data.copy()
946-
np.putmask(values, self._mask, self._falsey_value)
946+
# Argument 3 to "putmask" has incompatible type "object"; expected
947+
# "Union[_SupportsArray[dtype[Any]], _NestedSequence[
948+
# _SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _Nested
949+
# Sequence[Union[bool, int, float, complex, str, bytes]]]" [arg-type]
950+
np.putmask(values, self._mask, self._falsey_value) # type: ignore[arg-type]
947951
result = values.any()
948952
if skipna:
949953
return result
@@ -1019,7 +1023,11 @@ def all(self, *, skipna: bool = True, **kwargs):
10191023
nv.validate_all((), kwargs)
10201024

10211025
values = self._data.copy()
1022-
np.putmask(values, self._mask, self._truthy_value)
1026+
# Argument 3 to "putmask" has incompatible type "object"; expected
1027+
# "Union[_SupportsArray[dtype[Any]], _NestedSequence[
1028+
# _SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _Neste
1029+
# dSequence[Union[bool, int, float, complex, str, bytes]]]" [arg-type]
1030+
np.putmask(values, self._mask, self._truthy_value) # type: ignore[arg-type]
10231031
result = values.all()
10241032

10251033
if skipna:

pandas/core/arrays/sparse/array.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,8 @@ def fillna(
772772
elif method is not None:
773773
msg = "fillna with 'method' requires high memory usage."
774774
warnings.warn(msg, PerformanceWarning)
775-
new_values = np.asarray(self)
775+
# Need type annotation for "new_values" [var-annotated]
776+
new_values = np.asarray(self) # type: ignore[var-annotated]
776777
# interpolate_2d modifies new_values inplace
777778
interpolate_2d(new_values, method=method, limit=limit)
778779
return type(self)(new_values, fill_value=self.fill_value)
@@ -924,7 +925,15 @@ def __getitem__(
924925
if is_integer(key):
925926
return self._get_val_at(key)
926927
elif isinstance(key, tuple):
927-
data_slice = self.to_dense()[key]
928+
# Invalid index type "Tuple[Union[int, ellipsis], ...]" for
929+
# "ndarray[Any, Any]"; expected type "Union[SupportsIndex,
930+
# _SupportsArray[dtype[Union[bool_, integer[Any]]]], _NestedSequence[_Su
931+
# pportsArray[dtype[Union[bool_, integer[Any]]]]],
932+
# _NestedSequence[Union[bool, int]], Tuple[Union[SupportsIndex,
933+
# _SupportsArray[dtype[Union[bool_, integer[Any]]]],
934+
# _NestedSequence[_SupportsArray[dtype[Union[bool_, integer[Any]]]]], _N
935+
# estedSequence[Union[bool, int]]], ...]]" [index]
936+
data_slice = self.to_dense()[key] # type: ignore[index]
928937
elif isinstance(key, slice):
929938

930939
# Avoid densifying when handling contiguous slices
@@ -1164,7 +1173,9 @@ def _concat_same_type(
11641173

11651174
data = np.concatenate(values)
11661175
indices_arr = np.concatenate(indices)
1167-
sp_index = IntIndex(length, indices_arr)
1176+
# Argument 2 to "IntIndex" has incompatible type "ndarray[Any,
1177+
# dtype[signedinteger[_32Bit]]]"; expected "Sequence[int]"
1178+
sp_index = IntIndex(length, indices_arr) # type: ignore[arg-type]
11681179

11691180
else:
11701181
# when concatenating block indices, we don't claim that you'll
@@ -1342,7 +1353,8 @@ def __setstate__(self, state):
13421353
if isinstance(state, tuple):
13431354
# Compat for pandas < 0.24.0
13441355
nd_state, (fill_value, sp_index) = state
1345-
sparse_values = np.array([])
1356+
# Need type annotation for "sparse_values" [var-annotated]
1357+
sparse_values = np.array([]) # type: ignore[var-annotated]
13461358
sparse_values.__setstate__(nd_state)
13471359

13481360
self._sparse_values = sparse_values

pandas/core/dtypes/astype.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ def astype_nansafe(
111111
return lib.ensure_string_array(arr, skipna=skipna, convert_na_value=False)
112112

113113
elif is_datetime64_dtype(arr.dtype):
114-
# Non-overlapping equality check (left operand type: "dtype[Any]", right
115-
# operand type: "Type[signedinteger[Any]]")
116-
if dtype == np.int64: # type: ignore[comparison-overlap]
114+
if dtype == np.int64:
117115
warnings.warn(
118116
f"casting {arr.dtype} values to int64 with .astype(...) "
119117
"is deprecated and will raise in a future version. "
@@ -132,9 +130,7 @@ def astype_nansafe(
132130
raise TypeError(f"cannot astype a datetimelike from [{arr.dtype}] to [{dtype}]")
133131

134132
elif is_timedelta64_dtype(arr.dtype):
135-
# error: Non-overlapping equality check (left operand type: "dtype[Any]", right
136-
# operand type: "Type[signedinteger[Any]]")
137-
if dtype == np.int64: # type: ignore[comparison-overlap]
133+
if dtype == np.int64:
138134
warnings.warn(
139135
f"casting {arr.dtype} values to int64 with .astype(...) "
140136
"is deprecated and will raise in a future version. "

pandas/core/dtypes/common.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,7 @@ def is_string_or_object_np_dtype(dtype: np.dtype) -> bool:
534534
"""
535535
Faster alternative to is_string_dtype, assumes we have a np.dtype object.
536536
"""
537-
# error: Non-overlapping equality check (left operand type: "dtype[Any]",
538-
# right operand type: "Type[object]")
539-
return dtype == object or dtype.kind in "SU" # type: ignore[comparison-overlap]
537+
return dtype == object or dtype.kind in "SU"
540538

541539

542540
def is_string_dtype(arr_or_dtype) -> bool:

pandas/core/frame.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2428,7 +2428,9 @@ def to_records(
24282428
if dtype_mapping is None:
24292429
formats.append(v.dtype)
24302430
elif isinstance(dtype_mapping, (type, np.dtype, str)):
2431-
formats.append(dtype_mapping)
2431+
# Argument 1 to "append" of "list" has incompatible type
2432+
# "Union[type, dtype[Any], str]"; expected "dtype[_SCT]" [arg-type]
2433+
formats.append(dtype_mapping) # type: ignore[arg-type]
24322434
else:
24332435
element = "row" if i < index_len else "column"
24342436
msg = f"Invalid dtype {dtype_mapping} specified for {element} {name}"

pandas/core/generic.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6979,8 +6979,7 @@ def interpolate(
69796979
# create/use the index
69806980
if method == "linear":
69816981
# prior default
6982-
index = np.arange(len(obj.index))
6983-
index = Index(index)
6982+
index = Index(np.arange(len(obj.index)))
69846983
else:
69856984
index = obj.index
69866985
methods = {"index", "values", "nearest", "time"}

pandas/core/groupby/ops.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ def _get_cython_function(
158158
f = getattr(libgroupby, ftype)
159159
if is_numeric:
160160
return f
161-
# error: Non-overlapping equality check (left operand type: "dtype[Any]", right
162-
# operand type: "Literal['object']")
163-
elif dtype == object: # type: ignore[comparison-overlap]
161+
elif dtype == object:
164162
if "object" not in f.__signatures__:
165163
# raise NotImplementedError here rather than TypeError later
166164
raise NotImplementedError(
@@ -807,6 +805,7 @@ def size(self) -> Series:
807805
Compute group sizes.
808806
"""
809807
ids, _, ngroups = self.group_info
808+
out: np.ndarray | list
810809
if ngroups:
811810
out = np.bincount(ids[ids != -1], minlength=ngroups)
812811
else:

pandas/core/indexes/base.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -4535,7 +4535,12 @@ def _join_non_unique(
45354535
right = other._values.take(right_idx)
45364536

45374537
if isinstance(join_array, np.ndarray):
4538-
np.putmask(join_array, mask, right)
4538+
# Argument 3 to "putmask" has incompatible type "Union[ExtensionArray,
4539+
# ndarray[Any, Any]]"; expected "Union[_SupportsArray[dtype[Any]],
4540+
# _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, f
4541+
# loat, complex, str, bytes, _NestedSequence[Union[bool, int, float,
4542+
# complex, str, bytes]]]" [arg-type]
4543+
np.putmask(join_array, mask, right) # type: ignore[arg-type]
45394544
else:
45404545
join_array._putmask(mask, right)
45414546

@@ -5042,9 +5047,11 @@ def __getitem__(self, key):
50425047
if result.ndim > 1:
50435048
deprecate_ndim_indexing(result)
50445049
if hasattr(result, "_ndarray"):
5050+
# error: Item "ndarray[Any, Any]" of "Union[ExtensionArray,
5051+
# ndarray[Any, Any]]" has no attribute "_ndarray" [union-attr]
50455052
# i.e. NDArrayBackedExtensionArray
50465053
# Unpack to ndarray for MPL compat
5047-
return result._ndarray
5054+
return result._ndarray # type: ignore[union-attr]
50485055
return result
50495056

50505057
# NB: Using _constructor._simple_new would break if MultiIndex
@@ -6531,6 +6538,7 @@ def delete(self: _IndexT, loc) -> _IndexT:
65316538
Index(['b'], dtype='object')
65326539
"""
65336540
values = self._values
6541+
res_values: ArrayLike
65346542
if isinstance(values, np.ndarray):
65356543
# TODO(__array_function__): special casing will be unnecessary
65366544
res_values = np.delete(values, loc)
@@ -6584,7 +6592,9 @@ def insert(self, loc: int, item) -> Index:
65846592
new_values = np.insert(arr, loc, casted)
65856593

65866594
else:
6587-
new_values = np.insert(arr, loc, None)
6595+
# No overload variant of "insert" matches argument types
6596+
# "ndarray[Any, Any]", "int", "None" [call-overload]
6597+
new_values = np.insert(arr, loc, None) # type: ignore[call-overload]
65886598
loc = loc if loc >= 0 else loc - 1
65896599
new_values[loc] = item
65906600

pandas/core/indexes/multi.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,9 @@ def _validate_codes(self, level: list, code: list):
365365
"""
366366
null_mask = isna(level)
367367
if np.any(null_mask):
368-
code = np.where(null_mask[code], -1, code)
368+
# Incompatible types in assignment (expression has type
369+
# "ndarray[Any, dtype[Any]]", variable has type "List[Any]")
370+
code = np.where(null_mask[code], -1, code) # type: ignore[assignment]
369371
return code
370372

371373
def _verify_integrity(self, codes: list | None = None, levels: list | None = None):
@@ -1086,7 +1088,9 @@ def _engine(self):
10861088
# equivalent to sorting lexicographically the codes themselves. Notice
10871089
# that each level needs to be shifted by the number of bits needed to
10881090
# represent the _previous_ ones:
1089-
offsets = np.concatenate([lev_bits[1:], [0]]).astype("uint64")
1091+
offsets = np.concatenate([lev_bits[1:], [0]]).astype( # type: ignore[arg-type]
1092+
"uint64"
1093+
)
10901094

10911095
# Check the total number of bits needed for our representation:
10921096
if lev_bits[0] > 64:
@@ -1564,7 +1568,12 @@ def is_monotonic_increasing(self) -> bool:
15641568
self._get_level_values(i)._values for i in reversed(range(len(self.levels)))
15651569
]
15661570
try:
1567-
sort_order = np.lexsort(values)
1571+
# Argument 1 to "lexsort" has incompatible type "List[Union[ExtensionArray,
1572+
# ndarray[Any, Any]]]"; expected "Union[_SupportsArray[dtype[Any]],
1573+
# _NestedSequence[_SupportsArray[dtype[Any]]], bool,
1574+
# int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float,
1575+
# complex, str, bytes]]]" [arg-type]
1576+
sort_order = np.lexsort(values) # type: ignore[arg-type]
15681577
return Index(sort_order).is_monotonic
15691578
except TypeError:
15701579

pandas/core/indexing.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,9 @@ def _ensure_iterable_column_indexer(self, column_indexer):
20212021
if is_integer(column_indexer):
20222022
ilocs = [column_indexer]
20232023
elif isinstance(column_indexer, slice):
2024-
ilocs = np.arange(len(self.obj.columns))[column_indexer]
2024+
ilocs = np.arange(len(self.obj.columns))[ # type: ignore[assignment]
2025+
column_indexer
2026+
]
20252027
elif isinstance(column_indexer, np.ndarray) and is_bool_dtype(
20262028
column_indexer.dtype
20272029
):
@@ -2083,7 +2085,11 @@ def ravel(i):
20832085
# single indexer
20842086
if len(indexer) > 1 and not multiindex_indexer:
20852087
len_indexer = len(indexer[1])
2086-
ser = np.tile(ser, len_indexer).reshape(len_indexer, -1).T
2088+
ser = (
2089+
np.tile(ser, len_indexer) # type: ignore[assignment]
2090+
.reshape(len_indexer, -1)
2091+
.T
2092+
)
20872093

20882094
return ser
20892095

pandas/core/internals/blocks.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,13 @@ def delete(self, loc) -> None:
386386
"""
387387
Delete given loc(-s) from block in-place.
388388
"""
389-
self.values = np.delete(self.values, loc, 0)
389+
# Argument 1 to "delete" has incompatible type "Union[ndarray[Any, Any],
390+
# ExtensionArray]"; expected "Union[_SupportsArray[dtype[Any]],
391+
# Sequence[_SupportsArray[dtype[Any]]], Sequence[Sequence
392+
# [_SupportsArray[dtype[Any]]]], Sequence[Sequence[Sequence[
393+
# _SupportsArray[dtype[Any]]]]], Sequence[Sequence[Sequence[Sequence[
394+
# _SupportsArray[dtype[Any]]]]]]]" [arg-type]
395+
self.values = np.delete(self.values, loc, 0) # type: ignore[arg-type]
390396
self.mgr_locs = self._mgr_locs.delete(loc)
391397
try:
392398
self._cache.clear()

pandas/core/internals/managers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1125,8 +1125,8 @@ def value_getitem(placement):
11251125
unfit_mgr_locs = []
11261126
unfit_val_locs = []
11271127
removed_blknos = []
1128-
for blkno, val_locs in libinternals.get_blkno_placements(blknos, group=True):
1129-
blk = self.blocks[blkno]
1128+
for blkno_l, val_locs in libinternals.get_blkno_placements(blknos, group=True):
1129+
blk = self.blocks[blkno_l]
11301130
blk_locs = blklocs[val_locs.indexer]
11311131
if inplace and blk.should_store(value):
11321132
blk.set_inplace(blk_locs, value_getitem(val_locs))
@@ -1136,7 +1136,7 @@ def value_getitem(placement):
11361136

11371137
# If all block items are unfit, schedule the block for removal.
11381138
if len(val_locs) == len(blk.mgr_locs):
1139-
removed_blknos.append(blkno)
1139+
removed_blknos.append(blkno_l)
11401140
else:
11411141
blk.delete(blk_locs)
11421142
self._blklocs[blk.mgr_locs.indexer] = np.arange(len(blk))

pandas/core/internals/ops.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ def _get_same_shape_values(
125125
# argument type "Tuple[Union[ndarray, slice], slice]"
126126
lvals = lvals[rblk.mgr_locs.indexer, :] # type: ignore[call-overload]
127127
assert lvals.shape[0] == 1, lvals.shape
128-
# error: No overload variant of "__getitem__" of "ExtensionArray" matches
129-
# argument type "Tuple[int, slice]"
130-
lvals = lvals[0, :] # type: ignore[call-overload]
128+
lvals = lvals[0, :]
131129
else:
132130
# lvals are 1D, rvals are 2D
133131
assert rvals.shape[0] == 1, rvals.shape

0 commit comments

Comments
 (0)