You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/source/indexing.rst
+108-1
Original file line number
Diff line number
Diff line change
@@ -333,8 +333,15 @@ Selection By Label
333
333
334
334
dfl.loc['20130102':'20130104']
335
335
336
+
.. warning::
337
+
338
+
Starting in 0.21.0, pandas will show a ``FutureWarning`` if indexing with a list-of-lables and not ALL labels are present. In the future
339
+
this will raise a ``KeyError``. See :ref:`list-like Using loc with missing keys in a list is Deprecated <indexing.deprecate_loc_reindex_listlike>`
340
+
336
341
pandas provides a suite of methods in order to have **purely label based indexing**. This is a strict inclusion based protocol.
337
-
**At least 1** of the labels for which you ask, must be in the index or a ``KeyError`` will be raised! When slicing, both the start bound **AND** the stop bound are *included*, if present in the index. Integers are valid labels, but they refer to the label **and not the position**.
342
+
All of the labels for which you ask, must be in the index or a ``KeyError`` will be raised!
343
+
When slicing, both the start bound **AND** the stop bound are *included*, if present in the index.
344
+
Integers are valid labels, but they refer to the label **and not the position**.
338
345
339
346
The ``.loc`` attribute is the primary access method. The following are valid inputs:
340
347
@@ -635,6 +642,106 @@ For getting *multiple* indexers, using ``.get_indexer``
The idiomatic way to achieve selecting potentially not-found elmenents is via ``.reindex()``. See also the section on :ref:`reindexing <basics.reindexing>`.
704
+
705
+
.. ipython:: python
706
+
707
+
s.reindex([1, 2, 3])
708
+
709
+
Alternatively, if you want to select only *valid* keys, the following is idiomatic and efficient; it is guaranteed to preserve the dtype of the selection.
710
+
711
+
.. ipython:: python
712
+
713
+
labels = [1, 2, 3]
714
+
s.loc[s.index.intersection(labels)]
715
+
716
+
Having a duplicated index will raise for a ``.reindex()``:
717
+
718
+
.. ipython:: python
719
+
720
+
s = pd.Series(np.arange(4), index=['a', 'a', 'b', 'c'])
721
+
labels = ['c', 'd']
722
+
723
+
.. code-block:: python
724
+
725
+
In [17]: s.reindex(labels)
726
+
ValueError: cannot reindex from a duplicate axis
727
+
728
+
Generally, you can interesect the desired labels with the current
Previously, selecting at least 1 valid label with a list-like indexer would always succeed, returning ``NaN`` for missing labels.
277
+
This will now show a ``FutureWarning``, in the future this will raise a ``KeyError`` (:issue:`15747`).
278
+
This warning will trigger on a ``DataFrame`` or a ``Series`` for using ``.loc[]`` or ``[[]]`` when passing a list-of-labels with at least 1 missing label.
279
+
See the :ref:`deprecation docs <indexing.deprecate_loc_reindex_listlike>`.
280
+
281
+
282
+
.. ipython:: python
283
+
284
+
s = pd.Series([1, 2, 3])
285
+
s
286
+
287
+
Previous Behavior
288
+
289
+
.. code-block:: ipython
290
+
291
+
292
+
In [4]: s.loc[[1, 2, 3]]
293
+
Out[4]:
294
+
1 2.0
295
+
2 3.0
296
+
3 NaN
297
+
dtype: float64
298
+
299
+
300
+
Current Behavior
301
+
302
+
In [4]: s.loc[[1, 2, 3]]
303
+
Passing list-likes to .loc or [] with any missing label will raise
304
+
KeyError in the future, you can use .reindex() as an alternative.
0 commit comments