-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DEPR: deprecate returning a tuple from a callable in iloc indexing #53769
Conversation
The current semantics are that tuple-destructuring of the key is performed before unwinding any callables. As such, if a callable returns a tuple for iloc, it will be handled incorrectly. To avoid this, explicitly deprecate support for this behaviour. Closes pandas-dev#53533.
Currently callables operate on axis 0 only in loc/iloc. I'd be open to discussion on letting them operate using normal deconstruction, but that's maybe a bigger discussion, especially whether we should do the same for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments.
[Aside: I did not write this quoted comment, please don't edit my comments to put words in my mouth]
This is not true, 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 |
Ready for another look, thanks |
Any thoughts on this change? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One small nit and a merge conflict otherwise looks good
This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this. |
Thanks @wence- |
Thanks, and sorry for not doing a better job babysitting this. |
The current semantics are that tuple-destructuring of the key is
performed before unwinding any callables. As such, if a callable
returns a tuple for iloc, it will be handled incorrectly. To avoid
this, explicitly deprecate support for this behaviour.
[ ] Added type annotations to new arguments/methods/functions.doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.