DEPR: deprecate returning a tuple from a callable in iloc indexing by wence- · Pull Request #53769 · pandas-dev/pandas (original) (raw)
[Aside: I did not write this quoted comment, please don't edit my comments to put words in my mouth]
Currently callables operate on axis 0 only in loc/iloc.
This is not true, LocationIndexer
has this implementation in __getitem__
:
def __getitem__(self, key):
check_dict_or_set_indexers(key)
if type(key) is tuple:
key = tuple(list(x) if is_iterator(x) else x for x in key)
key = tuple(com.apply_if_callable(x, self.obj) for x in key)
if self._is_scalar_access(key):
return self.obj._get_value(*key, takeable=self._takeable)
return self._getitem_tuple(key)
So, if the key is a tuple, we destructure, and then apply the callable separately on the destructured axis-wise indexers if necessary.
Witness:
In [2]: df = pd.DataFrame({"a": [1, 2, 3], "b": [1, 2, 3], "c": [2, 3, 4]})
In [3]: df.iloc[[0, 1], lambda df: [1, 2]] Out[3]: b c 0 1 2 1 2 3
In [4]: df.loc[[1, 0], lambda df: ["a", "c"]] Out[4]: a c 1 2 3 0 1 2