-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TYP: __getitem__ method of EA (2nd pass) #37921
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 all commits
3d023db
06abda1
2f52aa6
f9fb7f5
be12bc6
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
TypeVar, | ||
Union, | ||
cast, | ||
overload, | ||
) | ||
import warnings | ||
|
||
|
@@ -266,9 +267,19 @@ def __array__(self, dtype=None) -> np.ndarray: | |
return np.array(list(self), dtype=object) | ||
return self._ndarray | ||
|
||
@overload | ||
def __getitem__(self, key: int) -> DTScalarOrNaT: | ||
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, can still return DatetimeLikeArrayT |
||
... | ||
|
||
@overload | ||
def __getitem__( | ||
self: DatetimeLikeArrayT, key: Union[slice, np.ndarray] | ||
) -> DatetimeLikeArrayT: | ||
... | ||
|
||
def __getitem__( | ||
self, key: Union[int, slice, np.ndarray] | ||
) -> Union[DatetimeLikeArrayMixin, DTScalarOrNaT]: | ||
self: DatetimeLikeArrayT, key: Union[int, slice, np.ndarray] | ||
) -> Union[DatetimeLikeArrayT, DTScalarOrNaT]: | ||
""" | ||
This getitem defers to the underlying array, which by-definition can | ||
only handle list-likes, slices, and integer scalars | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3222,14 +3222,8 @@ def _get_nearest_indexer(self, target: "Index", limit, tolerance) -> np.ndarray: | |
right_indexer = self.get_indexer(target, "backfill", limit=limit) | ||
|
||
target_values = target._values | ||
# error: Unsupported left operand type for - ("ExtensionArray") | ||
left_distances = np.abs( | ||
self._values[left_indexer] - target_values # type: ignore[operator] | ||
) | ||
# error: Unsupported left operand type for - ("ExtensionArray") | ||
right_distances = np.abs( | ||
self._values[right_indexer] - target_values # type: ignore[operator] | ||
) | ||
left_distances = np.abs(self._values[left_indexer] - target_values) | ||
right_distances = np.abs(self._values[right_indexer] - target_values) | ||
|
||
op = operator.lt if self.is_monotonic_increasing else operator.le | ||
indexer = np.where( | ||
|
@@ -3248,8 +3242,7 @@ def _filter_indexer_tolerance( | |
indexer: np.ndarray, | ||
tolerance, | ||
) -> np.ndarray: | ||
# error: Unsupported left operand type for - ("ExtensionArray") | ||
distance = abs(self._values[indexer] - target) # type: ignore[operator] | ||
distance = abs(self._values[indexer] - target) | ||
indexer = np.where(distance <= tolerance, indexer, -1) | ||
return indexer | ||
|
||
|
@@ -4546,9 +4539,8 @@ def asof_locs(self, where: "Index", mask) -> np.ndarray: | |
|
||
result = np.arange(len(self))[mask].take(locs) | ||
|
||
# TODO: overload return type of ExtensionArray.__getitem__ | ||
first_value = cast(Any, self._values[mask.argmax()]) | ||
result[(locs == 0) & (where._values < first_value)] = -1 | ||
first = mask.argmax() | ||
result[(locs == 0) & (where._values < self._values[first])] = -1 | ||
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. reverting change from previous PR |
||
|
||
return result | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from datetime import datetime, timedelta | ||
from typing import Any, cast | ||
from typing import Any | ||
|
||
import numpy as np | ||
|
||
|
@@ -673,10 +673,7 @@ def difference(self, other, sort=None): | |
|
||
if self.equals(other): | ||
# pass an empty PeriodArray with the appropriate dtype | ||
|
||
# TODO: overload DatetimeLikeArrayMixin.__getitem__ | ||
values = cast(PeriodArray, self._data[:0]) | ||
return type(self)._simple_new(values, name=self.name) | ||
return type(self)._simple_new(self._data[:0], name=self.name) | ||
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. reverting change from previous PR |
||
|
||
if is_object_dtype(other): | ||
return self.astype(object).difference(other).astype(self.dtype) | ||
|
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.
this can also return NDArrayBackedExtensionArrayT
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.
can you elaborate. is this for 2d EA?
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.
NDArrayBackedExtensionArray supports 2D, exactly
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.
so it also supports a tuple indexer? and I guess from that NDArrayBackedExtensionArray can't support nested data as it uses is_scalar checks.
so also need to change
EAScalarOrMissing = object # both scalar value and na_value can be any type
->ScalarOrScalarMissing = Scalar # both values and na_value must be scalars
?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.
Yes. For that matter i think even 1D will support 1-tuples
PandasArray can have object dtype, and Categorical can hold tuples. We never see 2D versions of those in practice though (yet).