Skip to content

Commit 33222bd

Browse files
jbrockmendelJulianWgs
authored andcommitted
TYP: implement typing.Positional (pandas-dev#40794)
1 parent c91a808 commit 33222bd

File tree

7 files changed

+23
-8
lines changed

7 files changed

+23
-8
lines changed

pandas/_typing.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,14 @@
187187
# internals
188188
Manager = Union["ArrayManager", "BlockManager"]
189189
SingleManager = Union["SingleArrayManager", "SingleBlockManager"]
190+
191+
# indexing
192+
# PositionalIndexer -> valid 1D positional indexer, e.g. can pass
193+
# to ndarray.__getitem__
194+
# TODO: add Ellipsis, see
195+
# https://github.com/python/typing/issues/684#issuecomment-548203158
196+
# https://bugs.python.org/issue41810
197+
PositionalIndexer = Union[int, np.integer, slice, Sequence[int], np.ndarray]
198+
PositionalIndexer2D = Union[
199+
PositionalIndexer, Tuple[PositionalIndexer, PositionalIndexer]
200+
]

pandas/core/arrays/_mixins.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pandas._libs import lib
1414
from pandas._typing import (
1515
F,
16+
PositionalIndexer2D,
1617
Shape,
1718
)
1819
from pandas.compat.numpy import function as nv
@@ -274,7 +275,8 @@ def _validate_setitem_value(self, value):
274275
return value
275276

276277
def __getitem__(
277-
self: NDArrayBackedExtensionArrayT, key: int | slice | np.ndarray
278+
self: NDArrayBackedExtensionArrayT,
279+
key: PositionalIndexer2D,
278280
) -> NDArrayBackedExtensionArrayT | Any:
279281
if lib.is_integer(key):
280282
# fast-path

pandas/core/arrays/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from pandas._typing import (
2525
ArrayLike,
2626
Dtype,
27+
PositionalIndexer,
2728
Shape,
2829
)
2930
from pandas.compat import set_function_name
@@ -288,7 +289,7 @@ def _from_factorized(cls, values, original):
288289
# Must be a Sequence
289290
# ------------------------------------------------------------------------
290291

291-
def __getitem__(self, item: int | slice | np.ndarray) -> ExtensionArray | Any:
292+
def __getitem__(self, item: PositionalIndexer) -> ExtensionArray | Any:
292293
"""
293294
Select a subset of self.
294295

pandas/core/arrays/datetimelike.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
Dtype,
4848
DtypeObj,
4949
NpDtype,
50+
PositionalIndexer2D,
5051
)
5152
from pandas.compat.numpy import function as nv
5253
from pandas.errors import (
@@ -338,7 +339,7 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
338339
return self._ndarray
339340

340341
def __getitem__(
341-
self, key: int | slice | np.ndarray
342+
self, key: PositionalIndexer2D
342343
) -> DatetimeLikeArrayMixin | DTScalarOrNaT:
343344
"""
344345
This getitem defers to the underlying array, which by-definition can

pandas/core/arrays/masked.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ArrayLike,
1818
Dtype,
1919
NpDtype,
20+
PositionalIndexer,
2021
Scalar,
2122
type_t,
2223
)
@@ -134,7 +135,7 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
134135
def dtype(self) -> BaseMaskedDtype:
135136
raise AbstractMethodError(self)
136137

137-
def __getitem__(self, item: int | slice | np.ndarray) -> BaseMaskedArray | Any:
138+
def __getitem__(self, item: PositionalIndexer) -> BaseMaskedArray | Any:
138139
if is_integer(item):
139140
if self._mask[item]:
140141
return self.dtype.na_value

pandas/core/arrays/string_arrow.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pandas._typing import (
1818
Dtype,
1919
NpDtype,
20+
PositionalIndexer,
2021
type_t,
2122
)
2223
from pandas.util._decorators import doc
@@ -310,7 +311,7 @@ def _concat_same_type(cls, to_concat) -> ArrowStringArray:
310311
)
311312
)
312313

313-
def __getitem__(self, item: Any) -> Any:
314+
def __getitem__(self, item: PositionalIndexer) -> Any:
314315
"""Select a subset of self.
315316
316317
Parameters

pandas/core/internals/blocks.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,9 +1407,7 @@ def iget(self, col):
14071407
elif isinstance(col, slice):
14081408
if col != slice(None):
14091409
raise NotImplementedError(col)
1410-
# error: Invalid index type "List[Any]" for "ExtensionArray"; expected
1411-
# type "Union[int, slice, ndarray]"
1412-
return self.values[[loc]] # type: ignore[index]
1410+
return self.values[[loc]]
14131411
return self.values[loc]
14141412
else:
14151413
if col != 0:

0 commit comments

Comments
 (0)