-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Fix typing for extension arrays and extension dtypes without isin and astype #40421
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 1 commit
f2c52a4
d7ff8d3
49fa06e
03b2c4a
3e19958
6861901
9be6486
260b367
6276725
4dafaca
8b2cee2
3a7d839
a21bb60
8cd6b76
e0e0131
6a6a21f
bf753e6
c9795a5
d452842
3c2c78b
548c198
db8ed9b
f8191f8
575645f
6f8fcb5
3ea2420
c83a628
5bb24d4
ad1ab3b
63c3d6d
1882074
1274a76
363e203
01c942c
1196132
66d5da4
5411998
9b7481d
4bd3422
3b1ff79
5719daa
dbfb3a2
d41bf91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,7 @@ | |
TYPE_CHECKING, | ||
Any, | ||
Callable, | ||
Dict, | ||
Iterator, | ||
Optional, | ||
Sequence, | ||
TypeVar, | ||
cast, | ||
|
@@ -28,6 +26,7 @@ | |
ArrayLike, | ||
Dtype, | ||
NpDtype, | ||
PositionalIndexer, | ||
Shape, | ||
) | ||
from pandas.compat import set_function_name | ||
|
@@ -389,7 +388,7 @@ def __iter__(self) -> Iterator[Any]: | |
for i in range(len(self)): | ||
yield self[i] | ||
|
||
def __contains__(self, item: Any) -> Union[bool, np.bool_]: | ||
def __contains__(self, item) -> bool | np.bool_: | ||
""" | ||
Return for `item in self`. | ||
""" | ||
|
@@ -428,9 +427,9 @@ def __ne__(self, other: Any) -> ArrayLike: # type: ignore[override] | |
|
||
def to_numpy( | ||
self, | ||
dtype: Optional[NpDtype] = None, | ||
dtype: NpDtype | None = None, | ||
copy: bool = False, | ||
na_value: Optional[Any] = lib.no_default, | ||
na_value: Any | None = lib.no_default, | ||
) -> np.ndarray: | ||
""" | ||
Convert to a NumPy ndarray. | ||
|
@@ -683,9 +682,9 @@ def argmax(self, skipna: bool = True) -> int: | |
|
||
def fillna( | ||
self, | ||
value: Optional[Union[Any, ArrayLike]] = None, | ||
method: Optional[Literal["backfill", "bfill", "ffill", "pad"]] = None, | ||
limit: Optional[int] = None, | ||
value: Any | ArrayLike | None = None, | ||
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. isn't the ArrayLike (and None) redundant? 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. Yes and no. We want to indicate that you can provide a single value (but for an |
||
method: Literal["backfill", "bfill", "ffill", "pad"] | None = None, | ||
limit: int | None = None, | ||
) -> ExtensionArray: | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Fill NA/NaN values using the specified method. | ||
|
@@ -818,7 +817,7 @@ def searchsorted( | |
self, | ||
value: Sequence[Any], | ||
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. Sequence is not yet compatible with EA (and others). see #28770 |
||
side: Literal["left", "right"] = "left", | ||
sorter: Optional[Sequence[Any]] = None, | ||
sorter: Sequence[Any] | None = None, | ||
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. from the docstring sorter is an arraylike of ints. |
||
) -> np.ndarray: | ||
""" | ||
Find indices where elements should be inserted to maintain order. | ||
|
@@ -1042,7 +1041,7 @@ def factorize(self, na_sentinel: int = -1) -> tuple[np.ndarray, ExtensionArray]: | |
@Appender(_extension_array_shared_docs["repeat"]) | ||
def repeat( | ||
self, | ||
repeats: Union[int, Sequence[int]], | ||
repeats: int | Sequence[int], | ||
axis: Literal[None, 0] = None, | ||
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. not seen Literal[None] used b4. |
||
) -> ExtensionArray: | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nv.validate_repeat((), {"axis": axis}) | ||
|
@@ -1241,9 +1240,7 @@ def transpose(self, *axes: int) -> ExtensionArray: | |
def T(self) -> ExtensionArray: | ||
return self.transpose() | ||
|
||
def ravel( | ||
self, order: Optional[Literal["C", "F", "A", "K"]] = "C" | ||
) -> ExtensionArray: | ||
def ravel(self, order: Literal["C", "F", "A", "K"] | None = "C") -> ExtensionArray: | ||
""" | ||
Return a flattened view on this array. | ||
|
||
|
@@ -1323,7 +1320,7 @@ def __hash__(self) -> int: | |
# ------------------------------------------------------------------------ | ||
# Non-Optimized Default Methods | ||
|
||
def delete(self, loc: Union[int, Sequence[int]]) -> ExtensionArray: | ||
def delete(self, loc: int | Sequence[int]) -> ExtensionArray: | ||
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. please revert the typevar change. |
||
indexer = np.delete(np.arange(len(self)), loc) | ||
return self.take(indexer) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
from typing import ( | ||
Sequence, | ||
TypeVar, | ||
Union, | ||
cast, | ||
) | ||
|
||
|
@@ -1519,7 +1518,7 @@ def delete(self: IntervalArrayT, loc) -> IntervalArrayT: | |
|
||
@Appender(_extension_array_shared_docs["repeat"] % _shared_docs_kwargs) | ||
def repeat( | ||
self: IntervalArrayT, repeats: Union[int, Sequence[int]], axis=None | ||
self: IntervalArrayT, repeats: int | Sequence[int], axis=None | ||
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. is this compatible with numpy where the type is int or array of ints? 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. Compatible in what sense? numpy takes a superset of |
||
) -> IntervalArrayT: | ||
nv.validate_repeat((), {"axis": axis}) | ||
left_repeat = self.left.repeat(repeats) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,15 +227,15 @@ def _chk_pyarrow_available(cls) -> None: | |
raise ImportError(msg) | ||
|
||
@classmethod | ||
def _from_sequence(cls, scalars, dtype: Optional[DtypeArg] = None, copy=False): | ||
def _from_sequence(cls, scalars, dtype: DtypeArg | None = None, copy: bool = False): | ||
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. DtypeArg? |
||
cls._chk_pyarrow_available() | ||
# convert non-na-likes to str, and nan-likes to ArrowStringDtype.na_value | ||
scalars = lib.ensure_string_array(scalars, copy=False) | ||
return cls(pa.array(scalars, type=pa.string(), from_pandas=True)) | ||
|
||
@classmethod | ||
def _from_sequence_of_strings( | ||
cls, strings, dtype: Optional[DtypeArg] = None, copy=False | ||
cls, strings, dtype: DtypeArg | None = None, copy: bool = False | ||
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. same |
||
): | ||
return cls._from_sequence(strings, dtype=dtype, copy=copy) | ||
|
||
|
@@ -693,7 +693,7 @@ def value_counts(self, dropna: bool = True) -> Series: | |
|
||
_str_na_value = ArrowStringDtype.na_value | ||
|
||
def _str_map(self, f, na_value=None, dtype: Dtype | None = None): | ||
def _str_map(self, f, na_value=None, dtype: DtypeArg | None = None): | ||
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. same |
||
# TODO: de-duplicate with StringArray method. This method is moreless copy and | ||
# paste. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -337,7 +337,10 @@ def _get_unique_index(self): | |
return self | ||
|
||
result = self._data.unique() | ||
return type(self)._simple_new(result, name=self.name) | ||
# error: Argument 1 to "_simple_new" of "ExtensionIndex" has incompatible | ||
# type "ExtensionArray"; expected "Union[IntervalArray, | ||
# NDArrayBackedExtensionArray]" | ||
return type(self)._simple_new(result, name=self.name) # type: ignore[arg-type] | ||
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. revealed type of result will be |
||
|
||
@doc(Index.map) | ||
def map(self, mapper, na_action=None): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None seems redundant here.