Skip to content

Commit 6fd0700

Browse files
author
tp
committed
Doc improvements for IntervalIndex and Interval
1 parent cc58b84 commit 6fd0700

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

doc/source/advanced.rst

+23
Original file line numberDiff line numberDiff line change
@@ -833,12 +833,21 @@ Of course if you need integer based selection, then use ``iloc``
833833
IntervalIndex
834834
~~~~~~~~~~~~~
835835
836+
:class:`IntervalIndex` together with its own dtype, ``interval`` as well as the
837+
:class:`Interval` scalar type, allow first-class support in pandas for interval
838+
notation.
839+
840+
The ``IntervalIndex`` allows some unique indexing and is also used as a
841+
return type for the categories in :func:`cut` and :func:`qcut`.
842+
836843
.. versionadded:: 0.20.0
837844
838845
.. warning::
839846
840847
These indexing behaviors are provisional and may change in a future version of pandas.
841848
849+
An ``IntervalIndex`` can be used in ``Series`` and in ``DataFrame`` as the index.
850+
842851
.. ipython:: python
843852
844853
df = pd.DataFrame({'A': [1, 2, 3, 4]},
@@ -860,6 +869,20 @@ If you select a lable *contained* within an interval, this will also select the
860869
df.loc[2.5]
861870
df.loc[[2.5, 3.5]]
862871
872+
``Interval`` and ``IntervalIndex`` are used by ``cut`` and ``qcut``:
873+
874+
.. ipython:: python
875+
876+
c = pd.cut(range(4), bins=2)
877+
c
878+
c.categories
879+
880+
Furthermore, ``IntervalIndex`` allows one to bin *other* data with these same
881+
bins, with ``NaN`` representing a missing value similar to other dtypes.
882+
883+
.. ipython:: python
884+
885+
pd.cut([0, 3, 5, 1], bins=c.categories)
863886
864887
Miscellaneous indexing FAQ
865888
--------------------------

pandas/_libs/interval.pyx

+27-4
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,40 @@ cdef class Interval(IntervalMixin):
5151
5252
.. versionadded:: 0.20.0
5353
54-
Attributes
54+
Parameters
5555
----------
56-
left, right : values
57-
Left and right bounds for each interval.
56+
left : value
57+
Left bound for interval.
58+
right : value
59+
Right bound for interval.
5860
closed : {'left', 'right', 'both', 'neither'}
5961
Whether the interval is closed on the left-side, right-side, both or
6062
neither. Defaults to 'right'.
6163
64+
Examples
65+
--------
66+
>>> iv = pd.Interval(left=0, right=5)
67+
>>> iv
68+
Interval(0, 5, closed='right')
69+
>>> 2.5 in iv
70+
True
71+
72+
>>> year_2017 = pd.Interval(pd.Timestamp('2017-01-01'),
73+
... pd.Timestamp('2017-12-31'), closed='both')
74+
>>> pd.Timestamp('2017-01-01 00:00') in year_2017
75+
True
76+
77+
Note
78+
----
79+
:func:`cut` and :func:`qcut` can turn an array of continuous data into a
80+
categorical consisting of ``Interval`` s.
81+
6282
See Also
6383
--------
64-
IntervalIndex : an Index of intervals that are all closed on the same side.
84+
IntervalIndex : an Index of ``interval`` s that are all closed on the same
85+
side.
86+
cut, qcut : convert arrays of continuous data into categoricals/series of
87+
``Interval``.
6588
"""
6689

6790
cdef readonly object left, right

pandas/core/indexes/interval.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ class IntervalIndex(IntervalMixin, Index):
105105
106106
.. versionadded:: 0.20.0
107107
108-
Warning: the indexing behaviors are provisional and may change in
109-
a future version of pandas.
108+
.. warning::
109+
110+
The indexing behaviors are provisional and may change in
111+
a future version of pandas.
110112
111113
Attributes
112114
----------
@@ -147,15 +149,11 @@ class IntervalIndex(IntervalMixin, Index):
147149
--------
148150
Index
149151
Interval : A bounded slice-like interval
150-
interval_range : Function to create a fixed frequency IntervalIndex
151-
IntervalIndex.from_arrays : Construct an IntervalIndex from a left and
152-
right array
153-
IntervalIndex.from_breaks : Construct an IntervalIndex from an array of
154-
splits
155-
IntervalIndex.from_intervals : Construct an IntervalIndex from an array of
156-
Interval objects
157-
IntervalIndex.from_tuples : Construct an IntervalIndex from a list/array of
158-
tuples
152+
interval_range : Function to create a fixed frequency
153+
IntervalIndex, IntervalIndex.from_arrays, IntervalIndex.from_breaks,
154+
IntervalIndex.from_intervals, IntervalIndex.from_tuples
155+
cut, qcut : convert arrays of continuous data into categoricals/series of
156+
``Interval``.
159157
"""
160158
_typ = 'intervalindex'
161159
_comparables = ['name']

0 commit comments

Comments
 (0)