Skip to content
Open
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
18 changes: 18 additions & 0 deletions doc/source/user_guide/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,24 @@ an error will be raised. For instance, in the above example, ``s.loc[2:5]`` woul
For more information about duplicate labels, see
:ref:`Duplicate Labels <duplicates>`.

Also, when using a slice with a step, such as ``.loc[start:stop:step]``, note that
Copy link
Member

Choose a reason for hiding this comment

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

The previous paragraph also starts with Also,, just from a prose standpoint I think we should avoid this.

Suggested change
Also, when using a slice with a step, such as ``.loc[start:stop:step]``, note that
When using a slice with a step, such as ``.loc[start:stop:step]``, note that

*start* and *stop* are interpreted as **labels**, while *step* is applied over
the **positional index** within that label range. This means a stepped slice
may return different labels than selecting an explicit list, even when they
appear similar.
Copy link
Member

Choose a reason for hiding this comment

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

I think this could be made a little more specific.

Suggested change
the **positional index** within that label range. This means a stepped slice
may return different labels than selecting an explicit list, even when they
appear similar.
the **positional index** within that label range. This means a stepped slice
will behave differently than using the labels `range(start, stop, step)` when
the index is not contiguous integers.


For example, in a ``Series`` with a non-contiguous integer index:

.. ipython:: python
s = pd.Series(range(10), index=[0, 5, 10, 15, 20, 25, 30, 35, 40, 45])
s.loc[10:50:5] # (10), then skip 3 positions → 35 only
Copy link
Member

Choose a reason for hiding this comment

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

Could you also show a similar example where the index is not numeric?

s.loc[[10, 15, 20, 25]] # explicit label selection
The first applies *step* across **positional locations** between the start/stop
labels. The second selects each label directly.


.. _indexing.integer:

Selection by position
Expand Down
Loading