Skip to content

DOC: tweak paragraph regarding cut and IntervalIndex #27132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from Jun 30, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions doc/source/user_guide/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -965,21 +965,28 @@ If you select a label *contained* within an interval, this will also select the
df.loc[2.5]
df.loc[[2.5, 3.5]]

``Interval`` and ``IntervalIndex`` are used by ``cut`` and ``qcut``:
:func:`cut` and :func:`qcut` both return a ``Categorical`` object, and the bins they
create are stored as an ``IntervalIndex`` in its `.categories` attribute.

.. ipython:: python

c = pd.cut(range(4), bins=2)
c
c.categories

Furthermore, ``IntervalIndex`` allows one to bin *other* data with these same
bins, with ``NaN`` representing a missing value similar to other dtypes.
:func:`cut` also accepts an ``IntervalIndex`` for its `bins` argument, which enables
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double backticks for anything that is not a role here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

a useful pandas idiom. We call :func:`cut` once on some data with `bins` set to a
fixed number, to generate the bins. We can then pass `.categories` as the `bins` argument
in subsequent calls to :func:`cut`, to have the new data binned into the same bins.

.. ipython:: python

pd.cut([0, 3, 5, 1], bins=c.categories)

When :func:`cut` is passed an ``IntervalIndex`` for the `bins` argument, The ``Interval``
values in the ``IntervalIndex`` define the bins and any value which falls outside all bins
will be assigned a ``NaN`` value.


Generating ranges of intervals
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down