diff --git a/pandas/_typing.py b/pandas/_typing.py index 64452bf337361..0b50dd69f7abb 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -94,8 +94,9 @@ Axes = Collection # dtypes +NpDtype = Union[str, np.dtype] Dtype = Union[ - "ExtensionDtype", str, np.dtype, Type[Union[str, float, int, complex, bool, object]] + "ExtensionDtype", NpDtype, Type[Union[str, float, int, complex, bool, object]] ] # DtypeArg specifies all allowable dtypes in a functions its dtype argument DtypeArg = Union[Dtype, Dict[Label, Dtype]] diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 918d96cd03112..9a8b37e0785e0 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -25,7 +25,7 @@ import numpy as np from pandas._libs import lib -from pandas._typing import ArrayLike, Shape +from pandas._typing import ArrayLike, Dtype, Shape from pandas.compat import set_function_name from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError @@ -189,7 +189,7 @@ class ExtensionArray: # ------------------------------------------------------------------------ @classmethod - def _from_sequence(cls, scalars, *, dtype=None, copy=False): + def _from_sequence(cls, scalars, *, dtype: Optional[Dtype] = None, copy=False): """ Construct a new ExtensionArray from a sequence of scalars. @@ -211,7 +211,9 @@ def _from_sequence(cls, scalars, *, dtype=None, copy=False): raise AbstractMethodError(cls) @classmethod - def _from_sequence_of_strings(cls, strings, *, dtype=None, copy=False): + def _from_sequence_of_strings( + cls, strings, *, dtype: Optional[Dtype] = None, copy=False + ): """ Construct a new ExtensionArray from a sequence of strings. @@ -391,7 +393,10 @@ def __ne__(self, other: Any) -> ArrayLike: return ~(self == other) def to_numpy( - self, dtype=None, copy: bool = False, na_value=lib.no_default + self, + dtype: Optional[Dtype] = None, + copy: bool = False, + na_value=lib.no_default, ) -> np.ndarray: """ Convert to a NumPy ndarray. @@ -1065,7 +1070,7 @@ def copy(self: ExtensionArrayT) -> ExtensionArrayT: """ raise AbstractMethodError(self) - def view(self, dtype=None) -> ArrayLike: + def view(self, dtype: Optional[Dtype] = None) -> ArrayLike: """ Return a view on the array. diff --git a/pandas/core/arrays/boolean.py b/pandas/core/arrays/boolean.py index ea2ca1f70d414..bbbc0911b4846 100644 --- a/pandas/core/arrays/boolean.py +++ b/pandas/core/arrays/boolean.py @@ -1,11 +1,11 @@ import numbers -from typing import TYPE_CHECKING, List, Tuple, Type, Union +from typing import TYPE_CHECKING, List, Optional, Tuple, Type, Union import warnings import numpy as np from pandas._libs import lib, missing as libmissing -from pandas._typing import ArrayLike +from pandas._typing import ArrayLike, Dtype from pandas.compat.numpy import function as nv from pandas.core.dtypes.common import ( @@ -273,7 +273,7 @@ def dtype(self) -> BooleanDtype: @classmethod def _from_sequence( - cls, scalars, *, dtype=None, copy: bool = False + cls, scalars, *, dtype: Optional[Dtype] = None, copy: bool = False ) -> "BooleanArray": if dtype: assert dtype == "boolean" @@ -282,7 +282,7 @@ def _from_sequence( @classmethod def _from_sequence_of_strings( - cls, strings: List[str], *, dtype=None, copy: bool = False + cls, strings: List[str], *, dtype: Optional[Dtype] = None, copy: bool = False ) -> "BooleanArray": def map_string(s): if isna(s): diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 982349ea345ca..8b350fef27fb1 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -7,6 +7,7 @@ Dict, Hashable, List, + Optional, Sequence, Type, TypeVar, @@ -21,7 +22,7 @@ from pandas._libs import NaT, algos as libalgos, hashtable as htable from pandas._libs.lib import no_default -from pandas._typing import ArrayLike, Dtype, Ordered, Scalar +from pandas._typing import ArrayLike, Dtype, NpDtype, Ordered, Scalar from pandas.compat.numpy import function as nv from pandas.util._decorators import cache_readonly, deprecate_kwarg from pandas.util._validators import validate_bool_kwarg, validate_fillna_kwargs @@ -318,7 +319,7 @@ def __init__( values, categories=None, ordered=None, - dtype=None, + dtype: Optional[Dtype] = None, fastpath=False, copy: bool = True, ): @@ -423,7 +424,7 @@ def _constructor(self) -> Type["Categorical"]: return Categorical @classmethod - def _from_sequence(cls, scalars, *, dtype=None, copy=False): + def _from_sequence(cls, scalars, *, dtype: Optional[Dtype] = None, copy=False): return Categorical(scalars, dtype=dtype, copy=copy) def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike: @@ -558,7 +559,9 @@ def _from_inferred_categories( return cls(codes, dtype=dtype, fastpath=True) @classmethod - def from_codes(cls, codes, categories=None, ordered=None, dtype=None): + def from_codes( + cls, codes, categories=None, ordered=None, dtype: Optional[Dtype] = None + ): """ Make a Categorical type from codes and categories or dtype. @@ -1294,7 +1297,7 @@ def _validate_fill_value(self, fill_value): # ------------------------------------------------------------- - def __array__(self, dtype=None) -> np.ndarray: + def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray: """ The numpy array interface. diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index c5946fa4ddc46..b31bc0934fe60 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -36,7 +36,7 @@ integer_op_not_supported, round_nsint64, ) -from pandas._typing import DatetimeLikeScalar, DtypeObj +from pandas._typing import DatetimeLikeScalar, Dtype, DtypeObj, NpDtype from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError, NullFrequencyError, PerformanceWarning from pandas.util._decorators import Appender, Substitution, cache_readonly @@ -107,7 +107,7 @@ class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray): _recognized_scalars: Tuple[Type, ...] _data: np.ndarray - def __init__(self, data, dtype=None, freq=None, copy=False): + def __init__(self, data, dtype: Optional[Dtype] = None, freq=None, copy=False): raise AbstractMethodError(self) @classmethod @@ -115,7 +115,7 @@ def _simple_new( cls: Type[DatetimeLikeArrayT], values: np.ndarray, freq: Optional[BaseOffset] = None, - dtype=None, + dtype: Optional[Dtype] = None, ) -> DatetimeLikeArrayT: raise AbstractMethodError(cls) @@ -265,7 +265,7 @@ def _formatter(self, boxed=False): # ---------------------------------------------------------------- # Array-Like / EA-Interface Methods - def __array__(self, dtype=None) -> np.ndarray: + def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray: # used for Timedelta/DatetimeArray, overwritten by PeriodArray if is_object_dtype(dtype): return np.array(list(self), dtype=object) @@ -383,7 +383,7 @@ def astype(self, dtype, copy=True): else: return np.asarray(self, dtype=dtype) - def view(self, dtype=None): + def view(self, dtype: Optional[Dtype] = None): if dtype is None or dtype is self.dtype: return type(self)(self._ndarray, dtype=self.dtype) return self._ndarray.view(dtype=dtype) diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py index d01f84b224a89..f8378fb7d1500 100644 --- a/pandas/core/arrays/integer.py +++ b/pandas/core/arrays/integer.py @@ -5,7 +5,7 @@ import numpy as np from pandas._libs import iNaT, lib, missing as libmissing -from pandas._typing import ArrayLike, DtypeObj +from pandas._typing import ArrayLike, Dtype, DtypeObj from pandas.compat.numpy import function as nv from pandas.util._decorators import cache_readonly @@ -304,14 +304,14 @@ def __abs__(self): @classmethod def _from_sequence( - cls, scalars, *, dtype=None, copy: bool = False + cls, scalars, *, dtype: Optional[Dtype] = None, copy: bool = False ) -> "IntegerArray": values, mask = coerce_to_array(scalars, dtype=dtype, copy=copy) return IntegerArray(values, mask) @classmethod def _from_sequence_of_strings( - cls, strings, *, dtype=None, copy: bool = False + cls, strings, *, dtype: Optional[Dtype] = None, copy: bool = False ) -> "IntegerArray": scalars = to_numeric(strings, errors="raise") return cls._from_sequence(scalars, dtype=dtype, copy=copy) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index ea97ec387f192..e0e40a666896d 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -26,7 +26,7 @@ get_period_field_arr, period_asfreq_arr, ) -from pandas._typing import AnyArrayLike +from pandas._typing import AnyArrayLike, Dtype from pandas.util._decorators import cache_readonly, doc from pandas.core.dtypes.common import ( @@ -198,10 +198,10 @@ def _from_sequence( cls: Type["PeriodArray"], scalars: Union[Sequence[Optional[Period]], AnyArrayLike], *, - dtype: Optional[PeriodDtype] = None, + dtype: Optional[Dtype] = None, copy: bool = False, ) -> "PeriodArray": - if dtype: + if dtype and isinstance(dtype, PeriodDtype): freq = dtype.freq else: freq = None diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 4cfba314c719c..123196f43ef2a 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -22,7 +22,7 @@ from pandas._libs.interval import Interval from pandas._libs.tslibs import NaT, Period, Timestamp, dtypes, timezones, to_offset from pandas._libs.tslibs.offsets import BaseOffset -from pandas._typing import DtypeObj, Ordered +from pandas._typing import Dtype, DtypeObj, Ordered from pandas.core.dtypes.base import ExtensionDtype, register_extension_dtype from pandas.core.dtypes.generic import ABCCategoricalIndex, ABCIndex @@ -185,7 +185,7 @@ def _from_values_or_dtype( values=None, categories=None, ordered: Optional[bool] = None, - dtype: Optional["CategoricalDtype"] = None, + dtype: Optional[Dtype] = None, ) -> "CategoricalDtype": """ Construct dtype from the input parameters used in :class:`Categorical`. @@ -272,7 +272,7 @@ def _from_values_or_dtype( # ordered=None. dtype = CategoricalDtype(categories, ordered) - return dtype + return cast(CategoricalDtype, dtype) @classmethod def construct_from_string(cls, string: str_type) -> "CategoricalDtype":