From cc99dffc47313bc8bb8772fad127c028599f0371 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Wed, 30 Dec 2020 19:26:18 +0100 Subject: [PATCH 1/3] CLN: add typing to dtype arg in selection of files in core/arrays --- pandas/core/arrays/base.py | 15 ++++++++++----- pandas/core/arrays/boolean.py | 8 ++++---- pandas/core/arrays/categorical.py | 13 ++++++++----- pandas/core/arrays/datetimelike.py | 10 +++++----- pandas/core/arrays/integer.py | 6 +++--- pandas/core/arrays/period.py | 6 +++--- pandas/core/dtypes/dtypes.py | 6 +++--- 7 files changed, 36 insertions(+), 28 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 918d96cd03112..b95e5243de88e 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, DtypeArg, 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[DtypeArg] = 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[DtypeArg] = 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[DtypeArg] = 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[DtypeArg] = None) -> ArrayLike: """ Return a view on the array. diff --git a/pandas/core/arrays/boolean.py b/pandas/core/arrays/boolean.py index ea2ca1f70d414..1b2d56b5a0639 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, DtypeArg 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[DtypeArg] = 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[DtypeArg] = 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..1adc157d25ecd 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, DtypeArg, 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[DtypeArg] = 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[DtypeArg] = 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[DtypeArg] = 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[DtypeArg] = None) -> np.ndarray: """ The numpy array interface. diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index c5946fa4ddc46..fe4e8e328babc 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, DtypeArg, DtypeObj 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[DtypeArg] = 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[DtypeArg] = 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[DtypeArg] = 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[DtypeArg] = 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..91319d9bac5c2 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, DtypeArg, 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[DtypeArg] = 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[DtypeArg] = 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..9ae3563ddd8f9 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, DtypeArg 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[DtypeArg] = 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..1eaf105ab9c24 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 DtypeArg, 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[DtypeArg] = None, ) -> "CategoricalDtype": """ Construct dtype from the input parameters used in :class:`Categorical`. @@ -247,7 +247,7 @@ def _from_values_or_dtype( CategoricalDtype(categories=['x', 'y'], ordered=False) """ - if dtype is not None: + if dtype is not None and isinstance(dtype, CategoricalDtype): # The dtype argument takes precedence over values.dtype (if any) if isinstance(dtype, str): if dtype == "category": From 776a97a522ea4a00f1fe1077d20651f863d3c09b Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Wed, 30 Dec 2020 21:00:24 +0100 Subject: [PATCH 2/3] Change to Dtype --- pandas/core/arrays/base.py | 10 +++++----- pandas/core/arrays/boolean.py | 6 +++--- pandas/core/arrays/categorical.py | 10 +++++----- pandas/core/arrays/datetimelike.py | 10 +++++----- pandas/core/arrays/integer.py | 6 +++--- pandas/core/arrays/period.py | 4 ++-- pandas/core/dtypes/dtypes.py | 4 ++-- 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index b95e5243de88e..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, DtypeArg, 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: Optional[DtypeArg] = None, copy=False): + def _from_sequence(cls, scalars, *, dtype: Optional[Dtype] = None, copy=False): """ Construct a new ExtensionArray from a sequence of scalars. @@ -212,7 +212,7 @@ def _from_sequence(cls, scalars, *, dtype: Optional[DtypeArg] = None, copy=False @classmethod def _from_sequence_of_strings( - cls, strings, *, dtype: Optional[DtypeArg] = None, copy=False + cls, strings, *, dtype: Optional[Dtype] = None, copy=False ): """ Construct a new ExtensionArray from a sequence of strings. @@ -394,7 +394,7 @@ def __ne__(self, other: Any) -> ArrayLike: def to_numpy( self, - dtype: Optional[DtypeArg] = None, + dtype: Optional[Dtype] = None, copy: bool = False, na_value=lib.no_default, ) -> np.ndarray: @@ -1070,7 +1070,7 @@ def copy(self: ExtensionArrayT) -> ExtensionArrayT: """ raise AbstractMethodError(self) - def view(self, dtype: Optional[DtypeArg] = 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 1b2d56b5a0639..bbbc0911b4846 100644 --- a/pandas/core/arrays/boolean.py +++ b/pandas/core/arrays/boolean.py @@ -5,7 +5,7 @@ import numpy as np from pandas._libs import lib, missing as libmissing -from pandas._typing import ArrayLike, DtypeArg +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: Optional[DtypeArg] = 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: Optional[DtypeArg] = 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 1adc157d25ecd..d08a6c85dc93e 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -22,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, DtypeArg, Ordered, Scalar +from pandas._typing import ArrayLike, Dtype, 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 @@ -319,7 +319,7 @@ def __init__( values, categories=None, ordered=None, - dtype: Optional[DtypeArg] = None, + dtype: Optional[Dtype] = None, fastpath=False, copy: bool = True, ): @@ -424,7 +424,7 @@ def _constructor(self) -> Type["Categorical"]: return Categorical @classmethod - def _from_sequence(cls, scalars, *, dtype: Optional[DtypeArg] = 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: @@ -560,7 +560,7 @@ def _from_inferred_categories( @classmethod def from_codes( - cls, codes, categories=None, ordered=None, dtype: Optional[DtypeArg] = None + cls, codes, categories=None, ordered=None, dtype: Optional[Dtype] = None ): """ Make a Categorical type from codes and categories or dtype. @@ -1297,7 +1297,7 @@ def _validate_fill_value(self, fill_value): # ------------------------------------------------------------- - def __array__(self, dtype: Optional[DtypeArg] = None) -> np.ndarray: + def __array__(self, dtype: Optional[Dtype] = None) -> np.ndarray: """ The numpy array interface. diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index fe4e8e328babc..51513dd3a8821 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, DtypeArg, DtypeObj +from pandas._typing import DatetimeLikeScalar, Dtype, DtypeObj 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: Optional[DtypeArg] = 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: Optional[DtypeArg] = 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: Optional[DtypeArg] = None) -> np.ndarray: + def __array__(self, dtype: Optional[Dtype] = 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: Optional[DtypeArg] = 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 91319d9bac5c2..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, DtypeArg, 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: Optional[DtypeArg] = 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: Optional[DtypeArg] = 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 9ae3563ddd8f9..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, DtypeArg +from pandas._typing import AnyArrayLike, Dtype from pandas.util._decorators import cache_readonly, doc from pandas.core.dtypes.common import ( @@ -198,7 +198,7 @@ def _from_sequence( cls: Type["PeriodArray"], scalars: Union[Sequence[Optional[Period]], AnyArrayLike], *, - dtype: Optional[DtypeArg] = None, + dtype: Optional[Dtype] = None, copy: bool = False, ) -> "PeriodArray": if dtype and isinstance(dtype, PeriodDtype): diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 1eaf105ab9c24..a8be22bd5fef1 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 DtypeArg, 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[DtypeArg] = None, + dtype: Optional[Dtype] = None, ) -> "CategoricalDtype": """ Construct dtype from the input parameters used in :class:`Categorical`. From db96d90e25d9895d8a16053e54944147565a841e Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Wed, 30 Dec 2020 23:27:12 +0100 Subject: [PATCH 3/3] Update code to let the tests pass --- pandas/_typing.py | 3 ++- pandas/core/arrays/categorical.py | 4 ++-- pandas/core/arrays/datetimelike.py | 4 ++-- pandas/core/dtypes/dtypes.py | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) 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/categorical.py b/pandas/core/arrays/categorical.py index d08a6c85dc93e..8b350fef27fb1 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -22,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 @@ -1297,7 +1297,7 @@ def _validate_fill_value(self, fill_value): # ------------------------------------------------------------- - def __array__(self, dtype: Optional[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 51513dd3a8821..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, Dtype, 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 @@ -265,7 +265,7 @@ def _formatter(self, boxed=False): # ---------------------------------------------------------------- # Array-Like / EA-Interface Methods - def __array__(self, dtype: Optional[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) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index a8be22bd5fef1..123196f43ef2a 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -247,7 +247,7 @@ def _from_values_or_dtype( CategoricalDtype(categories=['x', 'y'], ordered=False) """ - if dtype is not None and isinstance(dtype, CategoricalDtype): + if dtype is not None: # The dtype argument takes precedence over values.dtype (if any) if isinstance(dtype, str): if dtype == "category": @@ -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":