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
Starting in 0.21.0, using ``.loc`` with a list-like containing missing key, is deprecated, in favor of ``.reindex``.
652
+
Starting in 0.21.0, using ``.loc`` or ``[]`` with a list-like containing one or more missing labels, is deprecated, in favor of ``.reindex``.
653
653
654
-
In prior versions, using ``.loc[list-of-keys]`` would work as long as *at least 1* of the keys was found (otherwise it
654
+
In prior versions, using ``.loc[list-of-labels]`` would work as long as *at least 1* of the keys was found (otherwise it
655
655
would raise a ``KeyError``). This behavior is deprecated and will show a warning message pointing to this section. The
656
656
recommeded alternative is to use ``.reindex()``.
657
657
658
658
For example.
659
659
660
660
.. ipython:: python
661
661
662
-
s = Series([1, 2, 3])
662
+
s =pd.Series([1, 2, 3])
663
663
s
664
664
665
665
Selection with all keys found is unchanged.
@@ -706,29 +706,30 @@ The idiomatic way to achieve selecting potentially not-found elmenents is via ``
706
706
707
707
s.reindex([1, 2, 3])
708
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.
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
710
711
711
.. ipython:: python
712
712
713
-
keys= [1, 2, 3]
714
-
s.loc[s.index& keys]
713
+
labels= [1, 2, 3]
714
+
s.loc[s.index.intersection(labels)]
715
715
716
716
Having a duplicated index will raise for a ``.reindex()``:
717
717
718
718
.. ipython:: python
719
719
720
720
s = pd.Series(np.arange(4), index=['a', 'a', 'b', 'c'])
721
+
labels = ['c', 'd']
721
722
722
723
.. code-block:: python
723
724
724
-
In [17]: s.reindex(['c', 'd'])
725
+
In [17]: s.reindex(labels)
725
726
ValueError: cannot reindex from a duplicate axis
726
727
727
-
The idiomatic expression again allows this operation to proceed
728
+
The idiomatic expression 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.
200
-
This is exactly the function of ``.reindex()``. This will now show a ``FutureWarning`` message; in the future this will raise ``KeyError`` (:issue:`15747`).
201
-
This warning will trigger on a ``DataFrame`` or a ``Series`` for using ``.loc[]`` on the Index, or ``[[]]`` on a ``Series``.
199
+
Previously, selecting at least 1 valid label with a list-like indexer would always succeed, returning ``NaN`` for missing labels.
200
+
This will now show a ``FutureWarning``, in the future this will raise a ``KeyError`` (:issue:`15747`).
201
+
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.
202
202
See the :ref:`deprecation docs <indexing.deprecate_loc_reindex_listlike>`.
203
203
204
204
205
205
.. ipython:: python
206
206
207
-
s = Series([1, 2, 3])
207
+
s = pd.Series([1, 2, 3])
208
208
s
209
209
210
210
Previous Behavior
@@ -223,7 +223,7 @@ Previous Behavior
223
223
Current Behavior
224
224
225
225
In [4]: s.loc[[1, 2, 3]]
226
-
Passing list-likes to .loc with any non-matching elements will raise
226
+
Passing list-likes to .loc or [] with any missing label will raise
227
227
KeyError in the future, you can use .reindex() as an alternative.
0 commit comments