-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC/TYP: index.take return val #40521
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 4 commits
5253d39
67b6299
b609767
8b917de
eb6016a
2ff3ea5
a53cb32
d7ec12c
c37be8c
b23c43d
a16b108
74ca93a
9c34ff3
abd8e7c
a14be0b
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 |
---|---|---|
|
@@ -886,7 +886,7 @@ def astype(self, dtype, copy=True): | |
|
||
Parameters | ||
---------- | ||
indices : list | ||
indices : array-like | ||
Indices to be taken. | ||
axis : int, optional | ||
The axis over which to select values, always 0. | ||
|
@@ -897,8 +897,8 @@ def astype(self, dtype, copy=True): | |
|
||
Returns | ||
------- | ||
numpy.ndarray | ||
Elements of given indices. | ||
Index | ||
An index formed of elements at the given indices. | ||
|
||
See Also | ||
-------- | ||
|
@@ -907,16 +907,26 @@ def astype(self, dtype, copy=True): | |
""" | ||
|
||
@Appender(_index_shared_docs["take"] % _index_doc_kwargs) | ||
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs): | ||
def take( | ||
self, | ||
indices: Union[AnyArrayLike, Sequence[int]], | ||
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. isnt Sequence[int] going to cover the AnyArrayLike case? 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 even with 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. this is too complicated and specific an annotation to put here, leave this untyped for now, or put it in a typing alias, but then we should enforce it, alt we shouldn't allow non-array-likes here 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. Left untyped for now, now mostly just a doc fix |
||
axis: int = 0, | ||
allow_fill: bool = True, | ||
fill_value=None, | ||
**kwargs, | ||
): | ||
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. Any advice on typing this return value? Can almost use the TypeVar 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 in the base class use 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 tried something along those lines originally - the problem I was running into is how to handle subclasses which rely on parent implementations. For example, for
to essentially type the child while still relying on the inherited implementation. But couldn't figure out a way to achieve this. |
||
if kwargs: | ||
nv.validate_take((), kwargs) | ||
indices = ensure_platform_int(indices) | ||
allow_fill = self._maybe_disallow_fill(allow_fill, fill_value, indices) | ||
indices_as_array = ensure_platform_int(indices) | ||
allow_fill = self._maybe_disallow_fill(allow_fill, fill_value, indices_as_array) | ||
|
||
# Note: we discard fill_value and use self._na_value, only relevant | ||
# in the case where allow_fill is True and fill_value is not None | ||
taken = algos.take( | ||
self._values, indices, allow_fill=allow_fill, fill_value=self._na_value | ||
self._values, | ||
indices_as_array, | ||
allow_fill=allow_fill, | ||
fill_value=self._na_value, | ||
) | ||
return type(self)._simple_new(taken, name=self.name) | ||
|
||
|
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.
i think Sequence[int] would be more helpful
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.
Happy to change, my only question would be if we want to be using type hints for user facing API. The numpy documentation for
take
just usesarray_like
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.
we shouldn't use typing notation for docstrings. https://pandas.pydata.org/pandas-docs/dev/development/contributing_docstring.html#parameter-types
and from that guide...
but the guide doesn't mention sequence.
maybe
sequence of int
is allowed, but I think matching numpy is fine.