-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Avoid accessing private loc methods #27383
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
Changes from 9 commits
9ddb071
ba70015
21fb397
f6cfe85
0de71aa
aa3b6c3
deb6193
944fb36
0f44a3c
525a02d
99ca950
f9c7fc9
dbbae14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import textwrap | ||
from typing import Any | ||
import warnings | ||
|
||
import numpy as np | ||
|
@@ -222,7 +223,7 @@ def _validate_key(self, key, axis: int): | |
""" | ||
raise AbstractMethodError(self) | ||
|
||
def _has_valid_tuple(self, key): | ||
def _has_valid_tuple(self, key: tuple): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use the generic |
||
""" check the key for valid keys across my indexer """ | ||
for i, k in enumerate(key): | ||
if i >= self.obj.ndim: | ||
|
@@ -235,7 +236,7 @@ def _has_valid_tuple(self, key): | |
"[{types}] types".format(types=self._valid_types) | ||
) | ||
|
||
def _is_nested_tuple_indexer(self, tup): | ||
def _is_nested_tuple_indexer(self, tup: tuple): | ||
if any(isinstance(ax, MultiIndex) for ax in self.obj.axes): | ||
return any(is_nested_tuple(tup, ax) for ax in self.obj.axes) | ||
return False | ||
|
@@ -259,7 +260,7 @@ def _convert_tuple(self, key, is_setter: bool = False): | |
keyidx.append(idx) | ||
return tuple(keyidx) | ||
|
||
def _convert_range(self, key, is_setter: bool = False): | ||
def _convert_range(self, key: range, is_setter: bool = False): | ||
""" convert a range argument """ | ||
return list(key) | ||
|
||
|
@@ -269,7 +270,7 @@ def _convert_scalar_indexer(self, key, axis: int): | |
# a scalar | ||
return ax._convert_scalar_indexer(key, kind=self.name) | ||
|
||
def _convert_slice_indexer(self, key, axis: int): | ||
def _convert_slice_indexer(self, key: slice, axis: int): | ||
# if we are accessing via lowered dim, use the last dim | ||
ax = self.obj._get_axis(min(axis, self.ndim - 1)) | ||
return ax._convert_slice_indexer(key, kind=self.name) | ||
|
@@ -483,7 +484,7 @@ def setter(item, v): | |
if is_list_like_indexer(value) and getattr(value, "ndim", 1) > 0: | ||
|
||
# we have an equal len Frame | ||
if isinstance(value, ABCDataFrame) and value.ndim > 1: | ||
if isinstance(value, ABCDataFrame): | ||
sub_indexer = list(indexer) | ||
multiindex_indexer = isinstance(labels, MultiIndex) | ||
|
||
|
@@ -637,27 +638,23 @@ def _setitem_with_indexer_missing(self, indexer, value): | |
self.obj._maybe_update_cacher(clear=True) | ||
return self.obj | ||
|
||
def _align_series(self, indexer, ser, multiindex_indexer=False): | ||
def _align_series(self, indexer, ser: ABCSeries, multiindex_indexer: bool = False): | ||
""" | ||
Parameters | ||
---------- | ||
indexer : tuple, slice, scalar | ||
The indexer used to get the locations that will be set to | ||
`ser` | ||
|
||
ser : pd.Series | ||
The values to assign to the locations specified by `indexer` | ||
|
||
multiindex_indexer : boolean, optional | ||
Defaults to False. Should be set to True if `indexer` was from | ||
a `pd.MultiIndex`, to avoid unnecessary broadcasting. | ||
|
||
|
||
Returns | ||
------- | ||
`np.array` of `ser` broadcast to the appropriate shape for assignment | ||
to the locations selected by `indexer` | ||
|
||
""" | ||
if isinstance(indexer, (slice, np.ndarray, list, Index)): | ||
indexer = tuple([indexer]) | ||
|
@@ -733,7 +730,7 @@ def ravel(i): | |
|
||
raise ValueError("Incompatible indexer with Series") | ||
|
||
def _align_frame(self, indexer, df): | ||
def _align_frame(self, indexer, df: ABCDataFrame): | ||
is_frame = self.obj.ndim == 2 | ||
|
||
if isinstance(indexer, tuple): | ||
|
@@ -785,7 +782,7 @@ def _align_frame(self, indexer, df): | |
|
||
raise ValueError("Incompatible indexer with DataFrame") | ||
|
||
def _getitem_tuple(self, tup): | ||
def _getitem_tuple(self, tup: tuple): | ||
try: | ||
return self._getitem_lowerdim(tup) | ||
except IndexingError: | ||
|
@@ -808,7 +805,7 @@ def _getitem_tuple(self, tup): | |
|
||
return retval | ||
|
||
def _multi_take_opportunity(self, tup): | ||
def _multi_take_opportunity(self, tup: tuple): | ||
""" | ||
Check whether there is the possibility to use ``_multi_take``. | ||
Currently the limit is that all axes being indexed must be indexed with | ||
|
@@ -832,7 +829,7 @@ def _multi_take_opportunity(self, tup): | |
|
||
return True | ||
|
||
def _multi_take(self, tup): | ||
def _multi_take(self, tup: tuple): | ||
""" | ||
Create the indexers for the passed tuple of keys, and execute the take | ||
operation. This allows the take operation to be executed all at once - | ||
|
@@ -858,7 +855,7 @@ def _multi_take(self, tup): | |
def _convert_for_reindex(self, key, axis: int): | ||
return key | ||
|
||
def _handle_lowerdim_multi_index_axis0(self, tup): | ||
def _handle_lowerdim_multi_index_axis0(self, tup: tuple): | ||
# we have an axis0 multi-index, handle or raise | ||
axis = self.axis or 0 | ||
try: | ||
|
@@ -883,7 +880,7 @@ def _handle_lowerdim_multi_index_axis0(self, tup): | |
|
||
return None | ||
|
||
def _getitem_lowerdim(self, tup): | ||
def _getitem_lowerdim(self, tup: tuple): | ||
|
||
# we can directly get the axis result since the axis is specified | ||
if self.axis is not None: | ||
|
@@ -947,7 +944,7 @@ def _getitem_lowerdim(self, tup): | |
|
||
raise IndexingError("not applicable") | ||
|
||
def _getitem_nested_tuple(self, tup): | ||
def _getitem_nested_tuple(self, tup: tuple): | ||
# we have a nested tuple so have at least 1 multi-index level | ||
# we should be able to match up the dimensionality here | ||
|
||
|
@@ -1421,7 +1418,7 @@ def _getbool_axis(self, key, axis: int): | |
# caller is responsible for ensuring non-None axis | ||
labels = self.obj._get_axis(axis) | ||
key = check_bool_indexer(labels, key) | ||
inds, = key.nonzero() | ||
inds = key.nonzero()[0] | ||
try: | ||
return self.obj.take(inds, axis=axis) | ||
except Exception as detail: | ||
|
@@ -1739,22 +1736,26 @@ def _getitem_scalar(self, key): | |
values = self.obj._get_value(*key) | ||
return values | ||
|
||
def _get_partial_string_timestamp_match_key(self, key, labels): | ||
def _get_partial_string_timestamp_match_key(self, key, labels: Index): | ||
"""Translate any partial string timestamp matches in key, returning the | ||
new key (GH 10331)""" | ||
if isinstance(labels, MultiIndex): | ||
if isinstance(key, str) and labels.levels[0].is_all_dates: | ||
# Convert key '2016-01-01' to | ||
# ('2016-01-01'[, slice(None, None, None)]+) | ||
key = tuple([key] + [slice(None)] * (len(labels.levels) - 1)) | ||
key = tuple( | ||
[key] + [slice(None)] * (len(labels.levels) - 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The more I think about this a better idiom would be something like: new_key = None # type: List[str, slice]
if isinstance(labels, MultiIndex):
...
new_key = tuple([key] + [slice(None)] * len(labels.levels) - 1)
... Should appease mypy and prevent the shadowing of the |
||
) # type: ignore | ||
|
||
if isinstance(key, tuple): | ||
# Convert (..., '2016-01-01', ...) in tuple to | ||
# (..., slice('2016-01-01', '2016-01-01', None), ...) | ||
new_key = [] | ||
for i, component in enumerate(key): | ||
if isinstance(component, str) and labels.levels[i].is_all_dates: | ||
new_key.append(slice(component, component, None)) | ||
new_key.append( | ||
slice(component, component, None) | ||
) # type: ignore | ||
else: | ||
new_key.append(component) | ||
key = tuple(new_key) | ||
|
@@ -2042,7 +2043,7 @@ def _getitem_scalar(self, key): | |
values = self.obj._get_value(*key, takeable=True) | ||
return values | ||
|
||
def _validate_integer(self, key, axis): | ||
def _validate_integer(self, key: int, axis: int): | ||
""" | ||
Check that 'key' is a valid position in the desired axis. | ||
|
||
|
@@ -2067,7 +2068,7 @@ def _validate_integer(self, key, axis): | |
if key >= len_axis or key < -len_axis: | ||
raise IndexError("single positional indexer is out-of-bounds") | ||
|
||
def _getitem_tuple(self, tup): | ||
def _getitem_tuple(self, tup: tuple): | ||
|
||
self._has_valid_tuple(tup) | ||
try: | ||
|
@@ -2167,7 +2168,7 @@ class _ScalarAccessIndexer(_NDFrameIndexer): | |
""" access scalars quickly """ | ||
|
||
def _convert_key(self, key, is_setter: bool = False): | ||
return list(key) | ||
raise AbstractMethodError(self) | ||
|
||
def __getitem__(self, key): | ||
if not isinstance(key, tuple): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.columns.get_indexer(key)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this returns something different, will have to keep digging