Skip to content

Commit 98ca9f0

Browse files
authored
REF: write indexing checks in terms of should_fallback_to_positional (#47036)
1 parent 9a00692 commit 98ca9f0

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

pandas/core/indexes/base.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -4205,9 +4205,14 @@ def is_int(v):
42054205
return v is None or is_integer(v)
42064206

42074207
is_index_slice = is_int(start) and is_int(stop) and is_int(step)
4208-
is_positional = is_index_slice and not (
4209-
self.is_integer() or self.is_categorical()
4208+
4209+
# special case for interval_dtype bc we do not do partial-indexing
4210+
# on integer Intervals when slicing
4211+
# TODO: write this in terms of e.g. should_partial_index?
4212+
ints_are_positional = self._should_fallback_to_positional or is_interval_dtype(
4213+
self.dtype
42104214
)
4215+
is_positional = is_index_slice and ints_are_positional
42114216

42124217
if kind == "getitem":
42134218
"""

pandas/core/indexes/numeric.py

+2
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ def _convert_slice_indexer(self, key: slice, kind: str, is_frame: bool = False):
222222
if is_float_dtype(self.dtype):
223223
assert kind in ["loc", "getitem"]
224224

225+
# TODO: can we write this as a condition based on
226+
# e.g. _should_fallback_to_positional?
225227
# We always treat __getitem__ slicing as label-based
226228
# translate to locations
227229
return self.slice_indexer(key.start, key.stop, key.step)

pandas/core/indexing.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -1322,10 +1322,6 @@ def _convert_to_indexer(self, key, axis: int):
13221322
if isinstance(key, slice):
13231323
return labels._convert_slice_indexer(key, kind="loc")
13241324

1325-
# see if we are positional in nature
1326-
is_int_index = labels.is_integer()
1327-
is_int_positional = is_integer(key) and not is_int_index
1328-
13291325
if (
13301326
isinstance(key, tuple)
13311327
and not isinstance(labels, MultiIndex)
@@ -1350,17 +1346,9 @@ def _convert_to_indexer(self, key, axis: int):
13501346
if not isinstance(labels, MultiIndex):
13511347
raise
13521348
except ValueError:
1353-
if not is_int_positional:
1349+
if not is_integer(key):
13541350
raise
1355-
1356-
# a positional
1357-
if is_int_positional:
1358-
1359-
# if we are setting and its not a valid location
1360-
# its an insert which fails by definition
1361-
1362-
# always valid
1363-
return {"key": key}
1351+
return {"key": key}
13641352

13651353
if is_nested_tuple(key, labels):
13661354
if self.ndim == 1 and any(isinstance(k, tuple) for k in key):

0 commit comments

Comments
 (0)