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.
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
+
In [4]: s.loc[[1, 2, 3]]
292
+
Out[4]:
293
+
1 2.0
294
+
2 3.0
295
+
3 NaN
296
+
dtype: float64
297
+
298
+
299
+
Current Behavior
300
+
301
+
.. code-block:: ipython
302
+
303
+
In [4]: s.loc[[1, 2, 3]]
304
+
Passing list-likes to .loc or [] with any missing label will raise
305
+
KeyError in the future, you can use .reindex() as an alternative.
The idiomatic way to achieve selecting potentially not-found elmenents is via ``.reindex()``
317
+
318
+
.. ipython:: python
319
+
320
+
s.reindex([1, 2, 3])
321
+
322
+
Selection with all keys found is unchanged.
323
+
324
+
.. ipython:: python
325
+
326
+
s.loc[[1, 2]]
327
+
328
+
271
329
.. _whatsnew_0210.api_breaking.pandas_eval:
272
330
273
331
Improved error handling during item assignment in pd.eval
@@ -572,6 +630,7 @@ Deprecations
572
630
- :func:`DataFrame.as_blocks` is deprecated, as this is exposing the internal implementation (:issue:`17302`)
573
631
- ``pd.TimeGrouper`` is deprecated in favor of :class:`pandas.Grouper` (:issue:`16747`)
574
632
- ``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`)
633
+
- Passing a non-existant column in ``.to_excel(..., columns=)`` is deprecated and will raise a ``KeyError`` in the future (:issue:`17295`)
0 commit comments