Skip to content

Commit

Permalink
Update documentation and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
wence- committed Jun 21, 2023
1 parent fef8aa1 commit 268f5e5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
18 changes: 18 additions & 0 deletions doc/source/user_guide/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ of multi-axis indexing.
* A boolean array (any ``NA`` values will be treated as ``False``).
* A ``callable`` function with one argument (the calling Series or DataFrame) and
that returns valid output for indexing (one of the above).
* A tuple of row (and column) indices whose elements are one of the
above inputs.

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

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

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

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

.. note::

Destructuring tuple keys into row (and column) indexes occurs
*before* callables are applied, so you cannot return a tuple from
a callable to index both rows and columns.

Getting values from an object with multi-axes selection uses the following
notation (using ``.loc`` as an example, but the following applies to ``.iloc`` as
well). Any of the axes accessors may be the null slice ``:``. Axes left out of
Expand Down Expand Up @@ -446,6 +456,8 @@ The ``.iloc`` attribute is the primary access method. The following are valid in
* A slice object with ints ``1:7``.
* A boolean array.
* A ``callable``, see :ref:`Selection By Callable <indexing.callable>`.
* A tuple of row (and column) indexes, whose elements are one of the
above types.

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

.. note::

For ``.iloc`` indexing, returning a tuple from the callable is
not supported, since tuple destructuring for row and column indexes
occurs *before* applying callables.

.. ipython:: python
df1 = pd.DataFrame(np.random.randn(6, 4),
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ Deprecations
- Deprecated parameter ``obj`` in :meth:`GroupBy.get_group` (:issue:`53545`)
- Deprecated positional indexing on :class:`Series` with :meth:`Series.__getitem__` and :meth:`Series.__setitem__`, in a future version ``ser[item]`` will *always* interpret ``item`` as a label, not a position (:issue:`50617`)
- Deprecated strings ``T``, ``t``, ``L`` and ``l`` denoting units in :func:`to_timedelta` (:issue:`52536`)
- 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`)
- 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`)
- Deprecated the ``method`` and ``limit`` keywords in :meth:`DataFrame.replace` and :meth:`Series.replace` (:issue:`33302`)
- Deprecated values "pad", "ffill", "bfill", "backfill" for :meth:`Series.interpolate` and :meth:`DataFrame.interpolate`, use ``obj.ffill()`` or ``obj.bfill()`` instead (:issue:`53581`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ def iloc(self) -> _iLocIndexer:
- A ``callable`` function with one argument (the calling Series or
DataFrame) and that returns valid output for indexing (one of the above).
This is useful in method chains, when you don't have a reference to the
calling object, but would like to base your selection on some value.
calling object, but would like to base your selection on
some value. Note that returning a tuple from a callable is deprecated.
- A tuple of row and column indexes. The tuple elements consist of one of the
above inputs, e.g. ``(0, 1)``.
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,14 @@ def test_single_element_ix_dont_upcast(self, float_frame):
result = df.loc[[0], "b"]
tm.assert_series_equal(result, expected)

def test_iloc_callable_tuple_return_value(self):
df = DataFrame(np.random.randn(10, 4), index=range(0, 20, 2))
msg = "callable in iLocation indexing is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
df.iloc[lambda _: (0,)]
with tm.assert_produces_warning(FutureWarning, match=msg):
df.iloc[lambda _: (0,)] = 1

def test_iloc_row(self):
df = DataFrame(np.random.randn(10, 4), index=range(0, 20, 2))

Expand Down

0 comments on commit 268f5e5

Please sign in to comment.