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

Method get_slice_bound

pandas/core/indexes/base.py:6912–6998  ·  view source on GitHub ↗

Calculate slice bound that corresponds to given label. Returns leftmost (one-past-the-rightmost if ``side=='right'``) position of given label. Parameters ---------- label : object The label for which to calculate the slice bound.

(self, label, side: Literal["left", "right"])

Source from the content-addressed store, hash-verified

6910 raise ValueError("index must be monotonic increasing or decreasing")
6911
6912 def get_slice_bound(self, label, side: Literal["left", "right"]) -> int:
6913 """
6914 Calculate slice bound that corresponds to given label.
6915
6916 Returns leftmost (one-past-the-rightmost if ``side=='right'``) position
6917 of given label.
6918
6919 Parameters
6920 ----------
6921 label : object
6922 The label for which to calculate the slice bound.
6923 side : {'left', 'right'}
6924 if 'left' return leftmost position of given label.
6925 if 'right' return one-past-the-rightmost position of given label.
6926
6927 Returns
6928 -------
6929 int
6930 Index of label.
6931
6932 See Also
6933 --------
6934 Index.get_loc : Get integer location, slice or boolean mask for requested
6935 label.
6936
6937 Examples
6938 --------
6939 >>> idx = pd.RangeIndex(5)
6940 >>> idx.get_slice_bound(3, "left")
6941 3
6942
6943 >>> idx.get_slice_bound(3, "right")
6944 4
6945
6946 If ``label`` is non-unique in the index, an error will be raised.
6947
6948 >>> idx_duplicate = pd.Index(["a", "b", "a", "c", "d"])
6949 >>> idx_duplicate.get_slice_bound("a", "left")
6950 Traceback (most recent call last):
6951 KeyError: Cannot get left slice bound for non-unique label: 'a'
6952 """
6953
6954 if side not in ("left", "right"):
6955 raise ValueError(
6956 "Invalid value for side kwarg, must be either "
6957 f"'left' or 'right': {side}"
6958 )
6959
6960 original_label = label
6961
6962 # For datetime indices label may be a string that has to be converted
6963 # to datetime boundary according to its resolution.
6964 label = self._maybe_cast_slice_bound(label, side)
6965
6966 # we need to look up the label
6967 try:
6968 slc = self.get_loc(label)
6969 except KeyError:

Calls 5

get_locMethod · 0.95
is_bool_dtypeFunction · 0.90
viewMethod · 0.45