Skip to content

Commit d603d43

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

File tree

27 files changed

+147
-64
lines changed

27 files changed

+147
-64
lines changed

environment.yml

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 2 deletions
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

Lines changed: 1 addition & 6 deletions
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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 10 additions & 2 deletions
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

Lines changed: 16 additions & 4 deletions
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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"}

0 commit comments

Comments
 (0)