From bdffccc2c99b87291f5281342719e9f212cd0006 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 23 Jun 2021 20:14:09 +0100 Subject: [PATCH 1/3] TYP: update for numpy-1.21.0 --- pandas/core/apply.py | 6 +- pandas/core/arrays/masked.py | 4 +- pandas/core/arrays/period.py | 4 +- pandas/core/arrays/sparse/array.py | 6 +- pandas/core/common.py | 19 +++++- pandas/core/computation/pytables.py | 6 +- pandas/core/dtypes/cast.py | 42 +++++++++---- pandas/core/frame.py | 6 +- pandas/core/generic.py | 8 +-- pandas/core/groupby/ops.py | 4 +- pandas/core/indexes/base.py | 4 +- pandas/core/indexes/category.py | 7 +-- pandas/core/indexes/extension.py | 4 +- pandas/core/internals/blocks.py | 16 ++--- pandas/core/internals/construction.py | 9 +-- pandas/core/nanops.py | 34 ++++++----- pandas/core/reshape/reshape.py | 4 +- pandas/core/series.py | 4 +- pandas/core/tools/numeric.py | 6 +- pandas/io/formats/format.py | 18 +----- pandas/io/parsers/c_parser_wrapper.py | 4 +- pandas/io/stata.py | 60 ++++++++++++++----- .../indexing/multiindex/test_indexing_slow.py | 8 ++- 23 files changed, 161 insertions(+), 122 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 49e06825617bd..69e2650a15f16 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -1076,11 +1076,7 @@ def apply_standard(self) -> FrameOrSeriesUnion: with np.errstate(all="ignore"): if isinstance(f, np.ufunc): - # error: Argument 1 to "__call__" of "ufunc" has incompatible type - # "Series"; expected "Union[Union[int, float, complex, str, bytes, - # generic], Sequence[Union[int, float, complex, str, bytes, generic]], - # Sequence[Sequence[Any]], _SupportsArray]" - return f(obj) # type: ignore[arg-type] + return f(obj) # row-wise access if is_extension_array_dtype(obj.dtype) and hasattr(obj._values, "map"): diff --git a/pandas/core/arrays/masked.py b/pandas/core/arrays/masked.py index d274501143916..c4b9fab28c27e 100644 --- a/pandas/core/arrays/masked.py +++ b/pandas/core/arrays/masked.py @@ -409,9 +409,7 @@ def isin(self, values) -> BooleanArray: # type: ignore[override] result += self._mask else: result *= np.invert(self._mask) - # error: No overload variant of "zeros_like" matches argument types - # "BaseMaskedArray", "Type[bool]" - mask = np.zeros_like(self, dtype=bool) # type: ignore[call-overload] + mask = np.zeros_like(self, dtype=bool) return BooleanArray(result, mask, copy=False) def copy(self: BaseMaskedArrayT) -> BaseMaskedArrayT: diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 04db06ee9fb66..471ee295ebd2f 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -341,7 +341,9 @@ def freq(self) -> BaseOffset: def __array__(self, dtype: NpDtype | None = None) -> np.ndarray: if dtype == "i8": return self.asi8 - elif dtype == bool: + # error: Non-overlapping equality check (left operand type: "Optional[Union[str, + # dtype[Any]]]", right operand type: "Type[bool]") + elif dtype == bool: # type: ignore[comparison-overlap] return ~self._isnan # This will raise TypeError for non-object dtypes diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index e7fecfe2792e0..18c79ccc56bad 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -1463,11 +1463,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs): return type(self)(result) def __abs__(self): - # error: Argument 1 to "__call__" of "ufunc" has incompatible type - # "SparseArray"; expected "Union[Union[int, float, complex, str, bytes, - # generic], Sequence[Union[int, float, complex, str, bytes, generic]], - # Sequence[Sequence[Any]], _SupportsArray]" - return np.abs(self) # type: ignore[arg-type] + return np.abs(self) # ------------------------------------------------------------------------ # Ops diff --git a/pandas/core/common.py b/pandas/core/common.py index fb4c4910a8a95..dda3e39870ffb 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -411,7 +411,7 @@ def random_state(state: RandomState | None = None) -> np.random.RandomState: Returns ------- - np.random.RandomState + np.random.RandomState or np.random if state is None """ if ( @@ -419,11 +419,24 @@ def random_state(state: RandomState | None = None) -> np.random.RandomState: or is_array_like(state) or (not np_version_under1p18 and isinstance(state, np.random.BitGenerator)) ): - return np.random.RandomState(state) + # error: Argument 1 to "RandomState" has incompatible type "Optional[Union[int, + # Union[ExtensionArray, ndarray[Any, Any]], Generator, RandomState]]"; expected + # "Union[None, Union[Union[_SupportsArray[dtype[Union[bool_, integer[Any]]]], + # Sequence[_SupportsArray[dtype[Union[bool_, integer[Any]]]]], + # Sequence[Sequence[_SupportsArray[dtype[Union[bool_, integer[Any]]]]]], + # Sequence[Sequence[Sequence[_SupportsArray[dtype[Union[bool_, + # integer[Any]]]]]]], + # Sequence[Sequence[Sequence[Sequence[_SupportsArray[dtype[Union[bool_, + # integer[Any]]]]]]]]], Union[bool, int, Sequence[Union[bool, int]], + # Sequence[Sequence[Union[bool, int]]], Sequence[Sequence[Sequence[Union[bool, + # int]]]], Sequence[Sequence[Sequence[Sequence[Union[bool, int]]]]]]], + # BitGenerator]" + return np.random.RandomState(state) # type: ignore[arg-type] elif isinstance(state, np.random.RandomState): return state elif state is None: - return np.random + # error: Incompatible return value type (got Module, expected "RandomState") + return np.random # type: ignore[return-value] else: raise ValueError( "random_state must be an integer, array-like, a BitGenerator, " diff --git a/pandas/core/computation/pytables.py b/pandas/core/computation/pytables.py index f733a5c43dfb3..528c7f1a6af20 100644 --- a/pandas/core/computation/pytables.py +++ b/pandas/core/computation/pytables.py @@ -226,11 +226,7 @@ def stringify(value): if v not in metadata: result = -1 else: - # error: Incompatible types in assignment (expression has type - # "Union[Any, ndarray]", variable has type "int") - result = metadata.searchsorted( # type: ignore[assignment] - v, side="left" - ) + result = metadata.searchsorted(v, side="left") return TermValue(result, result, "integer") elif kind == "integer": v = int(float(v)) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 433d45d94167d..52254ff4cdb9b 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -15,6 +15,7 @@ TYPE_CHECKING, Any, Sized, + TypeVar, cast, overload, ) @@ -107,6 +108,8 @@ _int32_max = np.iinfo(np.int32).max _int64_max = np.iinfo(np.int64).max +NumpyArrayT = TypeVar("NumpyArrayT", bound=np.ndarray) + def maybe_convert_platform( values: list | tuple | range | np.ndarray | ExtensionArray, @@ -659,7 +662,10 @@ def _ensure_dtype_type(value, dtype: np.dtype): object """ # Start with exceptions in which we do _not_ cast to numpy types - if dtype == np.object_: + + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[object_]") + if dtype == np.object_: # type: ignore[comparison-overlap] return value # Note: before we get here we have already excluded isna(value) @@ -866,10 +872,10 @@ def maybe_infer_dtype_type(element): def maybe_upcast( - values: np.ndarray, + values: NumpyArrayT, fill_value: Scalar = np.nan, copy: bool = False, -) -> tuple[np.ndarray, Scalar]: +) -> tuple[NumpyArrayT, Scalar]: """ Provide explicit type promotion and coercion. @@ -1093,7 +1099,10 @@ def astype_nansafe( raise ValueError("dtype must be np.dtype or ExtensionDtype") if arr.dtype.kind in ["m", "M"] and ( - issubclass(dtype.type, str) or dtype == object + issubclass(dtype.type, str) + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[object]") + or dtype == object # type: ignore[comparison-overlap] ): from pandas.core.construction import ensure_wrapped_if_datetimelike @@ -1104,7 +1113,9 @@ def astype_nansafe( return lib.ensure_string_array(arr, skipna=skipna, convert_na_value=False) elif is_datetime64_dtype(arr): - if dtype == np.int64: + # Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[signedinteger[Any]]") + if dtype == np.int64: # type: ignore[comparison-overlap] warnings.warn( f"casting {arr.dtype} values to int64 with .astype(...) " "is deprecated and will raise in a future version. " @@ -1124,7 +1135,9 @@ def astype_nansafe( raise TypeError(f"cannot astype a datetimelike from [{arr.dtype}] to [{dtype}]") elif is_timedelta64_dtype(arr): - if dtype == np.int64: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[signedinteger[Any]]") + if dtype == np.int64: # type: ignore[comparison-overlap] warnings.warn( f"casting {arr.dtype} values to int64 with .astype(...) " "is deprecated and will raise in a future version. " @@ -1398,10 +1411,9 @@ def convert_dtypes( if is_string_dtype(inferred_dtype): if not convert_string: - inferred_dtype = input_array.dtype + return input_array.dtype else: - inferred_dtype = pandas_dtype("string") - return inferred_dtype + return pandas_dtype("string") if convert_integer: target_int_dtype = pandas_dtype("Int64") @@ -1454,7 +1466,9 @@ def convert_dtypes( else: return input_array.dtype - return inferred_dtype + # error: Incompatible return value type (got "Union[str, Union[dtype[Any], + # ExtensionDtype]]", expected "Union[dtype[Any], ExtensionDtype]") + return inferred_dtype # type: ignore[return-value] def maybe_infer_to_datetimelike( @@ -1831,7 +1845,9 @@ def construct_2d_arraylike_from_scalar( if dtype.kind in ["m", "M"]: value = maybe_unbox_datetimelike_tz_deprecation(value, dtype, stacklevel=4) - elif dtype == object: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[object]") + elif dtype == object: # type: ignore[comparison-overlap] if isinstance(value, (np.timedelta64, np.datetime64)): # calling np.array below would cast to pytimedelta/pydatetime out = np.empty(shape, dtype=object) @@ -2206,7 +2222,9 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool: return tipo.kind == "b" return lib.is_bool(element) - elif dtype == object: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[object]") + elif dtype == object: # type: ignore[comparison-overlap] return True elif dtype.kind == "S": diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ec7de0440f482..4c89c8e36d6a4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -647,7 +647,7 @@ def __init__( elif isinstance(data, (np.ndarray, Series, Index)): if data.dtype.names: # i.e. numpy structured array - + data = cast("np.ma.MaskedArray | np.recarray | np.ndarray", data) mgr = rec_array_to_mgr( data, index, @@ -2279,9 +2279,7 @@ def to_records( if dtype_mapping is None: formats.append(v.dtype) elif isinstance(dtype_mapping, (type, np.dtype, str)): - # error: Argument 1 to "append" of "list" has incompatible type - # "Union[type, dtype, str]"; expected "dtype" - formats.append(dtype_mapping) # type: ignore[arg-type] + formats.append(dtype_mapping) else: element = "row" if i < index_len else "column" msg = f"Invalid dtype {dtype_mapping} specified for {element} {name}" diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 30c33a951e145..adc722e770cff 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -9751,11 +9751,9 @@ def abs(self: FrameOrSeries) -> FrameOrSeries: 2 6 30 -30 3 7 40 -50 """ - # error: Argument 1 to "__call__" of "ufunc" has incompatible type - # "FrameOrSeries"; expected "Union[Union[int, float, complex, str, bytes, - # generic], Sequence[Union[int, float, complex, str, bytes, generic]], - # Sequence[Sequence[Any]], _SupportsArray]" - return np.abs(self) # type: ignore[arg-type] + # error: Incompatible return value type (got "ndarray[Any, dtype[Any]]", + # expected "FrameOrSeries") + return np.abs(self) # type: ignore[return-value] @final def describe( diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index b65f26c7174fc..874d7395b1950 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -161,7 +161,9 @@ def _get_cython_function( f = getattr(libgroupby, ftype) if is_numeric: return f - elif dtype == object: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Literal['object']") + elif dtype == object: # type: ignore[comparison-overlap] if "object" not in f.__signatures__: # raise NotImplementedError here rather than TypeError later raise NotImplementedError( diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index b092bd6666fc0..7aa52b54d61c0 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -559,7 +559,9 @@ def _dtype_to_subclass(cls, dtype: DtypeObj): return Int64Index - elif dtype == object: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[object]") + elif dtype == object: # type: ignore[comparison-overlap] # NB: assuming away MultiIndex return Index diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index ce5cb0779ee53..9529ed5531ad5 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -187,17 +187,12 @@ def _can_hold_strings(self): def _engine_type(self): # self.codes can have dtype int8, int16, int32 or int64, so we need # to return the corresponding engine type (libindex.Int8Engine, etc.). - - # error: Invalid index type "Type[generic]" for "Dict[Type[signedinteger[Any]], - # Any]"; expected type "Type[signedinteger[Any]]" return { np.int8: libindex.Int8Engine, np.int16: libindex.Int16Engine, np.int32: libindex.Int32Engine, np.int64: libindex.Int64Engine, - }[ - self.codes.dtype.type # type: ignore[index] - ] + }[self.codes.dtype.type] _attributes = ["name"] diff --git a/pandas/core/indexes/extension.py b/pandas/core/indexes/extension.py index 6ff20f7d009bc..ccc1884dd7495 100644 --- a/pandas/core/indexes/extension.py +++ b/pandas/core/indexes/extension.py @@ -397,11 +397,13 @@ def astype(self, dtype, copy: bool = True) -> Index: return self return self.copy() + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Literal['M8[ns]']") if ( isinstance(self.dtype, np.dtype) and isinstance(dtype, np.dtype) and dtype.kind == "M" - and dtype != "M8[ns]" + and dtype != "M8[ns]" # type: ignore[comparison-overlap] ): # For now Datetime supports this by unwrapping ndarray, but DTI doesn't raise TypeError(f"Cannot cast {type(self).__name__} to dtype") diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 237d06402a0ee..6275fe39558a3 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1167,13 +1167,17 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> list[Blo # convert integer to float if necessary. need to do a lot more than # that, handle boolean etc also - # error: Argument 1 to "maybe_upcast" has incompatible type "Union[ndarray, - # ExtensionArray]"; expected "ndarray" + # error: Value of type variable "NumpyArrayT" of "maybe_upcast" cannot be + # "Union[ndarray[Any, Any], ExtensionArray]" new_values, fill_value = maybe_upcast( - self.values, fill_value # type: ignore[arg-type] + self.values, fill_value # type: ignore[type-var] ) - new_values = shift(new_values, periods, axis, fill_value) + # error: Argument 1 to "shift" has incompatible type "Union[ndarray[Any, Any], + # ExtensionArray]"; expected "ndarray[Any, Any]" + new_values = shift( + new_values, periods, axis, fill_value # type: ignore[arg-type] + ) return [self.make_block(new_values)] @@ -1894,9 +1898,7 @@ def get_block_type(values, dtype: Dtype | None = None): cls = ExtensionBlock elif isinstance(dtype, CategoricalDtype): cls = CategoricalBlock - # error: Non-overlapping identity check (left operand type: "Type[generic]", - # right operand type: "Type[Timestamp]") - elif vtype is Timestamp: # type: ignore[comparison-overlap] + elif vtype is Timestamp: cls = DatetimeTZBlock elif isinstance(dtype, ExtensionDtype): # Note: need to be sure PandasArray is unwrapped before we get here diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 81bf3ca4ba07a..52b2412bd42d1 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -6,7 +6,6 @@ from collections import abc from typing import ( - TYPE_CHECKING, Any, Hashable, Sequence, @@ -86,10 +85,6 @@ create_block_manager_from_blocks, ) -if TYPE_CHECKING: - from numpy.ma.mrecords import MaskedRecords - - # --------------------------------------------------------------------- # BlockManager Interface @@ -142,7 +137,7 @@ def arrays_to_mgr( def rec_array_to_mgr( - data: MaskedRecords | np.recarray | np.ndarray, + data: np.ma.MaskedArray | np.recarray | np.ndarray, index, columns, dtype: DtypeObj | None, @@ -188,7 +183,7 @@ def rec_array_to_mgr( return mgr -def fill_masked_arrays(data: MaskedRecords, arr_columns: Index) -> list[np.ndarray]: +def fill_masked_arrays(data: np.ma.MaskedArray, arr_columns: Index) -> list[np.ndarray]: """ Convert numpy MaskedRecords to ensure mask is softened. """ diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index c34944985f2b6..93e6ce4997672 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -4,6 +4,7 @@ import itertools import operator from typing import ( + TYPE_CHECKING, Any, cast, ) @@ -56,6 +57,12 @@ from pandas.core.construction import extract_array +if TYPE_CHECKING: + from pandas.core.arrays import ( + DatetimeArray, + TimedeltaArray, + ) + bn = import_optional_dependency("bottleneck", errors="warn") _BOTTLENECK_INSTALLED = bn is not None _USE_BOTTLENECK = False @@ -603,7 +610,9 @@ def _mask_datetimelike_result( # we need to apply the mask result = result.astype("i8").view(orig_values.dtype) axis_mask = mask.any(axis=axis) - result[axis_mask] = iNaT + # error: Unsupported target for indexed assignment ("Union[ndarray[Any, Any], + # datetime64, timedelta64]") + result[axis_mask] = iNaT # type: ignore[index] else: if mask.any(): return NaT @@ -755,7 +764,10 @@ def get_median(x): def get_empty_reduction_result( - shape: tuple[int, ...], axis: int, dtype: np.dtype, fill_value: Any + shape: tuple[int, ...], + axis: int, + dtype: np.dtype | type[np.floating], + fill_value: Any, ) -> np.ndarray: """ The result from a reduction on an empty ndarray. @@ -809,9 +821,7 @@ def _get_counts_nanvar( """ dtype = get_dtype(dtype) count = _get_counts(values_shape, mask, axis, dtype=dtype) - # error: Unsupported operand types for - ("int" and "generic") - # error: Unsupported operand types for - ("float" and "generic") - d = count - dtype.type(ddof) # type: ignore[operator] + d = count - dtype.type(ddof) # always return NaN, never inf if is_scalar(count): @@ -1400,9 +1410,7 @@ def _get_counts( n = mask.size - mask.sum() else: n = np.prod(values_shape) - # error: Incompatible return value type (got "Union[Any, generic]", - # expected "Union[int, float, ndarray]") - return dtype.type(n) # type: ignore[return-value] + return dtype.type(n) if mask is not None: count = mask.shape[axis] - mask.sum(axis) @@ -1410,9 +1418,7 @@ def _get_counts( count = values_shape[axis] if is_scalar(count): - # error: Incompatible return value type (got "Union[Any, generic]", - # expected "Union[int, float, ndarray]") - return dtype.type(count) # type: ignore[return-value] + return dtype.type(count) try: return count.astype(dtype) except AttributeError: @@ -1784,11 +1790,9 @@ def na_accum_func(values: ArrayLike, accum_func, *, skipna: bool) -> ArrayLike: # DatetimeArray/TimedeltaArray # TODO: have this case go through a DTA method? # For DatetimeTZDtype, view result as M8[ns] + values = cast("DatetimeArray | TimedeltaArray", values) npdtype = orig_dtype if isinstance(orig_dtype, np.dtype) else "M8[ns]" - # error: "Type[ExtensionArray]" has no attribute "_simple_new" - result = type(values)._simple_new( # type: ignore[attr-defined] - result.view(npdtype), dtype=orig_dtype - ) + result = type(values)._simple_new(result.view(npdtype), dtype=orig_dtype) elif skipna and not issubclass(values.dtype.type, (np.integer, np.bool_)): vals = values.copy() diff --git a/pandas/core/reshape/reshape.py b/pandas/core/reshape/reshape.py index 2f45cae46b32e..0edb150bdc273 100644 --- a/pandas/core/reshape/reshape.py +++ b/pandas/core/reshape/reshape.py @@ -1017,7 +1017,9 @@ def get_empty_frame(data) -> DataFrame: fill_value: bool | float | int if is_integer_dtype(dtype): fill_value = 0 - elif dtype == bool: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[bool]") + elif dtype == bool: # type: ignore[comparison-overlap] fill_value = False else: fill_value = 0.0 diff --git a/pandas/core/series.py b/pandas/core/series.py index e94689383100d..fd294c12422d5 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -388,9 +388,7 @@ def __init__( copy = False elif isinstance(data, np.ndarray): - # error: Argument 1 to "len" has incompatible type "dtype"; expected - # "Sized" - if len(data.dtype): # type: ignore[arg-type] + if len(data.dtype): # GH#13296 we are dealing with a compound dtype, which # should be treated as 2D raise ValueError( diff --git a/pandas/core/tools/numeric.py b/pandas/core/tools/numeric.py index 6dfd67f5dc5ec..7d2bb75934c33 100644 --- a/pandas/core/tools/numeric.py +++ b/pandas/core/tools/numeric.py @@ -190,7 +190,7 @@ def to_numeric(arg, errors="raise", downcast=None): # attempt downcast only if the data has been successfully converted # to a numerical dtype and if a downcast method has been specified if downcast is not None and is_numeric_dtype(values.dtype): - typecodes = None + typecodes: str | None = None if downcast in ("integer", "signed"): typecodes = np.typecodes["Integer"] @@ -208,8 +208,8 @@ def to_numeric(arg, errors="raise", downcast=None): if typecodes is not None: # from smallest to largest - for dtype in typecodes: - dtype = np.dtype(dtype) + for typecode in typecodes: + dtype = np.dtype(typecode) if dtype.itemsize <= values.dtype.itemsize: values = maybe_downcast_numeric(values, dtype) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index c78671b789533..83e0086958b9a 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1635,24 +1635,10 @@ def format_percentiles( percentiles = 100 * percentiles - # error: Item "List[Union[int, float]]" of "Union[ndarray, List[Union[int, float]], - # List[float], List[Union[str, float]]]" has no attribute "astype" - # error: Item "List[float]" of "Union[ndarray, List[Union[int, float]], List[float], - # List[Union[str, float]]]" has no attribute "astype" - # error: Item "List[Union[str, float]]" of "Union[ndarray, List[Union[int, float]], - # List[float], List[Union[str, float]]]" has no attribute "astype" - int_idx = np.isclose( - percentiles.astype(int), percentiles # type: ignore[union-attr] - ) + int_idx = np.isclose(percentiles.astype(int), percentiles) if np.all(int_idx): - # error: Item "List[Union[int, float]]" of "Union[ndarray, List[Union[int, - # float]], List[float], List[Union[str, float]]]" has no attribute "astype" - # error: Item "List[float]" of "Union[ndarray, List[Union[int, float]], - # List[float], List[Union[str, float]]]" has no attribute "astype" - # error: Item "List[Union[str, float]]" of "Union[ndarray, List[Union[int, - # float]], List[float], List[Union[str, float]]]" has no attribute "astype" - out = percentiles.astype(int).astype(str) # type: ignore[union-attr] + out = percentiles.astype(int).astype(str) return [i + "%" for i in out] unique_pcts = np.unique(percentiles) diff --git a/pandas/io/parsers/c_parser_wrapper.py b/pandas/io/parsers/c_parser_wrapper.py index 110211125514e..ae62cc3b45578 100644 --- a/pandas/io/parsers/c_parser_wrapper.py +++ b/pandas/io/parsers/c_parser_wrapper.py @@ -365,7 +365,9 @@ def _concatenate_chunks(chunks: list[dict[int, ArrayLike]]) -> dict: numpy_dtypes, # type: ignore[arg-type] [], ) - if common_type == object: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", + # right operand type: "Type[object]") + if common_type == object: # type: ignore[comparison-overlap] warning_columns.append(str(name)) dtype = dtypes.pop() diff --git a/pandas/io/stata.py b/pandas/io/stata.py index ffaebb3c10ae2..4eb42640d9b70 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -853,15 +853,25 @@ def __eq__(self, other: Any) -> bool: @classmethod def get_base_missing_value(cls, dtype: np.dtype) -> int | float: - if dtype == np.int8: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[signedinteger[Any]]") + if dtype == np.int8: # type: ignore[comparison-overlap] value = cls.BASE_MISSING_VALUES["int8"] - elif dtype == np.int16: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[signedinteger[Any]]") + elif dtype == np.int16: # type: ignore[comparison-overlap] value = cls.BASE_MISSING_VALUES["int16"] - elif dtype == np.int32: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[signedinteger[Any]]") + elif dtype == np.int32: # type: ignore[comparison-overlap] value = cls.BASE_MISSING_VALUES["int32"] - elif dtype == np.float32: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[floating[Any]]") + elif dtype == np.float32: # type: ignore[comparison-overlap] value = cls.BASE_MISSING_VALUES["float32"] - elif dtype == np.float64: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[floating[Any]]") + elif dtype == np.float64: # type: ignore[comparison-overlap] value = cls.BASE_MISSING_VALUES["float64"] else: raise ValueError("Unsupported dtype") @@ -2033,15 +2043,25 @@ def _dtype_to_stata_type(dtype: np.dtype, column: Series) -> int: # do? itemsize = max_len_string_array(ensure_object(column._values)) return max(itemsize, 1) - elif dtype == np.float64: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[floating[Any]]") + elif dtype == np.float64: # type: ignore[comparison-overlap] return 255 - elif dtype == np.float32: + # Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[floating[Any]]") + elif dtype == np.float32: # type: ignore[comparison-overlap] return 254 - elif dtype == np.int32: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[signedinteger[Any]]") + elif dtype == np.int32: # type: ignore[comparison-overlap] return 253 - elif dtype == np.int16: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[signedinteger[Any]]") + elif dtype == np.int16: # type: ignore[comparison-overlap] return 252 - elif dtype == np.int8: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[signedinteger[Any]]") + elif dtype == np.int8: # type: ignore[comparison-overlap] return 251 else: # pragma : no cover raise NotImplementedError(f"Data type {dtype} not supported.") @@ -2761,15 +2781,25 @@ def _dtype_to_stata_type_117(dtype: np.dtype, column: Series, force_strl: bool) if itemsize <= 2045: return itemsize return 32768 - elif dtype == np.float64: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[floating[Any]]") + elif dtype == np.float64: # type: ignore[comparison-overlap] return 65526 - elif dtype == np.float32: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[floating[Any]]") + elif dtype == np.float32: # type: ignore[comparison-overlap] return 65527 - elif dtype == np.int32: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[signedinteger[Any]]") [comparison-overlap] + elif dtype == np.int32: # type: ignore[comparison-overlap] return 65528 - elif dtype == np.int16: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[signedinteger[Any]]") + elif dtype == np.int16: # type: ignore[comparison-overlap] return 65529 - elif dtype == np.int8: + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[signedinteger[Any]]") + elif dtype == np.int8: # type: ignore[comparison-overlap] return 65530 else: # pragma : no cover raise NotImplementedError(f"Data type {dtype} not supported.") diff --git a/pandas/tests/indexing/multiindex/test_indexing_slow.py b/pandas/tests/indexing/multiindex/test_indexing_slow.py index a38b5f6cc449a..e8c766d489813 100644 --- a/pandas/tests/indexing/multiindex/test_indexing_slow.py +++ b/pandas/tests/indexing/multiindex/test_indexing_slow.py @@ -1,3 +1,7 @@ +from typing import ( + Any, + List, +) import warnings import numpy as np @@ -14,7 +18,7 @@ n = 1000 cols = ["jim", "joe", "jolie", "joline", "jolia"] -vals = [ +vals: List[Any] = [ np.random.randint(0, 10, n), np.random.choice(list("abcdefghij"), n), np.random.choice(pd.date_range("20141009", periods=10).tolist(), n), @@ -24,7 +28,7 @@ vals = list(map(tuple, zip(*vals))) # bunch of keys for testing -keys = [ +keys: List[Any] = [ np.random.randint(0, 11, m), np.random.choice(list("abcdefghijk"), m), np.random.choice(pd.date_range("20141009", periods=11).tolist(), m), From 697a6fb031bdec2c53ef3f29592ee466cf031efd Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 23 Jun 2021 19:01:58 -0700 Subject: [PATCH 2/3] TYP: restore MaskedRecords annotation --- pandas/core/internals/construction.py | 13 +++++++++++-- pandas/core/nanops.py | 14 +++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 52b2412bd42d1..7bef7ae9b39d7 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -6,9 +6,11 @@ from collections import abc from typing import ( + TYPE_CHECKING, Any, Hashable, Sequence, + cast, ) import warnings @@ -85,6 +87,10 @@ create_block_manager_from_blocks, ) +if TYPE_CHECKING: + from numpy.ma.mrecords import MaskedRecords + + # --------------------------------------------------------------------- # BlockManager Interface @@ -137,7 +143,7 @@ def arrays_to_mgr( def rec_array_to_mgr( - data: np.ma.MaskedArray | np.recarray | np.ndarray, + data: MaskedRecords | np.recarray | np.ndarray, index, columns, dtype: DtypeObj | None, @@ -160,6 +166,9 @@ def rec_array_to_mgr( # fill if needed if isinstance(data, np.ma.MaskedArray): + # GH#42200 we only get here with MaskedRecords, but check for the + # parent class MaskedArray to avoid the need to import MaskedRecords + data = cast("MaskedRecords", data) new_arrays = fill_masked_arrays(data, arr_columns) else: # error: Incompatible types in assignment (expression has type @@ -183,7 +192,7 @@ def rec_array_to_mgr( return mgr -def fill_masked_arrays(data: np.ma.MaskedArray, arr_columns: Index) -> list[np.ndarray]: +def fill_masked_arrays(data: MaskedRecords, arr_columns: Index) -> list[np.ndarray]: """ Convert numpy MaskedRecords to ensure mask is softened. """ diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 93e6ce4997672..3b03a28afe163 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -4,7 +4,6 @@ import itertools import operator from typing import ( - TYPE_CHECKING, Any, cast, ) @@ -57,12 +56,6 @@ from pandas.core.construction import extract_array -if TYPE_CHECKING: - from pandas.core.arrays import ( - DatetimeArray, - TimedeltaArray, - ) - bn = import_optional_dependency("bottleneck", errors="warn") _BOTTLENECK_INSTALLED = bn is not None _USE_BOTTLENECK = False @@ -1790,9 +1783,12 @@ def na_accum_func(values: ArrayLike, accum_func, *, skipna: bool) -> ArrayLike: # DatetimeArray/TimedeltaArray # TODO: have this case go through a DTA method? # For DatetimeTZDtype, view result as M8[ns] - values = cast("DatetimeArray | TimedeltaArray", values) npdtype = orig_dtype if isinstance(orig_dtype, np.dtype) else "M8[ns]" - result = type(values)._simple_new(result.view(npdtype), dtype=orig_dtype) + # Item "type" of "Union[Type[ExtensionArray], Type[ndarray[Any, Any]]]" + # has no attribute "_simple_new" + result = type(values)._simple_new( # type: ignore[union-attr] + result.view(npdtype), dtype=orig_dtype + ) elif skipna and not issubclass(values.dtype.type, (np.integer, np.bool_)): vals = values.copy() From d9e2e20a5c03f2239f19a9b31fc8fe0ae67599b9 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Thu, 24 Jun 2021 10:00:33 +0100 Subject: [PATCH 3/3] simplify cast --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4c89c8e36d6a4..6cc12ccfba22e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -647,7 +647,7 @@ def __init__( elif isinstance(data, (np.ndarray, Series, Index)): if data.dtype.names: # i.e. numpy structured array - data = cast("np.ma.MaskedArray | np.recarray | np.ndarray", data) + data = cast(np.ndarray, data) mgr = rec_array_to_mgr( data, index,