Skip to content

Commit 10d10c6

Browse files
authored
move method from LocationIndexer to Index (#31857)
1 parent 1825fa1 commit 10d10c6

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

pandas/core/indexes/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3091,6 +3091,16 @@ def _filter_indexer_tolerance(
30913091
# --------------------------------------------------------------------
30923092
# Indexer Conversion Methods
30933093

3094+
def _get_partial_string_timestamp_match_key(self, key):
3095+
"""
3096+
Translate any partial string timestamp matches in key, returning the
3097+
new key.
3098+
3099+
Only relevant for MultiIndex.
3100+
"""
3101+
# GH#10331
3102+
return key
3103+
30943104
def _convert_scalar_indexer(self, key, kind: str_t):
30953105
"""
30963106
Convert a scalar indexer.

pandas/core/indexes/multi.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,35 @@ def _convert_listlike_indexer(self, keyarr):
23232323

23242324
return indexer, keyarr
23252325

2326+
def _get_partial_string_timestamp_match_key(self, key):
2327+
"""
2328+
Translate any partial string timestamp matches in key, returning the
2329+
new key.
2330+
2331+
Only relevant for MultiIndex.
2332+
"""
2333+
# GH#10331
2334+
if isinstance(key, str) and self.levels[0]._supports_partial_string_indexing:
2335+
# Convert key '2016-01-01' to
2336+
# ('2016-01-01'[, slice(None, None, None)]+)
2337+
key = tuple([key] + [slice(None)] * (len(self.levels) - 1))
2338+
2339+
if isinstance(key, tuple):
2340+
# Convert (..., '2016-01-01', ...) in tuple to
2341+
# (..., slice('2016-01-01', '2016-01-01', None), ...)
2342+
new_key = []
2343+
for i, component in enumerate(key):
2344+
if (
2345+
isinstance(component, str)
2346+
and self.levels[i]._supports_partial_string_indexing
2347+
):
2348+
new_key.append(slice(component, component, None))
2349+
else:
2350+
new_key.append(component)
2351+
key = tuple(new_key)
2352+
2353+
return key
2354+
23262355
@Appender(_index_shared_docs["get_indexer"] % _index_doc_kwargs)
23272356
def get_indexer(self, target, method=None, limit=None, tolerance=None):
23282357
method = missing.clean_reindex_fill_method(method)

pandas/core/indexing.py

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -966,38 +966,6 @@ def _multi_take(self, tup: Tuple):
966966

967967
# -------------------------------------------------------------------
968968

969-
def _get_partial_string_timestamp_match_key(self, key, labels):
970-
"""
971-
Translate any partial string timestamp matches in key, returning the
972-
new key.
973-
974-
(GH 10331)
975-
"""
976-
if isinstance(labels, ABCMultiIndex):
977-
if (
978-
isinstance(key, str)
979-
and labels.levels[0]._supports_partial_string_indexing
980-
):
981-
# Convert key '2016-01-01' to
982-
# ('2016-01-01'[, slice(None, None, None)]+)
983-
key = tuple([key] + [slice(None)] * (len(labels.levels) - 1))
984-
985-
if isinstance(key, tuple):
986-
# Convert (..., '2016-01-01', ...) in tuple to
987-
# (..., slice('2016-01-01', '2016-01-01', None), ...)
988-
new_key = []
989-
for i, component in enumerate(key):
990-
if (
991-
isinstance(component, str)
992-
and labels.levels[i]._supports_partial_string_indexing
993-
):
994-
new_key.append(slice(component, component, None))
995-
else:
996-
new_key.append(component)
997-
key = tuple(new_key)
998-
999-
return key
1000-
1001969
def _getitem_iterable(self, key, axis: int):
1002970
"""
1003971
Index current object with an an iterable collection of keys.
@@ -1079,7 +1047,7 @@ def _getitem_axis(self, key, axis: int):
10791047
key = list(key)
10801048

10811049
labels = self.obj._get_axis(axis)
1082-
key = self._get_partial_string_timestamp_match_key(key, labels)
1050+
key = labels._get_partial_string_timestamp_match_key(key)
10831051

10841052
if isinstance(key, slice):
10851053
self._validate_key(key, axis)

0 commit comments

Comments
 (0)