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
+110-2
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 with missing labels. 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,107 @@ 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>`.
705
+
706
+
.. ipython:: python
707
+
708
+
s.reindex([1, 2, 3])
709
+
710
+
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.
711
+
712
+
.. ipython:: python
713
+
714
+
labels = [1, 2, 3]
715
+
s.loc[s.index.intersection(labels)]
716
+
717
+
Having a duplicated index will raise for a ``.reindex()``:
718
+
719
+
.. ipython:: python
720
+
721
+
s = pd.Series(np.arange(4), index=['a', 'a', 'b', 'c'])
722
+
labels = ['c', 'd']
723
+
724
+
.. code-block:: ipython
725
+
726
+
In [17]: s.reindex(labels)
727
+
ValueError: cannot reindex from a duplicate axis
728
+
729
+
Generally, you can interesect the desired labels with the current
Previously, selecting with a list of labels, where one or more labels were missing would always succeed, returning ``NaN`` for missing labels.
309
+
This will now show a ``FutureWarning``, in the future this will raise a ``KeyError`` (:issue:`15747`).
310
+
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.
311
+
See the :ref:`deprecation docs <indexing.deprecate_loc_reindex_listlike>`.
312
+
313
+
314
+
.. ipython:: python
315
+
316
+
s = pd.Series([1, 2, 3])
317
+
s
318
+
319
+
Previous Behavior
320
+
321
+
.. code-block:: ipython
322
+
323
+
In [4]: s.loc[[1, 2, 3]]
324
+
Out[4]:
325
+
1 2.0
326
+
2 3.0
327
+
3 NaN
328
+
dtype: float64
329
+
330
+
331
+
Current Behavior
332
+
333
+
.. code-block:: ipython
334
+
335
+
In [4]: s.loc[[1, 2, 3]]
336
+
Passing list-likes to .loc or [] with any missing label will raise
337
+
KeyError in the future, you can use .reindex() as an alternative.
The idiomatic way to achieve selecting potentially not-found elmenents is via ``.reindex()``
349
+
350
+
.. ipython:: python
351
+
352
+
s.reindex([1, 2, 3])
353
+
354
+
Selection with all keys found is unchanged.
355
+
356
+
.. ipython:: python
357
+
358
+
s.loc[[1, 2]]
359
+
360
+
303
361
.. _whatsnew_0210.api_breaking.pandas_eval:
304
362
305
363
Improved error handling during item assignment in pd.eval
@@ -607,6 +665,7 @@ Deprecations
607
665
- ``pd.TimeGrouper`` is deprecated in favor of :class:`pandas.Grouper` (:issue:`16747`)
608
666
- ``cdate_range`` has been deprecated in favor of :func:`bdate_range`, which has gained ``weekmask`` and ``holidays`` parameters for building custom frequency date ranges. See the :ref:`documentation <timeseries.custom-freq-ranges>` for more details (:issue:`17596`)
609
667
- passing ``categories`` or ``ordered`` kwargs to :func:`Series.astype` is deprecated, in favor of passing a :ref:`CategoricalDtype <whatsnew_0210.enhancements.categorical_dtype>` (:issue:`17636`)
668
+
- Passing a non-existant column in ``.to_excel(..., columns=)`` is deprecated and will raise a ``KeyError`` in the future (:issue:`17295`)
0 commit comments