API: multi-line, not inplace eval by chris-b1 · Pull Request #11149 · pandas-dev/pandas (original) (raw)
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
This is just a proof of concept at this point - the idea is to allow multiple assignments within an eval block, and also include an inplace argument (currently default True to match old behavior, although maybe that should be changed) for chaining.
In [15]: df = pd.DataFrame({'a': np.linspace(0, 100), 'b': range(50)})
In [16]: df.eval("""
...: c = a * 2
...: d = c / 2.0
...: e = a + b + c""")
In [17]: df.head()
Out[17]:
a b c d e
0 0.000000 0 0.000000 0.000000 0.000000
1 2.040816 1 4.081633 2.040816 7.122449
2 4.081633 2 8.163265 4.081633 14.244898
3 6.122449 3 12.244898 6.122449 21.367347
4 8.163265 4 16.326531 8.163265 28.489796
In [18]: df.eval("""
...: f = e * 2
...: g = f * 1.5""", inplace=False).head()
Out[18]:
a b c d e f g
0 0.000000 0 0.000000 0.000000 0.000000 0.000000 0.000000
1 2.040816 1 4.081633 2.040816 7.122449 14.244898 21.367347
2 4.081633 2 8.163265 4.081633 14.244898 28.489796 42.734694
3 6.122449 3 12.244898 6.122449 21.367347 42.734694 64.102041
4 8.163265 4 16.326531 8.163265 28.489796 56.979592 85.469388
jreback added this to the Next Major Release milestone
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add the issue as a comment
@chris-b1 this looks reasonable. I think that we should change the inplace to False by default. That obviously is a breaking change. So what I think we could do is this:
make inplace=None the default
detect if an assignment is being made. if inplace is None, then show a deprecation warning.
this implicity makes inplace=None -> inplace=False for non-assignments (which is what it currently does, and is quite reasonable). To get inplace then assignment you would have to be explict about passing inplace=True, which is what we would want.
yes?
Yes, this looks great!
I agree that we should change the default behavior to inplace=True in a version or two. I think @jreback has the right idea on the migration path.
@chris-b1 going to mark this for 0.18.0 as putting the deprecation warning in-place (pun intended)
@chris-b1 want to update / add in deprecation warning as discussed
@jreback - this is updated - added a whatsnew and deprecation warnings.
chris-b1 changed the title
Experimental/WIP: multi-line, not inplace eval API: multi-line, not inplace eval
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's add .assign in the See also (as .assign is basically what df.eval('c=a+b',inplace=False) IS)
pls review the eval docs for any updates (in enhancingperf.rst)
@jreback
Updated the enhancingperf docs, and added a fix/test for #8664
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a versionadded tag for inplace
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a versionadded
lgtm. couple of doc changes. squash & ping when green.
Updated for your doc comment and PEP8 - nothing major but did have to add a couple # noqa where a name was only used in an eval block - is that the right approach?
merged via 99c2d93
thanks for the nice changes!
yeh the # noqa is fine (I took one out, but otherwise ok).
This was referenced
Jan 4, 2016
gfyoung added a commit to forking-repos/pandas that referenced this pull request
gfyoung added a commit to forking-repos/pandas that referenced this pull request
gfyoung added a commit to forking-repos/pandas that referenced this pull request
gfyoung added a commit to forking-repos/pandas that referenced this pull request
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
gfyoung added a commit to forking-repos/pandas that referenced this pull request
Deprecated in 0.18.0.
xref pandas-devgh-11149.
Also patches bug where we were improperly handling the inplace=False condition, as we were assuming that target input was non-None when that wasn't necessarily enforced.
TomAugspurger added a commit to TomAugspurger/dask that referenced this pull request
TomAugspurger added a commit to TomAugspurger/dask that referenced this pull request
TomAugspurger added a commit to dask/dask that referenced this pull request