API: multi-line, not inplace eval by chris-b1 · Pull Request #11149 · pandas-dev/pandas (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation29 Commits1 Checks0 Files changed

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 }})

chris-b1

closes #9297
closes #8664

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 jreback added this to the Next Major Release milestone

Oct 8, 2015

jreback

@@ -1256,6 +1256,44 @@ def f():
expected['c'] = expected['a'] + expected['b']
assert_frame_equal(df, expected)
def test_multi_line_expression(self):
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})

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

@jreback

@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?

@jreback

@shoyer

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.

@jreback

@chris-b1 going to mark this for 0.18.0 as putting the deprecation warning in-place (pun intended)

@jreback

@chris-b1 want to update / add in deprecation warning as discussed

@chris-b1

@jreback - this is updated - added a whatsnew and deprecation warnings.

@chris-b1 chris-b1 changed the titleExperimental/WIP: multi-line, not inplace eval API: multi-line, not inplace eval

Dec 6, 2015

jreback

WARNING: inplace=None currently falls back to to True, but
in a future version, will default to False. Use inplace=True
explicitly rather than relying on the default.

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)

@jreback

pls review the eval docs for any updates (in enhancingperf.rst)

@jreback

@chris-b1

@jreback
Updated the enhancingperf docs, and added a fix/test for #8664

jreback

identifier.
The ``inplace`` keyword determines whether this assignment will performed
on the original ``DataFrame`` or return a copy with the new column.

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

jreback

@@ -2048,6 +2048,9 @@ def query(self, expr, **kwargs):
The query string to evaluate. You can refer to variables
in the environment by prefixing them with an '@' character like
``@a + b``.
inplace : bool

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a versionadded

@jreback

lgtm. couple of doc changes. squash & ping when green.

@chris-b1

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?

@jreback

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

Jun 20, 2017

@gfyoung

gfyoung added a commit to forking-repos/pandas that referenced this pull request

Jun 20, 2017

@gfyoung

gfyoung added a commit to forking-repos/pandas that referenced this pull request

Jun 20, 2017

@gfyoung

gfyoung added a commit to forking-repos/pandas that referenced this pull request

Jun 20, 2017

@gfyoung

gfyoung added a commit to forking-repos/pandas that referenced this pull request

Jun 21, 2017

@gfyoung

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

Jun 21, 2017

@gfyoung

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

Jun 21, 2017

@gfyoung

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

Jun 21, 2017

@gfyoung

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

Jun 21, 2017

@gfyoung

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

Jun 23, 2017

@gfyoung

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

Jun 23, 2017

@gfyoung

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

Jun 23, 2017

@gfyoung

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

Jun 23, 2017

@gfyoung

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

Jun 24, 2017

@gfyoung

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

Jul 1, 2017

@gfyoung

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

Jul 2, 2017

@gfyoung

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

Jul 3, 2017

@gfyoung

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

Oct 5, 2017

@TomAugspurger

TomAugspurger added a commit to TomAugspurger/dask that referenced this pull request

Oct 5, 2017

@TomAugspurger

TomAugspurger added a commit to dask/dask that referenced this pull request

Oct 5, 2017

@TomAugspurger