Skip to content

TYP: implement typing.Positional #40794

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

Merged
merged 4 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,14 @@
# internals
Manager = Union["ArrayManager", "BlockManager"]
SingleManager = Union["SingleArrayManager", "SingleBlockManager"]

# indexing
# PositionalIndexer -> valid 1D positional indexer, e.g. can pass
# to ndarray.__getitem__
# TODO: add Ellipsis, see
# https://github.com/python/typing/issues/684#issuecomment-548203158
# https://bugs.python.org/issue41810
PositionalIndexer = Union[int, np.integer, slice, Sequence[int], np.ndarray]
PositionalIndexer2D = Union[
PositionalIndexer, Tuple[PositionalIndexer, PositionalIndexer]
]
4 changes: 3 additions & 1 deletion pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pandas._libs import lib
from pandas._typing import (
F,
PositionalIndexer2D,
Shape,
)
from pandas.compat.numpy import function as nv
Expand Down Expand Up @@ -274,7 +275,8 @@ def _validate_setitem_value(self, value):
return value

def __getitem__(
self: NDArrayBackedExtensionArrayT, key: int | slice | np.ndarray
self: NDArrayBackedExtensionArrayT,
key: PositionalIndexer2D,
) -> NDArrayBackedExtensionArrayT | Any:
if lib.is_integer(key):
# fast-path
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from pandas._typing import (
ArrayLike,
Dtype,
PositionalIndexer,
Shape,
)
from pandas.compat import set_function_name
Expand Down Expand Up @@ -288,7 +289,7 @@ def _from_factorized(cls, values, original):
# Must be a Sequence
# ------------------------------------------------------------------------

def __getitem__(self, item: int | slice | np.ndarray) -> ExtensionArray | Any:
def __getitem__(self, item: PositionalIndexer) -> ExtensionArray | Any:
"""
Select a subset of self.
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
Dtype,
DtypeObj,
NpDtype,
PositionalIndexer2D,
)
from pandas.compat.numpy import function as nv
from pandas.errors import (
Expand Down Expand Up @@ -338,7 +339,7 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
return self._ndarray

def __getitem__(
self, key: int | slice | np.ndarray
self, key: PositionalIndexer2D
) -> DatetimeLikeArrayMixin | DTScalarOrNaT:
"""
This getitem defers to the underlying array, which by-definition can
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ArrayLike,
Dtype,
NpDtype,
PositionalIndexer,
Scalar,
type_t,
)
Expand Down Expand Up @@ -134,7 +135,7 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
def dtype(self) -> BaseMaskedDtype:
raise AbstractMethodError(self)

def __getitem__(self, item: int | slice | np.ndarray) -> BaseMaskedArray | Any:
def __getitem__(self, item: PositionalIndexer) -> BaseMaskedArray | Any:
if is_integer(item):
if self._mask[item]:
return self.dtype.na_value
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pandas._typing import (
Dtype,
NpDtype,
PositionalIndexer,
type_t,
)
from pandas.util._decorators import doc
Expand Down Expand Up @@ -310,7 +311,7 @@ def _concat_same_type(cls, to_concat) -> ArrowStringArray:
)
)

def __getitem__(self, item: Any) -> Any:
def __getitem__(self, item: PositionalIndexer) -> Any:
"""Select a subset of self.

Parameters
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,9 +1381,7 @@ def iget(self, col):
elif isinstance(col, slice):
if col != slice(None):
raise NotImplementedError(col)
# error: Invalid index type "List[Any]" for "ExtensionArray"; expected
# type "Union[int, slice, ndarray]"
return self.values[[loc]] # type: ignore[index]
return self.values[[loc]]
return self.values[loc]
else:
if col != 0:
Expand Down