-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1970,6 +1970,7 @@ def iget_value(self, i, j): | |
return self.iat[i, j] | ||
|
||
def __getitem__(self, key): | ||
key = com._apply_if_callable(key, self) | ||
|
||
# shortcut if we are an actual column | ||
is_mi_columns = isinstance(self.columns, MultiIndex) | ||
|
@@ -2138,6 +2139,9 @@ def query(self, expr, inplace=False, **kwargs): | |
>>> df.query('a > b') | ||
>>> df[df.a > df.b] # same result as the previous expression | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add an Example as well |
||
""" | ||
if not isinstance(expr, compat.string_types): | ||
msg = "expr must be a string to be evaluated, {0} given" | ||
raise ValueError(msg.format(type(expr))) | ||
kwargs['level'] = kwargs.pop('level', 0) + 1 | ||
kwargs['target'] = None | ||
res = self.eval(expr, **kwargs) | ||
|
@@ -2336,6 +2340,7 @@ def _box_col_values(self, values, items): | |
name=items, fastpath=True) | ||
|
||
def __setitem__(self, key, value): | ||
key = com._apply_if_callable(key, self) | ||
|
||
# see if we can slice the rows | ||
indexer = convert_to_index_sliceable(self, key) | ||
|
@@ -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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
change input DataFrame (though pandas doesn't check it). | ||
If the values are not callable, (e.g. a Series, scalar, or array), | ||
they are simply assigned. | ||
|
||
Returns | ||
|
@@ -2513,11 +2519,7 @@ def assign(self, **kwargs): | |
# do all calculations first... | ||
results = {} | ||
for k, v in kwargs.items(): | ||
|
||
if callable(v): | ||
results[k] = v(data) | ||
else: | ||
results[k] = v | ||
results[k] = com._apply_if_callable(v, data) | ||
|
||
# ... and then assign | ||
for k, v in sorted(results.items()): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4283,8 +4283,26 @@ def _align_series(self, other, join='outer', axis=None, level=None, | |
|
||
Parameters | ||
---------- | ||
cond : boolean %(klass)s or array | ||
other : scalar or %(klass)s | ||
cond : boolean %(klass)s, array or callable | ||
If cond is callable, it is computed on the %(klass)s and | ||
should return boolean %(klass)s or array. | ||
The callable must not change input %(klass)s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know a format to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can do
I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok looks fine. thxs. going to merge. |
||
(though pandas doesn't check it). | ||
|
||
.. versionadded:: 0.18.1 | ||
|
||
A callable can be used as cond. | ||
|
||
other : scalar, %(klass)s, or callable | ||
If other is callable, it is computed on the %(klass)s and | ||
should return scalar or %(klass)s. | ||
The callable must not change input %(klass)s | ||
(though pandas doesn't check it). | ||
|
||
.. versionadded:: 0.18.1 | ||
|
||
A callable can be used as other. | ||
|
||
inplace : boolean, default False | ||
Whether to perform the operation in place on the data | ||
axis : alignment axis if needed, default None | ||
|
@@ -4304,6 +4322,9 @@ def _align_series(self, other, join='outer', axis=None, level=None, | |
def where(self, cond, other=np.nan, inplace=False, axis=None, level=None, | ||
try_cast=False, raise_on_error=True): | ||
|
||
cond = com._apply_if_callable(cond, self) | ||
other = com._apply_if_callable(other, self) | ||
|
||
if isinstance(cond, NDFrame): | ||
cond, _ = cond.align(self, join='right', broadcast_axis=1) | ||
else: | ||
|
@@ -4461,6 +4482,9 @@ def where(self, cond, other=np.nan, inplace=False, axis=None, level=None, | |
@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="False")) | ||
def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None, | ||
try_cast=False, raise_on_error=True): | ||
|
||
cond = com._apply_if_callable(cond, self) | ||
|
||
return self.where(~cond, other=other, inplace=inplace, axis=axis, | ||
level=level, try_cast=try_cast, | ||
raise_on_error=raise_on_error) | ||
|
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)