From 46c9bc272bd7354ba9614730ff1625d44a38bafe Mon Sep 17 00:00:00 2001 From: tp Date: Mon, 9 Oct 2017 23:01:35 +0100 Subject: [PATCH 1/2] .slice_indexer correction + examples --- doc/source/advanced.rst | 6 +++--- pandas/core/indexes/base.py | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/doc/source/advanced.rst b/doc/source/advanced.rst index 44358593793bc..db1780e88baef 100644 --- a/doc/source/advanced.rst +++ b/doc/source/advanced.rst @@ -833,6 +833,8 @@ Of course if you need integer based selection, then use ``iloc`` IntervalIndex ~~~~~~~~~~~~~ +.. versionadded:: 0.20.0 + :class:`IntervalIndex` together with its own dtype, ``interval`` as well as the :class:`Interval` scalar type, allow first-class support in pandas for interval notation. @@ -840,8 +842,6 @@ notation. The ``IntervalIndex`` allows some unique indexing and is also used as a return type for the categories in :func:`cut` and :func:`qcut`. -.. versionadded:: 0.20.0 - .. warning:: These indexing behaviors are provisional and may change in a future version of pandas. @@ -862,7 +862,7 @@ selecting that particular interval. df.loc[2] df.loc[[2, 3]] -If you select a lable *contained* within an interval, this will also select the interval. +If you select a label *contained* within an interval, this will also select the interval. .. ipython:: python diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index a995fc10a6674..d23e6b52f5763 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3430,7 +3430,7 @@ def _get_string_slice(self, key, use_lhs=True, use_rhs=True): def slice_indexer(self, start=None, end=None, step=None, kind=None): """ - For an ordered Index, compute the slice indexer for input labels and + For an ordered index, compute the slice indexer for input labels and step Parameters @@ -3444,11 +3444,23 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None): Returns ------- - indexer : ndarray or slice + indexer : slice Notes ----- This function assumes that the data is sorted, so use at your own peril + + Examples + --------- + This is a method on all index types. For example you can do: + + >>> idx = pd.Index(list('abcd')) + >>> idx.slice_indexer(start='b', end='c') + slice(1, 3) + + >>> idx = pd.MultiIndex.from_arrays([list('abcd'), list('efgh')]) + >>> idx.slice_indexer(start='b', end=('c', 'g')) + slice(1, 3) """ start_slice, end_slice = self.slice_locs(start, end, step=step, kind=kind) From e78f3cd9e8acc64f37e38b7ec9eb375cf1aef4ae Mon Sep 17 00:00:00 2001 From: tp Date: Sun, 29 Oct 2017 22:49:19 +0000 Subject: [PATCH 2/2] index may be unique + added notw on keyError --- pandas/core/indexes/base.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index d23e6b52f5763..7b59a9fd33f6d 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3430,8 +3430,8 @@ def _get_string_slice(self, key, use_lhs=True, use_rhs=True): def slice_indexer(self, start=None, end=None, step=None, kind=None): """ - For an ordered index, compute the slice indexer for input labels and - step + For an ordered or unique index, compute the slice indexer for input + labels and step. Parameters ---------- @@ -3446,6 +3446,11 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None): ------- indexer : slice + Raises + ------ + KeyError : If key does not exist, or key is not unique and index is + not ordered. + Notes ----- This function assumes that the data is sorted, so use at your own peril