diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 42299aaf46a48..0900688e04374 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -123,6 +123,8 @@ from pandas.tseries import frequencies if TYPE_CHECKING: + from typing import Literal + from pandas.core.arrays import ( DatetimeArray, TimedeltaArray, @@ -458,6 +460,14 @@ def astype(self, dtype, copy=True): def view(self: DatetimeLikeArrayT) -> DatetimeLikeArrayT: ... + @overload + def view(self, dtype: Literal["M8[ns]"]) -> DatetimeArray: + ... + + @overload + def view(self, dtype: Literal["m8[ns]"]) -> TimedeltaArray: + ... + @overload def view(self, dtype: Optional[Dtype] = ...) -> ArrayLike: ... diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index a39182d61a8fb..d91522a9e1bb6 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -643,11 +643,7 @@ def fillna(self, value=None, method=None, limit=None) -> PeriodArray: if method is not None: # view as dt64 so we get treated as timelike in core.missing dta = self.view("M8[ns]") - # error: Item "ndarray" of "Union[ExtensionArray, ndarray]" has no attribute - # "fillna" - result = dta.fillna( # type: ignore[union-attr] - value=value, method=method, limit=limit - ) + result = dta.fillna(value=value, method=method, limit=limit) return result.view(self.dtype) return super().fillna(value=value, method=method, limit=limit) diff --git a/pandas/core/arrays/sparse/dtype.py b/pandas/core/arrays/sparse/dtype.py index 8e55eb5f3d358..d2d05577d14df 100644 --- a/pandas/core/arrays/sparse/dtype.py +++ b/pandas/core/arrays/sparse/dtype.py @@ -27,7 +27,6 @@ from pandas.core.dtypes.cast import astype_nansafe from pandas.core.dtypes.common import ( is_bool_dtype, - is_extension_array_dtype, is_object_dtype, is_scalar, is_string_dtype, @@ -339,14 +338,10 @@ def update_dtype(self, dtype): dtype = pandas_dtype(dtype) if not isinstance(dtype, cls): - if is_extension_array_dtype(dtype): + if not isinstance(dtype, np.dtype): raise TypeError("sparse arrays of extension dtypes not supported") - # error: Item "ExtensionArray" of "Union[ExtensionArray, ndarray]" has no - # attribute "item" - fill_value = astype_nansafe( # type: ignore[union-attr] - np.array(self.fill_value), dtype - ).item() + fill_value = astype_nansafe(np.array(self.fill_value), dtype).item() dtype = cls(dtype, fill_value=fill_value) return dtype diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 44650500e0f65..ce91276bc6cf4 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -25,6 +25,7 @@ Type, Union, cast, + overload, ) import warnings @@ -107,6 +108,8 @@ ) if TYPE_CHECKING: + from typing import Literal + from pandas import Series from pandas.core.arrays import ( DatetimeArray, @@ -1164,6 +1167,20 @@ def astype_td64_unit_conversion( return result +@overload +def astype_nansafe( + arr: np.ndarray, dtype: np.dtype, copy: bool = ..., skipna: bool = ... +) -> np.ndarray: + ... + + +@overload +def astype_nansafe( + arr: np.ndarray, dtype: ExtensionDtype, copy: bool = ..., skipna: bool = ... +) -> ExtensionArray: + ... + + def astype_nansafe( arr: np.ndarray, dtype: DtypeObj, copy: bool = True, skipna: bool = False ) -> ArrayLike: @@ -1190,14 +1207,10 @@ def astype_nansafe( flags = arr.flags flat = arr.ravel("K") result = astype_nansafe(flat, dtype, copy=copy, skipna=skipna) - order = "F" if flags.f_contiguous else "C" + order: Literal["C", "F"] = "F" if flags.f_contiguous else "C" # error: Item "ExtensionArray" of "Union[ExtensionArray, ndarray]" has no # attribute "reshape" - # error: No overload variant of "reshape" of "_ArrayOrScalarCommon" matches - # argument types "Tuple[int, ...]", "str" - return result.reshape( # type: ignore[union-attr,call-overload] - arr.shape, order=order - ) + return result.reshape(arr.shape, order=order) # type: ignore[union-attr] # We get here with 0-dim from sparse arr = np.atleast_1d(arr)