Skip to content

TYP: Arraylike alias change follow up #40398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@
from pandas.tseries import frequencies

if TYPE_CHECKING:
from typing import Literal

from pandas.core.arrays import (
DatetimeArray,
TimedeltaArray,
Expand Down Expand Up @@ -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:
...
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 2 additions & 7 deletions pandas/core/arrays/sparse/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Type,
Union,
cast,
overload,
)
import warnings

Expand Down Expand Up @@ -107,6 +108,8 @@
)

if TYPE_CHECKING:
from typing import Literal

from pandas import Series
from pandas.core.arrays import (
DatetimeArray,
Expand Down Expand Up @@ -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:
Expand All @@ -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"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel this is not necessarily a false positive. I think this maybe assuming that if ndim > 1, we have a numpy array. but a 2d EA will have additional methods such as reshape.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah even if we got here with NDArrayBackedExtensionArray (which does have reshape) itd break bc it doesn't have flags. ignoring for the time being seems reasonable

# 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)
Expand Down