Skip to content

REF: _convert_to_index_sliceable as DataFrame method #33488

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

Closed
wants to merge 4 commits into from
Closed
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
30 changes: 27 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
from pandas.core.indexes.datetimes import DatetimeIndex
from pandas.core.indexes.multi import MultiIndex, maybe_droplevels
from pandas.core.indexes.period import PeriodIndex
from pandas.core.indexing import check_bool_indexer, convert_to_index_sliceable
from pandas.core.indexing import check_bool_indexer
from pandas.core.internals import BlockManager
from pandas.core.internals.construction import (
arrays_to_mgr,
Expand Down Expand Up @@ -2620,7 +2620,7 @@ def __getitem__(self, key):
return self._get_item_cache(key)

# Do we have a slicer (on rows)?
indexer = convert_to_index_sliceable(self, key)
indexer = self._convert_to_index_sliceable(key)
if indexer is not None:
# either we have a slice or we have a string that can be converted
# to a slice for partial-string date indexing
Expand Down Expand Up @@ -2762,11 +2762,35 @@ def _get_value(self, index, col, takeable: bool = False):
index = self.index.get_loc(index)
return self._get_value(index, col, takeable=True)

def _convert_to_index_sliceable(self, key):
"""
If we are index sliceable, then return my slicer, otherwise return None.
"""
idx = self.index
if isinstance(key, slice):
return idx._convert_slice_indexer(key, kind="getitem")

elif isinstance(key, str):

# we are an actual column
if key in self.columns:
return None

# We might have a datetimelike string that we can translate to a
# slice here via partial string indexing
if idx._supports_partial_string_indexing:
try:
return idx._get_string_slice(key)
except (KeyError, ValueError, NotImplementedError):
return None

return None

def __setitem__(self, key, value):
key = com.apply_if_callable(key, self)

# see if we can slice the rows
indexer = convert_to_index_sliceable(self, key)
indexer = self._convert_to_index_sliceable(key)
if indexer is not None:
# either we have a slice or we have a string that can be converted
# to a slice for partial-string date indexing
Expand Down
25 changes: 0 additions & 25 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2109,31 +2109,6 @@ def _tuplify(ndim: int, loc: Hashable) -> Tuple[Union[Hashable, slice], ...]:
return tuple(_tup)


def convert_to_index_sliceable(obj: "DataFrame", key):
"""
If we are index sliceable, then return my slicer, otherwise return None.
"""
idx = obj.index
if isinstance(key, slice):
return idx._convert_slice_indexer(key, kind="getitem")

elif isinstance(key, str):

# we are an actual column
if key in obj.columns:
return None

# We might have a datetimelike string that we can translate to a
# slice here via partial string indexing
if idx._supports_partial_string_indexing:
try:
return idx._get_string_slice(key)
except (KeyError, ValueError, NotImplementedError):
return None

return None


def check_bool_indexer(index: Index, key) -> np.ndarray:
"""
Check if key is a valid boolean indexer for an object with such index and
Expand Down