Skip to content

REF: move Loc method that belongs on Index #31857

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
Feb 22, 2020
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
10 changes: 10 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3101,6 +3101,16 @@ def _filter_indexer_tolerance(
# --------------------------------------------------------------------
# Indexer Conversion Methods

def _get_partial_string_timestamp_match_key(self, key):
"""
Translate any partial string timestamp matches in key, returning the
new key.

Only relevant for MultiIndex.
"""
# GH#10331
return key

def _convert_scalar_indexer(self, key, kind: str_t):
"""
Convert a scalar indexer.
Expand Down
29 changes: 29 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,35 @@ def _convert_listlike_indexer(self, keyarr):

return indexer, keyarr

def _get_partial_string_timestamp_match_key(self, key):
"""
Translate any partial string timestamp matches in key, returning the
new key.

Only relevant for MultiIndex.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can remove this comment at some point

"""
# GH#10331
if isinstance(key, str) and self.levels[0]._supports_partial_string_indexing:
# Convert key '2016-01-01' to
# ('2016-01-01'[, slice(None, None, None)]+)
key = tuple([key] + [slice(None)] * (len(self.levels) - 1))

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 self.levels[i]._supports_partial_string_indexing
):
new_key.append(slice(component, component, None))
else:
new_key.append(component)
key = tuple(new_key)

return key

@Appender(_index_shared_docs["get_indexer"] % _index_doc_kwargs)
def get_indexer(self, target, method=None, limit=None, tolerance=None):
method = missing.clean_reindex_fill_method(method)
Expand Down
34 changes: 1 addition & 33 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,38 +980,6 @@ def _multi_take(self, tup: Tuple):

# -------------------------------------------------------------------

def _get_partial_string_timestamp_match_key(self, key, labels):
"""
Translate any partial string timestamp matches in key, returning the
new key.

(GH 10331)
"""
if isinstance(labels, ABCMultiIndex):
if (
isinstance(key, str)
and labels.levels[0]._supports_partial_string_indexing
):
# Convert key '2016-01-01' to
# ('2016-01-01'[, slice(None, None, None)]+)
key = tuple([key] + [slice(None)] * (len(labels.levels) - 1))

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]._supports_partial_string_indexing
):
new_key.append(slice(component, component, None))
else:
new_key.append(component)
key = tuple(new_key)

return key

def _getitem_iterable(self, key, axis: int):
"""
Index current object with an an iterable collection of keys.
Expand Down Expand Up @@ -1072,7 +1040,7 @@ def _getitem_axis(self, key, axis: int):
key = list(key)

labels = self.obj._get_axis(axis)
key = self._get_partial_string_timestamp_match_key(key, labels)
key = labels._get_partial_string_timestamp_match_key(key)

if isinstance(key, slice):
self._validate_key(key, axis)
Expand Down