-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TYP: Typing changes for ExtensionArray.astype #41251
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
Changes from 2 commits
dda21d1
da626a0
e2f27f7
ed106fc
7de25ae
d9ef38f
1807fd2
e683efe
ffee0e5
4815ede
a61e7ae
80d780e
5ce17cb
5d74047
1f19758
a6d7ebe
84777e6
6a135f2
9282e8e
640f4bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
Sequence, | ||
TypeVar, | ||
cast, | ||
overload, | ||
) | ||
|
||
import numpy as np | ||
|
@@ -26,6 +27,7 @@ | |
ArrayLike, | ||
Dtype, | ||
FillnaOptions, | ||
NpDtype, | ||
PositionalIndexer, | ||
Shape, | ||
) | ||
|
@@ -516,7 +518,15 @@ def nbytes(self) -> int: | |
# Additional Methods | ||
# ------------------------------------------------------------------------ | ||
|
||
def astype(self, dtype, copy=True): | ||
@overload | ||
def astype(self, dtype: NpDtype, copy: bool = True) -> np.ndarray: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dt64/td64 generally go to DTA/TDA There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think so. When you pass a numpy dtype, you get a
Dr-Irv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
... | ||
|
||
@overload | ||
def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could add an overload for ExtensionDtype and this overload could be str for the union return type if type_t[Union[str, float, int, complex, bool, object] is moved as suggested? the accepted strings for numpy is already known? so we should eventually be able to remove the union return type. |
||
... | ||
|
||
def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike: | ||
Dr-Irv marked this conversation as resolved.
Show resolved
Hide resolved
simonjayhawkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Cast to a NumPy array with 'dtype'. | ||
|
||
|
@@ -531,8 +541,9 @@ def astype(self, dtype, copy=True): | |
|
||
Returns | ||
------- | ||
array : ndarray | ||
NumPy ndarray with 'dtype' for its dtype. | ||
array : ArrayLike | ||
Dr-Irv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
An ExtensionArray if dtype StringDtype or same as that of underlying array. | ||
Dr-Irv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Otherwise a NumPy ndarray with 'dtype' for its dtype. | ||
""" | ||
from pandas.core.arrays.string_ import StringDtype | ||
|
||
|
@@ -548,7 +559,11 @@ def astype(self, dtype, copy=True): | |
# allow conversion to StringArrays | ||
return dtype.construct_array_type()._from_sequence(self, copy=False) | ||
|
||
return np.array(self, dtype=dtype, copy=copy) | ||
# error: Argument "dtype" to "array" has incompatible type | ||
# "Union[ExtensionDtype, dtype[Any]]"; expected "Union[dtype[Any], None, type, | ||
# _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any, Union[int, | ||
# Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]" | ||
return np.array(self, dtype=dtype, copy=copy) # type: ignore[arg-type] | ||
|
||
def isna(self) -> np.ndarray | ExtensionArraySupportsAnyAll: | ||
""" | ||
|
@@ -933,7 +948,9 @@ def _values_for_factorize(self) -> tuple[np.ndarray, Any]: | |
The values returned by this method are also used in | ||
:func:`pandas.util.hash_pandas_object`. | ||
""" | ||
return self.astype(object), np.nan | ||
# error: Incompatible return value type (got "Tuple[Union[ExtensionArray, | ||
# ndarray], float]", expected "Tuple[ndarray, Any]") | ||
return self.astype(object), np.nan # type: ignore[return-value] | ||
Dr-Irv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def factorize(self, na_sentinel: int = -1) -> tuple[np.ndarray, ExtensionArray]: | ||
""" | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,10 @@ | ||||||
from __future__ import annotations | ||||||
|
||||||
import numbers | ||||||
from typing import TYPE_CHECKING | ||||||
from typing import ( | ||||||
TYPE_CHECKING, | ||||||
overload, | ||||||
) | ||||||
import warnings | ||||||
|
||||||
import numpy as np | ||||||
|
@@ -13,6 +16,7 @@ | |||||
from pandas._typing import ( | ||||||
ArrayLike, | ||||||
Dtype, | ||||||
NpDtype, | ||||||
type_t, | ||||||
) | ||||||
from pandas.compat.numpy import function as nv | ||||||
|
@@ -392,7 +396,16 @@ def reconstruct(x): | |||||
def _coerce_to_array(self, value) -> tuple[np.ndarray, np.ndarray]: | ||||||
return coerce_to_array(value) | ||||||
|
||||||
def astype(self, dtype, copy: bool = True) -> ArrayLike: | ||||||
@overload | ||||||
def astype(self, dtype: NpDtype, copy: bool = True) -> np.ndarray: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
and elsewhere There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in next commit |
||||||
... | ||||||
|
||||||
@overload | ||||||
def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike: | ||||||
... | ||||||
|
||||||
def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike: | ||||||
|
||||||
""" | ||||||
Cast to a NumPy array or ExtensionArray with 'dtype'. | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
TypeVar, | ||
Union, | ||
cast, | ||
overload, | ||
) | ||
from warnings import ( | ||
catch_warnings, | ||
|
@@ -487,6 +488,14 @@ def _constructor(self) -> type[Categorical]: | |
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False): | ||
return Categorical(scalars, dtype=dtype, copy=copy) | ||
|
||
@overload | ||
def astype(self, dtype: NpDtype, copy: bool = True) -> np.ndarray: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think with dt64/td64 could return DTA/TDA? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't see that: >>> s = pd.Series(pd.to_datetime(["2021-03-15 12:05", "2021-04-05 5:10"]), dtype
="category")
>>> s
0 2021-03-15 12:05:00
1 2021-04-05 05:10:00
dtype: category
Categories (2, datetime64[ns]): [2021-03-15 12:05:00, 2021-04-05 05:10:00]
>>> s.dtype.categories.dtype
dtype('<M8[ns]')
>>> type(s.astype(s.dtype.categories.dtype).values)
<class 'numpy.ndarray'> |
||
... | ||
|
||
@overload | ||
def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike: | ||
... | ||
|
||
def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike: | ||
""" | ||
Coerce this type to another dtype | ||
|
Uh oh!
There was an error while loading. Please reload this page.