From 416a1a1d63049b0b3075a32665685163c84b0421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 5 Sep 2021 10:48:37 -0400 Subject: [PATCH 01/15] TYP: ExtensionArray.take accept np.ndarray --- pandas/core/algorithms.py | 7 ++++++- pandas/core/array_algos/take.py | 9 +-------- pandas/core/arrays/_mixins.py | 7 +++---- pandas/core/arrays/base.py | 2 +- pandas/core/arrays/string_arrow.py | 9 +++++---- pandas/core/indexes/base.py | 8 ++------ 6 files changed, 18 insertions(+), 24 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index bb4e0dff0f4c7..7e10a242420f5 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -27,6 +27,7 @@ AnyArrayLike, ArrayLike, DtypeObj, + PositionalIndexer, Scalar, npt, ) @@ -1431,7 +1432,11 @@ def get_indexer(current_indexer, other_indexer): def take( - arr, indices: np.ndarray, axis: int = 0, allow_fill: bool = False, fill_value=None + arr, + indices: PositionalIndexer, + axis: int = 0, + allow_fill: bool = False, + fill_value=None, ): """ Take elements from an array. diff --git a/pandas/core/array_algos/take.py b/pandas/core/array_algos/take.py index 201e177d8bb10..6d350cfa2c1d6 100644 --- a/pandas/core/array_algos/take.py +++ b/pandas/core/array_algos/take.py @@ -178,14 +178,7 @@ def take_1d( """ if not isinstance(arr, np.ndarray): # ExtensionArray -> dispatch to their method - - # error: Argument 1 to "take" of "ExtensionArray" has incompatible type - # "ndarray"; expected "Sequence[int]" - return arr.take( - indexer, # type: ignore[arg-type] - fill_value=fill_value, - allow_fill=allow_fill, - ) + return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill) if not allow_fill: return arr.take(indexer) diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index 4c7ccc2f16477..10c52f44ab503 100644 --- a/pandas/core/arrays/_mixins.py +++ b/pandas/core/arrays/_mixins.py @@ -17,6 +17,7 @@ from pandas._libs.arrays import NDArrayBacked from pandas._typing import ( F, + PositionalIndexer, PositionalIndexer2D, PositionalIndexerTuple, ScalarIndexer, @@ -101,7 +102,7 @@ def _validate_scalar(self, value): def take( self: NDArrayBackedExtensionArrayT, - indices: Sequence[int], + indices: PositionalIndexer, *, allow_fill: bool = False, fill_value: Any = None, @@ -112,9 +113,7 @@ def take( new_data = take( self._ndarray, - # error: Argument 2 to "take" has incompatible type "Sequence[int]"; - # expected "ndarray" - indices, # type: ignore[arg-type] + indices, allow_fill=allow_fill, fill_value=fill_value, axis=axis, diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 088c44334495c..4abaa0f8c44ff 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -1076,7 +1076,7 @@ def repeat(self, repeats: int | Sequence[int], axis: int | None = None): def take( self: ExtensionArrayT, - indices: Sequence[int], + indices: PositionalIndexer, *, allow_fill: bool = False, fill_value: Any = None, diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index 9411d3535e06f..42fcc9713a8f4 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -307,9 +307,7 @@ def __getitem__( if not len(item): return type(self)(pa.chunked_array([], type=pa.string())) elif is_integer_dtype(item.dtype): - # error: Argument 1 to "take" of "ArrowStringArray" has incompatible - # type "ndarray"; expected "Sequence[int]" - return self.take(item) # type: ignore[arg-type] + return self.take(item) elif is_bool_dtype(item.dtype): return type(self)(self._data.filter(item)) else: @@ -513,7 +511,10 @@ def __setitem__(self, key: int | slice | np.ndarray, value: Any) -> None: self[k] = v def take( - self, indices: Sequence[int], allow_fill: bool = False, fill_value: Any = None + self, + indices: PositionalIndexer, + allow_fill: bool = False, + fill_value: Any = None, ): """ Take elements from an array. diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 6887b919cc7d6..15cec99aff0eb 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4318,12 +4318,8 @@ def _join_non_unique( ) mask = left_idx == -1 - # error: Argument 1 to "take" of "ExtensionArray" has incompatible - # type "ndarray[Any, dtype[signedinteger[Any]]]"; expected "Sequence[int]" - join_array = self._values.take(left_idx) # type: ignore[arg-type] - # error: Argument 1 to "take" of "ExtensionArray" has incompatible type - # "ndarray[Any, dtype[signedinteger[Any]]]"; expected "Sequence[int]" - right = other._values.take(right_idx) # type: ignore[arg-type] + join_array = self._values.take(left_idx) + right = other._values.take(right_idx) if isinstance(join_array, np.ndarray): np.putmask(join_array, mask, right) From cf463dfd5dab8eb0c6eafa039abd29a4db2b80f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 5 Sep 2021 12:12:39 -0400 Subject: [PATCH 02/15] remove unused import --- pandas/core/arrays/string_arrow.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index 42fcc9713a8f4..9d80b13a9556d 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -5,7 +5,6 @@ from typing import ( TYPE_CHECKING, Any, - Sequence, Union, cast, overload, From a97490571e198ba174b8eb13c4d2e2da85573501 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 5 Sep 2021 14:25:23 -0400 Subject: [PATCH 03/15] NonScalarPositionalIndexer --- pandas/core/algorithms.py | 4 ++-- pandas/core/arrays/_mixins.py | 4 ++-- pandas/core/arrays/base.py | 3 ++- pandas/core/arrays/string_arrow.py | 3 ++- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 7e10a242420f5..0ed1d20f454c1 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -27,7 +27,7 @@ AnyArrayLike, ArrayLike, DtypeObj, - PositionalIndexer, + NonScalarPositionalIndexer, Scalar, npt, ) @@ -1433,7 +1433,7 @@ def get_indexer(current_indexer, other_indexer): def take( arr, - indices: PositionalIndexer, + indices: NonScalarPositionalIndexer, axis: int = 0, allow_fill: bool = False, fill_value=None, diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index 10c52f44ab503..be6999cd40b42 100644 --- a/pandas/core/arrays/_mixins.py +++ b/pandas/core/arrays/_mixins.py @@ -17,7 +17,7 @@ from pandas._libs.arrays import NDArrayBacked from pandas._typing import ( F, - PositionalIndexer, + NonScalarPositionalIndexer, PositionalIndexer2D, PositionalIndexerTuple, ScalarIndexer, @@ -102,7 +102,7 @@ def _validate_scalar(self, value): def take( self: NDArrayBackedExtensionArrayT, - indices: PositionalIndexer, + indices: NonScalarPositionalIndexer, *, allow_fill: bool = False, fill_value: Any = None, diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 4abaa0f8c44ff..88e1e680d31a1 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -29,6 +29,7 @@ AstypeArg, Dtype, FillnaOptions, + NonScalarPositionalIndexer, PositionalIndexer, ScalarIndexer, SequenceIndexer, @@ -1076,7 +1077,7 @@ def repeat(self, repeats: int | Sequence[int], axis: int | None = None): def take( self: ExtensionArrayT, - indices: PositionalIndexer, + indices: NonScalarPositionalIndexer, *, allow_fill: bool = False, fill_value: Any = None, diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index 9d80b13a9556d..d7884f39b6755 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -18,6 +18,7 @@ ) from pandas._typing import ( Dtype, + NonScalarPositionalIndexer, NpDtype, PositionalIndexer, Scalar, @@ -511,7 +512,7 @@ def __setitem__(self, key: int | slice | np.ndarray, value: Any) -> None: def take( self, - indices: PositionalIndexer, + indices: NonScalarPositionalIndexer, allow_fill: bool = False, fill_value: Any = None, ): From 65adf47e45b460111efbc7236d360d3a9495b123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 10 Sep 2021 11:13:14 -0400 Subject: [PATCH 04/15] use SequenceIndexer --- pandas/core/algorithms.py | 4 ++-- pandas/core/arrays/_mixins.py | 3 +-- pandas/core/arrays/base.py | 3 +-- pandas/core/arrays/string_arrow.py | 3 +-- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 0ed1d20f454c1..a014590c4862f 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -27,8 +27,8 @@ AnyArrayLike, ArrayLike, DtypeObj, - NonScalarPositionalIndexer, Scalar, + SequenceIndexer, npt, ) from pandas.util._decorators import doc @@ -1433,7 +1433,7 @@ def get_indexer(current_indexer, other_indexer): def take( arr, - indices: NonScalarPositionalIndexer, + indices: SequenceIndexer, axis: int = 0, allow_fill: bool = False, fill_value=None, diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index be6999cd40b42..010227427dc0f 100644 --- a/pandas/core/arrays/_mixins.py +++ b/pandas/core/arrays/_mixins.py @@ -17,7 +17,6 @@ from pandas._libs.arrays import NDArrayBacked from pandas._typing import ( F, - NonScalarPositionalIndexer, PositionalIndexer2D, PositionalIndexerTuple, ScalarIndexer, @@ -102,7 +101,7 @@ def _validate_scalar(self, value): def take( self: NDArrayBackedExtensionArrayT, - indices: NonScalarPositionalIndexer, + indices: SequenceIndexer, *, allow_fill: bool = False, fill_value: Any = None, diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 88e1e680d31a1..e91b62c119a23 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -29,7 +29,6 @@ AstypeArg, Dtype, FillnaOptions, - NonScalarPositionalIndexer, PositionalIndexer, ScalarIndexer, SequenceIndexer, @@ -1077,7 +1076,7 @@ def repeat(self, repeats: int | Sequence[int], axis: int | None = None): def take( self: ExtensionArrayT, - indices: NonScalarPositionalIndexer, + indices: SequenceIndexer, *, allow_fill: bool = False, fill_value: Any = None, diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index d7884f39b6755..c28ee4aff4855 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -18,7 +18,6 @@ ) from pandas._typing import ( Dtype, - NonScalarPositionalIndexer, NpDtype, PositionalIndexer, Scalar, @@ -512,7 +511,7 @@ def __setitem__(self, key: int | slice | np.ndarray, value: Any) -> None: def take( self, - indices: NonScalarPositionalIndexer, + indices: SequenceIndexer, allow_fill: bool = False, fill_value: Any = None, ): From e296f0ee5f9a20dd615245bd680da0a42824a809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sat, 11 Sep 2021 10:13:38 -0400 Subject: [PATCH 05/15] include Sequence --- pandas/_typing.py | 1 + pandas/core/algorithms.py | 3 ++- pandas/core/arrays/_mixins.py | 3 ++- pandas/core/arrays/base.py | 3 ++- pandas/core/arrays/string_arrow.py | 3 ++- 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 9ed31dc3738f3..84f70094444b1 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -216,6 +216,7 @@ # Using List[int] here rather than Sequence[int] to disallow tuples. ScalarIndexer = Union[int, np.integer] SequenceIndexer = Union[slice, List[int], np.ndarray] +GeneralSequenceIndexer = Union[Sequence[int], SequenceIndexer] PositionalIndexer = Union[ScalarIndexer, SequenceIndexer] PositionalIndexerTuple = Tuple[PositionalIndexer, PositionalIndexer] PositionalIndexer2D = Union[PositionalIndexer, PositionalIndexerTuple] diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index a014590c4862f..b7bf138f08475 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -27,6 +27,7 @@ AnyArrayLike, ArrayLike, DtypeObj, + GeneralSequenceIndexer, Scalar, SequenceIndexer, npt, @@ -1433,7 +1434,7 @@ def get_indexer(current_indexer, other_indexer): def take( arr, - indices: SequenceIndexer, + indices: GeneralSequenceIndexer, axis: int = 0, allow_fill: bool = False, fill_value=None, diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index 010227427dc0f..0461d3cc1bb53 100644 --- a/pandas/core/arrays/_mixins.py +++ b/pandas/core/arrays/_mixins.py @@ -17,6 +17,7 @@ from pandas._libs.arrays import NDArrayBacked from pandas._typing import ( F, + GeneralSequenceIndexer, PositionalIndexer2D, PositionalIndexerTuple, ScalarIndexer, @@ -101,7 +102,7 @@ def _validate_scalar(self, value): def take( self: NDArrayBackedExtensionArrayT, - indices: SequenceIndexer, + indices: GeneralSequenceIndexer, *, allow_fill: bool = False, fill_value: Any = None, diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index e91b62c119a23..a2d78cf984534 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -29,6 +29,7 @@ AstypeArg, Dtype, FillnaOptions, + GeneralSequenceIndexer, PositionalIndexer, ScalarIndexer, SequenceIndexer, @@ -1076,7 +1077,7 @@ def repeat(self, repeats: int | Sequence[int], axis: int | None = None): def take( self: ExtensionArrayT, - indices: SequenceIndexer, + indices: GeneralSequenceIndexer, *, allow_fill: bool = False, fill_value: Any = None, diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index c28ee4aff4855..c9880b62b9ff8 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -18,6 +18,7 @@ ) from pandas._typing import ( Dtype, + GeneralSequenceIndexer, NpDtype, PositionalIndexer, Scalar, @@ -511,7 +512,7 @@ def __setitem__(self, key: int | slice | np.ndarray, value: Any) -> None: def take( self, - indices: SequenceIndexer, + indices: GeneralSequenceIndexer, allow_fill: bool = False, fill_value: Any = None, ): From eba3f07d3155f8fb7fa61a7f357916d6a657c7cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sat, 11 Sep 2021 11:39:47 -0400 Subject: [PATCH 06/15] unused import --- pandas/core/algorithms.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index b7bf138f08475..e664c3e5845b9 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -29,7 +29,6 @@ DtypeObj, GeneralSequenceIndexer, Scalar, - SequenceIndexer, npt, ) from pandas.util._decorators import doc From a05592b7ca050ef8e4b80fce1333509c4dcd47f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Tue, 14 Sep 2021 12:21:06 -0400 Subject: [PATCH 07/15] Sequence and ndarray --- pandas/_typing.py | 2 +- pandas/core/algorithms.py | 4 ++-- pandas/core/arrays/_mixins.py | 4 ++-- pandas/core/arrays/base.py | 4 ++-- pandas/core/arrays/string_arrow.py | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 84f70094444b1..9c9fef2e6cfaf 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -216,7 +216,7 @@ # Using List[int] here rather than Sequence[int] to disallow tuples. ScalarIndexer = Union[int, np.integer] SequenceIndexer = Union[slice, List[int], np.ndarray] -GeneralSequenceIndexer = Union[Sequence[int], SequenceIndexer] +TakeIndexer = Union[Sequence[int], np.ndarray] PositionalIndexer = Union[ScalarIndexer, SequenceIndexer] PositionalIndexerTuple = Tuple[PositionalIndexer, PositionalIndexer] PositionalIndexer2D = Union[PositionalIndexer, PositionalIndexerTuple] diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index e664c3e5845b9..b8fb646001523 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -27,8 +27,8 @@ AnyArrayLike, ArrayLike, DtypeObj, - GeneralSequenceIndexer, Scalar, + TakeIndexer, npt, ) from pandas.util._decorators import doc @@ -1433,7 +1433,7 @@ def get_indexer(current_indexer, other_indexer): def take( arr, - indices: GeneralSequenceIndexer, + indices: TakeIndexer, axis: int = 0, allow_fill: bool = False, fill_value=None, diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index 0461d3cc1bb53..22062f7d165f9 100644 --- a/pandas/core/arrays/_mixins.py +++ b/pandas/core/arrays/_mixins.py @@ -17,12 +17,12 @@ from pandas._libs.arrays import NDArrayBacked from pandas._typing import ( F, - GeneralSequenceIndexer, PositionalIndexer2D, PositionalIndexerTuple, ScalarIndexer, SequenceIndexer, Shape, + TakeIndexer, npt, type_t, ) @@ -102,7 +102,7 @@ def _validate_scalar(self, value): def take( self: NDArrayBackedExtensionArrayT, - indices: GeneralSequenceIndexer, + indices: TakeIndexer, *, allow_fill: bool = False, fill_value: Any = None, diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index a2d78cf984534..34fa65a360b01 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -29,11 +29,11 @@ AstypeArg, Dtype, FillnaOptions, - GeneralSequenceIndexer, PositionalIndexer, ScalarIndexer, SequenceIndexer, Shape, + TakeIndexer, npt, ) from pandas.compat import set_function_name @@ -1077,7 +1077,7 @@ def repeat(self, repeats: int | Sequence[int], axis: int | None = None): def take( self: ExtensionArrayT, - indices: GeneralSequenceIndexer, + indices: TakeIndexer, *, allow_fill: bool = False, fill_value: Any = None, diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index c9880b62b9ff8..4c8cb89378ecd 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -18,12 +18,12 @@ ) from pandas._typing import ( Dtype, - GeneralSequenceIndexer, NpDtype, PositionalIndexer, Scalar, ScalarIndexer, SequenceIndexer, + TakeIndexer, npt, ) from pandas.compat import ( @@ -512,7 +512,7 @@ def __setitem__(self, key: int | slice | np.ndarray, value: Any) -> None: def take( self, - indices: GeneralSequenceIndexer, + indices: TakeIndexer, allow_fill: bool = False, fill_value: Any = None, ): From d2aadd4f61a6e10d0f42394ad5899c08ac73467d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Thu, 16 Sep 2021 08:55:17 -0400 Subject: [PATCH 08/15] Update pandas/_typing.py Co-authored-by: Simon Hawkins --- pandas/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 9c9fef2e6cfaf..7d2ac6571dd8d 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -216,7 +216,7 @@ # Using List[int] here rather than Sequence[int] to disallow tuples. ScalarIndexer = Union[int, np.integer] SequenceIndexer = Union[slice, List[int], np.ndarray] -TakeIndexer = Union[Sequence[int], np.ndarray] +TakeIndexer = Union[Sequence[ScalarIndexer], npt.NDArray[np.integer]] PositionalIndexer = Union[ScalarIndexer, SequenceIndexer] PositionalIndexerTuple = Tuple[PositionalIndexer, PositionalIndexer] PositionalIndexer2D = Union[PositionalIndexer, PositionalIndexerTuple] From 3c830030567ae7d3bd34adb12def83440bfab74c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Thu, 16 Sep 2021 09:02:08 -0400 Subject: [PATCH 09/15] no mixed sequences --- pandas/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 7d2ac6571dd8d..b8bc997d578a0 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -216,7 +216,7 @@ # Using List[int] here rather than Sequence[int] to disallow tuples. ScalarIndexer = Union[int, np.integer] SequenceIndexer = Union[slice, List[int], np.ndarray] -TakeIndexer = Union[Sequence[ScalarIndexer], npt.NDArray[np.integer]] +TakeIndexer = Union[Sequence[int], Sequence[np.integer] npt.NDArray[np.integer]] PositionalIndexer = Union[ScalarIndexer, SequenceIndexer] PositionalIndexerTuple = Tuple[PositionalIndexer, PositionalIndexer] PositionalIndexer2D = Union[PositionalIndexer, PositionalIndexerTuple] From e6510179f69bf5a85b10dad062c0231d327cbc6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Thu, 16 Sep 2021 09:58:20 -0400 Subject: [PATCH 10/15] forgot a comma --- pandas/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index b8bc997d578a0..34acded283228 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -216,7 +216,7 @@ # Using List[int] here rather than Sequence[int] to disallow tuples. ScalarIndexer = Union[int, np.integer] SequenceIndexer = Union[slice, List[int], np.ndarray] -TakeIndexer = Union[Sequence[int], Sequence[np.integer] npt.NDArray[np.integer]] +TakeIndexer = Union[Sequence[int], Sequence[np.integer], npt.NDArray[np.integer]] PositionalIndexer = Union[ScalarIndexer, SequenceIndexer] PositionalIndexerTuple = Tuple[PositionalIndexer, PositionalIndexer] PositionalIndexer2D = Union[PositionalIndexer, PositionalIndexerTuple] From 1076a7ec7f7005ef76f0170c73deb9a3c05e6fca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Thu, 16 Sep 2021 10:24:44 -0400 Subject: [PATCH 11/15] guard npt --- pandas/_typing.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 34acded283228..99c16639b3003 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -216,10 +216,13 @@ # Using List[int] here rather than Sequence[int] to disallow tuples. ScalarIndexer = Union[int, np.integer] SequenceIndexer = Union[slice, List[int], np.ndarray] -TakeIndexer = Union[Sequence[int], Sequence[np.integer], npt.NDArray[np.integer]] PositionalIndexer = Union[ScalarIndexer, SequenceIndexer] PositionalIndexerTuple = Tuple[PositionalIndexer, PositionalIndexer] PositionalIndexer2D = Union[PositionalIndexer, PositionalIndexerTuple] +if npt is not None: + TakeIndexer = Union[Sequence[int], Sequence[np.integer], npt.NDArray[np.integer]] +else: + TakeIndexer = Union[Sequence[int], npt.NDArray[np.integer]] # Windowing rank methods WindowingRankType = Literal["average", "min", "max"] From d31e1a93b021bfa55520bc57a9cf14b3171c8a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Thu, 16 Sep 2021 10:27:08 -0400 Subject: [PATCH 12/15] fix --- pandas/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 99c16639b3003..0b7eccc73f70e 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -222,7 +222,7 @@ if npt is not None: TakeIndexer = Union[Sequence[int], Sequence[np.integer], npt.NDArray[np.integer]] else: - TakeIndexer = Union[Sequence[int], npt.NDArray[np.integer]] + TakeIndexer = Union[Sequence[int], Sequence[np.integer], np.ndarray] # Windowing rank methods WindowingRankType = Literal["average", "min", "max"] From f82337898ed216e59734a7f220580308ba76af57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Mon, 20 Sep 2021 12:40:55 -0400 Subject: [PATCH 13/15] change docstring --- pandas/core/algorithms.py | 2 +- pandas/core/arrays/base.py | 2 +- pandas/core/arrays/string_arrow.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index b8fb646001523..1aa1e75cd0c9a 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1446,7 +1446,7 @@ def take( arr : array-like or scalar value Non array-likes (sequences/scalars without a dtype) are coerced to an ndarray. - indices : sequence of integers + indices : sequence of int or one-dimensional np.ndarray of int Indices to be taken. axis : int, default 0 The axis over which to select values. diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 34fa65a360b01..8ee5a4a2d913a 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -1087,7 +1087,7 @@ def take( Parameters ---------- - indices : sequence of int + indices : sequence of int or one-dimensional np.ndarray of int Indices to be taken. allow_fill : bool, default False How to handle negative values in `indices`. diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index 4c8cb89378ecd..c7d08f7873c09 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -521,7 +521,7 @@ def take( Parameters ---------- - indices : sequence of int + indices : sequence of int or one-dimensional np.ndarray of int Indices to be taken. allow_fill : bool, default False How to handle negative values in `indices`. From 58ac439b27b8f56a75eddf549bf13e49ff413da3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Mon, 20 Sep 2021 16:17:32 -0400 Subject: [PATCH 14/15] use TYPE_CHECKING --- pandas/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 0b7eccc73f70e..1886698259a89 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -219,7 +219,7 @@ PositionalIndexer = Union[ScalarIndexer, SequenceIndexer] PositionalIndexerTuple = Tuple[PositionalIndexer, PositionalIndexer] PositionalIndexer2D = Union[PositionalIndexer, PositionalIndexerTuple] -if npt is not None: +if TYPE_CHECKING: TakeIndexer = Union[Sequence[int], Sequence[np.integer], npt.NDArray[np.integer]] else: TakeIndexer = Union[Sequence[int], Sequence[np.integer], np.ndarray] From c9a02c0f8b83f5fbfa283c4037dbf1b899b9958d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 22 Sep 2021 09:03:04 -0400 Subject: [PATCH 15/15] use Any for TakeIndexer when not type checking --- pandas/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 1886698259a89..5f8ea79046235 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -222,7 +222,7 @@ if TYPE_CHECKING: TakeIndexer = Union[Sequence[int], Sequence[np.integer], npt.NDArray[np.integer]] else: - TakeIndexer = Union[Sequence[int], Sequence[np.integer], np.ndarray] + TakeIndexer = Any # Windowing rank methods WindowingRankType = Literal["average", "min", "max"]