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
+96-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,94 @@ 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; furthermore this is more efficient, and is guaranteed to preserve the dtype of the selection.
710
+
711
+
.. ipython:: python
712
+
713
+
keys = [1, 2, 3]
714
+
s.loc[s.index & keys]
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
+
722
+
.. code-block:: python
723
+
724
+
In [17]: s.reindex(['c', 'd'])
725
+
ValueError: cannot reindex from a duplicate axis
726
+
727
+
The idiomatic expression again allows this operation to proceed
Previously, selecting at least 1 valid key with a list-like indexer would succeed and return ``NaN`` for non-found elements.
181
+
This is exactly the function of ``.reindex()``. This will now show a ``FutureWarning`` message; in the future this will raise ``KeyError`` (:issue:`15747`).
182
+
This warning will trigger on a ``DataFrame`` or a ``Series`` for using ``.loc[]`` on the Index, or ``[[]]`` on a ``Series``.
183
+
See the :ref:`deprecation docs <indexing.deprecate_loc_reindex_listlike>`.
184
+
185
+
186
+
.. ipython:: python
187
+
188
+
s = Series([1, 2, 3])
189
+
s
190
+
191
+
Previous Behavior
192
+
193
+
.. code-block:: ipython
194
+
195
+
196
+
In [4]: s.loc[[1, 2, 3]]
197
+
Out[4]:
198
+
1 2.0
199
+
2 3.0
200
+
3 NaN
201
+
dtype: float64
202
+
203
+
204
+
Current Behavior
205
+
206
+
In [4]: s.loc[[1, 2, 3]]
207
+
Passing list-likes to .loc with any non-matching elements will raise
208
+
KeyError in the future, you can use .reindex() as an alternative.
0 commit comments