Skip to content

Commit d75d758

Browse files
committed
Update documentation and add test
1 parent fef8aa1 commit d75d758

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

doc/source/user_guide/indexing.rst

+18
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ of multi-axis indexing.
6262
* A boolean array (any ``NA`` values will be treated as ``False``).
6363
* A ``callable`` function with one argument (the calling Series or DataFrame) and
6464
that returns valid output for indexing (one of the above).
65+
* A tuple of row (and column) indices whose elements are one of the
66+
above inputs.
6567

6668
See more at :ref:`Selection by Label <indexing.label>`.
6769

@@ -78,13 +80,21 @@ of multi-axis indexing.
7880
* A boolean array (any ``NA`` values will be treated as ``False``).
7981
* A ``callable`` function with one argument (the calling Series or DataFrame) and
8082
that returns valid output for indexing (one of the above).
83+
* A tuple of row (and column) indices whose elements are one of the
84+
above inputs.
8185

8286
See more at :ref:`Selection by Position <indexing.integer>`,
8387
:ref:`Advanced Indexing <advanced>` and :ref:`Advanced
8488
Hierarchical <advanced.advanced_hierarchical>`.
8589

8690
* ``.loc``, ``.iloc``, and also ``[]`` indexing can accept a ``callable`` as indexer. See more at :ref:`Selection By Callable <indexing.callable>`.
8791

92+
.. note::
93+
94+
Destructuring tuple keys into row (and column) indexes occurs
95+
*before* callables are applied, so you cannot return a tuple from
96+
a callable to index both rows and columns.
97+
8898
Getting values from an object with multi-axes selection uses the following
8999
notation (using ``.loc`` as an example, but the following applies to ``.iloc`` as
90100
well). Any of the axes accessors may be the null slice ``:``. Axes left out of
@@ -446,6 +456,8 @@ The ``.iloc`` attribute is the primary access method. The following are valid in
446456
* A slice object with ints ``1:7``.
447457
* A boolean array.
448458
* A ``callable``, see :ref:`Selection By Callable <indexing.callable>`.
459+
* A tuple of row (and column) indexes, whose elements are one of the
460+
above types.
449461

450462
.. ipython:: python
451463
@@ -547,6 +559,12 @@ Selection by callable
547559
``.loc``, ``.iloc``, and also ``[]`` indexing can accept a ``callable`` as indexer.
548560
The ``callable`` must be a function with one argument (the calling Series or DataFrame) that returns valid output for indexing.
549561

562+
.. note::
563+
564+
For ``.iloc`` indexing, returning a tuple from the callable is
565+
not supported, since tuple destructuring for row and column indexes
566+
occurs *before* applying callables.
567+
550568
.. ipython:: python
551569
552570
df1 = pd.DataFrame(np.random.randn(6, 4),

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ Deprecations
301301
- Deprecated the "method" and "limit" keywords on :meth:`Series.fillna`, :meth:`DataFrame.fillna`, :meth:`SeriesGroupBy.fillna`, :meth:`DataFrameGroupBy.fillna`, and :meth:`Resampler.fillna`, use ``obj.bfill()`` or ``obj.ffill()`` instead (:issue:`53394`)
302302
- Deprecated the ``method`` and ``limit`` keywords in :meth:`DataFrame.replace` and :meth:`Series.replace` (:issue:`33302`)
303303
- Deprecated values "pad", "ffill", "bfill", "backfill" for :meth:`Series.interpolate` and :meth:`DataFrame.interpolate`, use ``obj.ffill()`` or ``obj.bfill()`` instead (:issue:`53581`)
304+
- Deprecated support for returning a tuple from a callable in ``iloc``-indexing. Place callables inside a tuple if you need to generate row and column indices using functions (:issue:`53533`)
304305

305306
.. ---------------------------------------------------------------------------
306307
.. _whatsnew_210.performance:

pandas/core/indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ def iloc(self) -> _iLocIndexer:
163163
- A ``callable`` function with one argument (the calling Series or
164164
DataFrame) and that returns valid output for indexing (one of the above).
165165
This is useful in method chains, when you don't have a reference to the
166-
calling object, but would like to base your selection on some value.
166+
calling object, but would like to base your selection on
167+
some value. Note that returning a tuple from a callable is deprecated.
167168
- A tuple of row and column indexes. The tuple elements consist of one of the
168169
above inputs, e.g. ``(0, 1)``.
169170

pandas/tests/frame/indexing/test_indexing.py

+8
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,14 @@ def test_single_element_ix_dont_upcast(self, float_frame):
10001000
result = df.loc[[0], "b"]
10011001
tm.assert_series_equal(result, expected)
10021002

1003+
def test_iloc_callable_tuple_return_value(self):
1004+
df = DataFrame(np.random.randn(10, 4), index=range(0, 20, 2))
1005+
msg = "callable in iLocation indexing is deprecated"
1006+
with tm.assert_produces_warning(FutureWarning, match=msg):
1007+
df.iloc[lambda _: (0,)]
1008+
with tm.assert_produces_warning(FutureWarning, match=msg):
1009+
df.iloc[lambda _: (0,)] = 1
1010+
10031011
def test_iloc_row(self):
10041012
df = DataFrame(np.random.randn(10, 4), index=range(0, 20, 2))
10051013

0 commit comments

Comments
 (0)