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 2 commits
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
14 changes: 11 additions & 3 deletions doc/source/user_guide/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -965,21 +965,29 @@ 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
a useful pandas idiom. First, We call :func:`cut` with some data and ``bins`` set to a
fixed number, to establish the bins. Then, we can pass the value of ``.categories`` as
the ``bins`` argument in subsequent calls to :func:`cut`, supplying new data which
will be 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.
Copy link
Member

Choose a reason for hiding this comment

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

I would keep it to the last part here (the former is already explained before the example), like "Values that fall outside the passed Intervals will be assigned a NaN value".

Copy link
Author

Choose a reason for hiding this comment

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

You're right. Updated.



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