MCPcopy
hub / github.com/pandas-dev/pandas / _convert_slice_indexer

Method _convert_slice_indexer

pandas/core/indexes/base.py:4044–4107  ·  view source on GitHub ↗

Convert a slice indexer. By definition, these are labels unless 'iloc' is passed in. Floats are not allowed as the start, step, or stop of the slice. Parameters ---------- key : label of the slice bound kind : {'loc', 'getitem'}

(self, key: slice, kind: Literal["loc", "getitem"])

Source from the content-addressed store, hash-verified

4042 self._validate_indexer("positional", key.step, "iloc")
4043
4044 def _convert_slice_indexer(self, key: slice, kind: Literal["loc", "getitem"]):
4045 """
4046 Convert a slice indexer.
4047
4048 By definition, these are labels unless 'iloc' is passed in.
4049 Floats are not allowed as the start, step, or stop of the slice.
4050
4051 Parameters
4052 ----------
4053 key : label of the slice bound
4054 kind : {'loc', 'getitem'}
4055 """
4056
4057 # potentially cast the bounds to integers
4058 start, stop, step = key.start, key.stop, key.step
4059
4060 # figure out if this is a positional indexer
4061 is_index_slice = is_valid_positional_slice(key)
4062
4063 # TODO(GH#50617): once Series.__[gs]etitem__ is removed we should be able
4064 # to simplify this.
4065 if kind == "getitem":
4066 # called from the getitem slicers, validate that we are in fact integers
4067 if is_index_slice:
4068 # In this case the _validate_indexer checks below are redundant
4069 return key
4070 elif self.dtype.kind in "iu":
4071 # Note: these checks are redundant if we know is_index_slice
4072 self._validate_indexer("slice", key.start, "getitem")
4073 self._validate_indexer("slice", key.stop, "getitem")
4074 self._validate_indexer("slice", key.step, "getitem")
4075 return key
4076
4077 # convert the slice to an indexer here; checking that the user didn't
4078 # pass a positional slice to loc
4079 is_positional = is_index_slice and self._should_fallback_to_positional
4080
4081 # if we are mixed and have integers
4082 if is_positional:
4083 try:
4084 # Validate start & stop
4085 if start is not None:
4086 self.get_loc(start)
4087 if stop is not None:
4088 self.get_loc(stop)
4089 is_positional = False
4090 except KeyError:
4091 pass
4092
4093 if com.is_null_slice(key):
4094 # It doesn't matter if we are positional or label based
4095 indexer = key
4096 elif is_positional:
4097 if kind == "loc":
4098 # GH#16121, GH#24612, GH#31810
4099 raise TypeError(
4100 "Slicing a positional slice with .loc is not allowed, "
4101 "Use .loc with labels or .iloc with positions instead.",

Callers 5

__setitem__Method · 0.45
__setitem__Method · 0.45
_convert_to_indexerMethod · 0.45
_getitem_sliceMethod · 0.45

Calls 4

_validate_indexerMethod · 0.95
get_locMethod · 0.95
slice_indexerMethod · 0.95

Tested by 1