-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Allow where/mask/Indexers to accept callable #12539
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
Conversation
One potential problem is when you subclass |
Do you know such a package? The logic is not specific here and I don't think we have to care ATM. |
I don't know anyone that uses it that way. But since the order of consideration is relevant, I think it should be documented that the args are first tested for callability. |
let's simultaneously do all of the chained reshaping ops #11485 in 0.18.1, making them consistent. |
9b39b6f
to
f2430c4
Compare
Sure, once done and changed the title. |
|
||
.. ipython:: python | ||
|
||
# callable returns bool indexer |
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.
need to have some tests if people try to pass something like:
df.loc[lambda x: ...., list_of_columns]
which is a bit odd, but I suppose ok, otherwise we need to interpret this
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.
OK, added some tests for mixture of lamda
and scalar/list
.
Method chaininng improvements | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Following methods / ``Indexer`` now accept ``callable`` as input. It is |
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.
The following methods / indexers now acccept a callable
Current coverage is 83.91%@@ master #12539 diff @@
==========================================
Files 136 136
Lines 49908 49933 +25
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 41876 41901 +25
Misses 8032 8032
Partials 0 0
|
- ``[]`` indexing | ||
|
||
When a ``callable`` is passed, it is evaluated on the caller | ||
``Series/DataFrame`` prior to any other logics. |
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.
What do you mean by this?
ok, how about for |
You already get an error when passing a callable to |
I would definitely raise if the input to |
ok @sinhrks if you can make those mods would be great. (you might need to adjust docs/example as I think you use .query, but can just change to |
7ad04b7
to
ebe5aaf
Compare
Thanks for all the feedbacks. Updated. Lmk if I missed something. |
@@ -5489,6 +5489,269 @@ def test_none_coercion_mixed_dtypes(self): | |||
strict_nan=True) | |||
|
|||
|
|||
class TestIndexingCallable(tm.TestCase): |
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.
I would make this a separate file: indexing/test_callables.py
just minor change otherwise lgtm. |
df = pd.DataFrame({'A': [1, 2, 3], | ||
'B': [4, 5, 6], | ||
'C': [7, 8, 9]}) | ||
df.where(lambda x: x > 4, lambda x: x + 10) |
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.
I would add this example somewhere in the where docs as well (http://pandas-docs.github.io/pandas-docs-travis/indexing.html#the-where-method-and-masking)
@sinhrks Can you also update the docstrings for loc/iloc/ix (eg for loc https://github.com/pydata/pandas/blob/65ed3afd092908130905e7d86ff1b6b9c2002d00/pandas/core/indexing.py#L1314), these are used in the API docs. For the rest, +1! |
@@ -2454,8 +2459,9 @@ def assign(self, **kwargs): | |||
kwargs : keyword, value pairs | |||
keywords are the column names. If the values are | |||
callable, they are computed on the DataFrame and | |||
assigned to the new columns. If the values are | |||
not callable, (e.g. a Series, scalar, or array), | |||
assigned to the new columns. The callable must not |
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.
add a versionadded tag here (and other doc-strings)
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.
assign
impl is unchanged.
@sinhrks some very minor doc-changes. ping when pushed. |
thanks @sinhrks ! awesome enhancement! |
git diff upstream/master | flake8 --diff
Current impl is very simple like
assign
. Another idea is applying some restriction to return value fromcallable
(for example, only allow alignedDataFrame
or scalar).