Skip to content

TYP: to_numpy #41751

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion pandas/core/array_algos/putmask.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def extract_bool_array(mask: ArrayLike) -> np.ndarray:
# We could have BooleanArray, Sparse[bool], ...
# Except for BooleanArray, this is equivalent to just
# np.asarray(mask, dtype=bool)
mask = mask.to_numpy(dtype=bool, na_value=False)
mask = mask.to_numpy(dtype=np.dtype("bool"), na_value=False)
Copy link
Member

Choose a reason for hiding this comment

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

this shouldn't need to be changed. Not sure without checking when the NpDtype alias was added, but pretty sure from memory that this is causing several issues (e.g. #41203 (review)) and needs to be fixed first. #41185

Copy link
Member Author

Choose a reason for hiding this comment

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

either way i guess. at some point it is going to be cast to a np.dtype object, so i see it as harmless

Copy link
Member

Choose a reason for hiding this comment

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

yes, but the change means that the to_numpy annotation is wrong and since it's public will give false positives to users.


mask = np.asarray(mask, dtype=bool)
return mask
Expand Down
10 changes: 3 additions & 7 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
ArrayLike,
Dtype,
FillnaOptions,
NpDtype,
PositionalIndexer,
Shape,
)
Expand Down Expand Up @@ -429,7 +430,7 @@ def __ne__(self, other: Any) -> ArrayLike: # type: ignore[override]

def to_numpy(
self,
dtype: Dtype | None = None,
dtype: NpDtype | None = None,
copy: bool = False,
na_value=lib.no_default,
) -> np.ndarray:
Expand Down Expand Up @@ -458,12 +459,7 @@ def to_numpy(
-------
numpy.ndarray
"""
# error: Argument "dtype" to "asarray" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float], Type[int],
# Type[complex], Type[bool], Type[object], None]"; expected "Union[dtype[Any],
# None, type, _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any, Union[int,
# Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]"
result = np.asarray(self, dtype=dtype) # type: ignore[arg-type]
result = np.asarray(self, dtype=dtype)
if copy or na_value is not lib.no_default:
result = result.copy()
if na_value is not lib.no_default:
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,7 @@ def __len__(self) -> int:
def __invert__(self: BaseMaskedArrayT) -> BaseMaskedArrayT:
return type(self)(~self._data, self._mask.copy())

# error: Argument 1 of "to_numpy" is incompatible with supertype "ExtensionArray";
# supertype defines the argument type as "Union[ExtensionDtype, str, dtype[Any],
# Type[str], Type[float], Type[int], Type[complex], Type[bool], Type[object], None]"
def to_numpy( # type: ignore[override]
def to_numpy(
self,
dtype: NpDtype | None = None,
copy: bool = False,
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,7 @@ def skew(
# ------------------------------------------------------------------------
# Additional Methods

# error: Argument 1 of "to_numpy" is incompatible with supertype "ExtensionArray";
# supertype defines the argument type as "Union[ExtensionDtype, str, dtype[Any],
# Type[str], Type[float], Type[int], Type[complex], Type[bool], Type[object], None]"
def to_numpy( # type: ignore[override]
def to_numpy(
self,
dtype: NpDtype | None = None,
copy: bool = False,
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,7 @@ def __arrow_array__(self, type=None):
"""Convert myself to a pyarrow Array or ChunkedArray."""
return self._data

# error: Argument 1 of "to_numpy" is incompatible with supertype "ExtensionArray";
# supertype defines the argument type as "Union[ExtensionDtype, str, dtype[Any],
# Type[str], Type[float], Type[int], Type[complex], Type[bool], Type[object], None]"
def to_numpy( # type: ignore[override]
def to_numpy(
self,
dtype: NpDtype | None = None,
copy: bool = False,
Expand Down
11 changes: 3 additions & 8 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import pandas._libs.lib as lib
from pandas._typing import (
ArrayLike,
Dtype,
DtypeObj,
FrameOrSeries,
IndexLabel,
NpDtype,
Shape,
final,
)
Expand Down Expand Up @@ -413,7 +413,7 @@ def array(self) -> ExtensionArray:

def to_numpy(
self,
dtype: Dtype | None = None,
dtype: NpDtype | None = None,
copy: bool = False,
na_value=lib.no_default,
**kwargs,
Expand Down Expand Up @@ -523,12 +523,7 @@ def to_numpy(
f"to_numpy() got an unexpected keyword argument '{bad_keys}'"
)

# error: Argument "dtype" to "asarray" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float], Type[int],
# Type[complex], Type[bool], Type[object], None]"; expected "Union[dtype[Any],
# None, type, _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any, Union[int,
# Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]"
result = np.asarray(self._values, dtype=dtype) # type: ignore[arg-type]
result = np.asarray(self._values, dtype=dtype)
# TODO(GH-24345): Avoid potential double copy
if copy or na_value is not lib.no_default:
result = result.copy()
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2424,12 +2424,12 @@ def pre_processor(vals: ArrayLike) -> tuple[np.ndarray, np.dtype | None]:
inference: np.dtype | None = None
if is_integer_dtype(vals.dtype):
if isinstance(vals, ExtensionArray):
out = vals.to_numpy(dtype=float, na_value=np.nan)
out = vals.to_numpy(dtype="float64", na_value=np.nan)
else:
out = vals
inference = np.dtype(np.int64)
elif is_bool_dtype(vals.dtype) and isinstance(vals, ExtensionArray):
out = vals.to_numpy(dtype=float, na_value=np.nan)
out = vals.to_numpy(dtype="float64", na_value=np.nan)
elif is_datetime64_dtype(vals.dtype):
inference = np.dtype("datetime64[ns]")
out = np.asarray(vals).astype(float)
Expand Down
26 changes: 8 additions & 18 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
ArrayLike,
Dtype,
DtypeObj,
NpDtype,
Shape,
type_t,
)
Expand Down Expand Up @@ -1385,7 +1386,7 @@ def to_dict(self, copy: bool = True):
def as_array(
self,
transpose: bool = False,
dtype: Dtype | None = None,
dtype: NpDtype | None = None,
copy: bool = False,
na_value=lib.no_default,
) -> np.ndarray:
Expand All @@ -1396,7 +1397,7 @@ def as_array(
----------
transpose : bool, default False
If True, transpose the return array.
dtype : object, default None
dtype : str or numpy.dtype, optional
Data type of the return array.
copy : bool, default False
If True then guarantee that a copy is returned. A value of
Expand Down Expand Up @@ -1430,12 +1431,7 @@ def as_array(
else:
arr = np.asarray(blk.get_values())
if dtype:
# error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has
# incompatible type "Union[ExtensionDtype, str, dtype[Any],
# Type[object]]"; expected "Union[dtype[Any], None, type,
# _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any, Union[int,
# Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]"
arr = arr.astype(dtype, copy=False) # type: ignore[arg-type]
arr = arr.astype(dtype, copy=False)
else:
arr = self._interleave(dtype=dtype, na_value=na_value)
# The underlying data was copied within _interleave
Expand Down Expand Up @@ -1468,12 +1464,9 @@ def _interleave(
elif is_dtype_equal(dtype, str):
dtype = np.dtype("object")

# error: Argument "dtype" to "empty" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; expected
# "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any, int],
# Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict,
# Tuple[Any, Any]]]"
result = np.empty(self.shape, dtype=dtype) # type: ignore[arg-type]
dtype = cast(np.dtype, dtype)

result = np.empty(self.shape, dtype=dtype)

itemmask = np.zeros(self.shape[0])

Expand All @@ -1488,10 +1481,7 @@ def _interleave(
dtype=dtype, na_value=na_value
)
else:
# error: Argument 1 to "get_values" of "Block" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; expected
# "Union[dtype[Any], ExtensionDtype, None]"
arr = blk.get_values(dtype) # type: ignore[arg-type]
arr = blk.get_values(dtype)
result[rl.indexer] = arr
itemmask[rl.indexer] = 1

Expand Down