From 5253d39da1de2727910502d1359e69dde384cf01 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Fri, 19 Mar 2021 14:27:30 -0400 Subject: [PATCH 1/5] DOC/TYP: index.take --- pandas/core/indexes/base.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 3a468758ab3fd..57baadd2db14a 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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[ArrayLike, Sequence[int]], + axis: int = 0, + allow_fill: bool = False, + fill_value=None, + **kwargs, + ): 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) From 67b62992fba939817717a0fcbe05031305bb593a Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Fri, 19 Mar 2021 14:51:35 -0400 Subject: [PATCH 2/5] Fix default --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 57baadd2db14a..37f5d4d3e3491 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -911,7 +911,7 @@ def take( self, indices: Union[ArrayLike, Sequence[int]], axis: int = 0, - allow_fill: bool = False, + allow_fill: bool = True, fill_value=None, **kwargs, ): From b6097670185a25086ea77ace633710a73543a591 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin <37011898+mzeitlin11@users.noreply.github.com> Date: Mon, 22 Mar 2021 13:04:39 -0400 Subject: [PATCH 3/5] Update pandas/core/indexes/base.py Co-authored-by: Simon Hawkins --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 37f5d4d3e3491..ca144911984f0 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -886,7 +886,7 @@ def astype(self, dtype, copy=True): Parameters ---------- - indices : array_like + indices : array-like Indices to be taken. axis : int, optional The axis over which to select values, always 0. From 8b917def2c5514212baa1f77d543c6bae8d93044 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin <37011898+mzeitlin11@users.noreply.github.com> Date: Mon, 22 Mar 2021 13:07:26 -0400 Subject: [PATCH 4/5] Use AnyArrayLike instead of ArrayLike Co-authored-by: Simon Hawkins --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index ca144911984f0..f18f3520058f3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -909,7 +909,7 @@ def astype(self, dtype, copy=True): @Appender(_index_shared_docs["take"] % _index_doc_kwargs) def take( self, - indices: Union[ArrayLike, Sequence[int]], + indices: Union[AnyArrayLike, Sequence[int]], axis: int = 0, allow_fill: bool = True, fill_value=None, From c37be8c51b68f5df43dc73b9f2a4ae1c567be8de Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Fri, 2 Apr 2021 17:40:35 -0400 Subject: [PATCH 5/5] Remove complicated typing, doc fixes --- pandas/core/indexes/base.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index f8390cd20fc66..0e0350b3d602d 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -888,14 +888,15 @@ def astype(self, dtype, copy=True): axis : int, optional The axis over which to select values, always 0. allow_fill : bool, default True - fill_value : bool, default None + fill_value : scalar, default None If allow_fill=True and fill_value is not None, indices specified by - -1 is regarded as NA. If Index doesn't hold NA, raise ValueError. + -1 are regarded as NA. If Index doesn't hold NA, raise ValueError. Returns ------- Index - An index formed of elements at the given indices. + An index formed of elements at the given indices. Will be the same + type as self, except for RangeIndex. See Also -------- @@ -905,25 +906,17 @@ def astype(self, dtype, copy=True): @Appender(_index_shared_docs["take"] % _index_doc_kwargs) def take( - self, - indices: Union[AnyArrayLike, Sequence[int]], - axis: int = 0, - allow_fill: bool = True, - fill_value=None, - **kwargs, + self, indices, axis: int = 0, allow_fill: bool = True, fill_value=None, **kwargs ): if kwargs: nv.validate_take((), kwargs) - indices_as_array = ensure_platform_int(indices) - allow_fill = self._maybe_disallow_fill(allow_fill, fill_value, indices_as_array) + indices = ensure_platform_int(indices) + allow_fill = self._maybe_disallow_fill(allow_fill, fill_value, indices) # 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_as_array, - allow_fill=allow_fill, - fill_value=self._na_value, + self._values, indices, allow_fill=allow_fill, fill_value=self._na_value ) return type(self)._simple_new(taken, name=self.name)