Skip to content

TYP: resolve mypy ingores in core/indexing.py #47010

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 2 commits into from
May 25, 2022
Merged
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
42 changes: 18 additions & 24 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def iat(self) -> _iAtIndexer:

class _LocationIndexer(NDFrameIndexerBase):
_valid_types: str
axis = None
axis: int | None = None

@final
def __call__(self, axis=None):
Expand All @@ -652,7 +652,7 @@ def _get_setitem_indexer(self, key):
check_deprecated_indexers(x)

if self.axis is not None:
key = self._tupleize_axis_indexer(key)
key = _tupleize_axis_indexer(self.ndim, self.axis, key)

ax = self.obj._get_axis(0)

Expand Down Expand Up @@ -737,17 +737,6 @@ def _maybe_mask_setitem_value(self, indexer, value):

return indexer, value

@final
def _tupleize_axis_indexer(self, key) -> tuple:
"""
If we have an axis, adapt the given key to be axis-independent.
"""
new_key = [slice(None)] * self.ndim
# error: Invalid index type "Optional[Any]" for "List[slice]"; expected
# type "SupportsIndex"
new_key[self.axis] = key # type: ignore[index]
return tuple(new_key)

@final
def _ensure_listlike_indexer(self, key, axis=None, value=None):
"""
Expand Down Expand Up @@ -1621,7 +1610,7 @@ def _get_setitem_indexer(self, key):
key = list(key)

if self.axis is not None:
key = self._tupleize_axis_indexer(key)
key = _tupleize_axis_indexer(self.ndim, self.axis, key)

return key

Expand Down Expand Up @@ -2137,13 +2126,11 @@ def _ensure_iterable_column_indexer(self, column_indexer):
"""
Ensure that our column indexer is something that can be iterated over.
"""
ilocs: Sequence[int]
ilocs: Sequence[int] | np.ndarray
if is_integer(column_indexer):
ilocs = [column_indexer]
elif isinstance(column_indexer, slice):
ilocs = np.arange(len(self.obj.columns))[ # type: ignore[assignment]
column_indexer
]
ilocs = np.arange(len(self.obj.columns))[column_indexer]
elif isinstance(column_indexer, np.ndarray) and is_bool_dtype(
column_indexer.dtype
):
Expand Down Expand Up @@ -2201,18 +2188,16 @@ def ravel(i):
# TODO: This is hacky, align Series and DataFrame behavior GH#45778
if obj.ndim == 2 and is_empty_indexer(indexer[0]):
return ser._values.copy()
ser = ser.reindex(obj.axes[0][indexer[0]], copy=True)._values
ser_values = ser.reindex(obj.axes[0][indexer[0]], copy=True)._values

# single indexer
if len(indexer) > 1 and not multiindex_indexer:
len_indexer = len(indexer[1])
ser = (
np.tile(ser, len_indexer) # type: ignore[assignment]
.reshape(len_indexer, -1)
.T
ser_values = (
np.tile(ser_values, len_indexer).reshape(len_indexer, -1).T
)

return ser
return ser_values

for i, idx in enumerate(indexer):
ax = obj.axes[i]
Expand Down Expand Up @@ -2428,6 +2413,15 @@ def _tuplify(ndim: int, loc: Hashable) -> tuple[Hashable | slice, ...]:
return tuple(_tup)


def _tupleize_axis_indexer(ndim: int, axis: int, key) -> tuple:
"""
If we have an axis, adapt the given key to be axis-independent.
"""
new_key = [slice(None)] * ndim
new_key[axis] = key
return tuple(new_key)


def convert_to_index_sliceable(obj: DataFrame, key):
"""
If we are index sliceable, then return my slicer, otherwise return None.
Expand Down