Skip to content
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

API: multi-line, not inplace eval #11149

Closed
wants to merge 1 commit into from

Conversation

chris-b1
Copy link
Contributor

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
@@ -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]})
Copy link
Contributor

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
Copy link
Contributor

jreback commented Oct 8, 2015

@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
Copy link
Contributor

jreback commented Oct 8, 2015

cc @shoyer

@shoyer
Copy link
Member

shoyer commented Oct 8, 2015

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
Copy link
Contributor

jreback commented Nov 10, 2015

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

@jreback jreback modified the milestones: 0.18.0, Next Major Release Nov 10, 2015
@jreback
Copy link
Contributor

jreback commented Dec 6, 2015

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

@chris-b1
Copy link
Contributor Author

chris-b1 commented Dec 6, 2015

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

@chris-b1 chris-b1 changed the title Experimental/WIP: multi-line, not inplace eval API: multi-line, not inplace eval Dec 6, 2015

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.
Copy link
Contributor

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
Copy link
Contributor

jreback commented Dec 7, 2015

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

@jreback
Copy link
Contributor

jreback commented Dec 7, 2015

does this handle #8664 ?

@chris-b1
Copy link
Contributor Author

chris-b1 commented Dec 9, 2015

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

identifier.

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

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

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

add a versionadded

@jreback
Copy link
Contributor

jreback commented Dec 16, 2015

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

@chris-b1
Copy link
Contributor Author

chris-b1 commented Jan 3, 2016

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
Copy link
Contributor

jreback commented Jan 4, 2016

merged via 99c2d93

thanks for the nice changes!

yeh the # noqa is fine (I took one out, but otherwise ok).

@jreback jreback closed this Jan 4, 2016
@chris-b1 chris-b1 deleted the eval-inplace branch January 15, 2016 01:02
gfyoung added a commit to forking-repos/pandas that referenced this pull request Jun 20, 2017
gfyoung added a commit to forking-repos/pandas that referenced this pull request Jun 20, 2017
Deprecated back in 0.18.0

xref pandas-devgh-11149

[ci skip]
gfyoung added a commit to forking-repos/pandas that referenced this pull request Jun 20, 2017
Deprecated back in 0.18.0

xref pandas-devgh-11149

[ci skip]
gfyoung added a commit to forking-repos/pandas that referenced this pull request Jun 20, 2017
Deprecated back in 0.18.0

xref pandas-devgh-11149

[ci skip]
gfyoung added a commit to forking-repos/pandas that referenced this pull request Jun 21, 2017
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
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
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
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
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
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
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
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
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
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
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
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
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 added a commit to TomAugspurger/dask that referenced this pull request Oct 5, 2017
TomAugspurger added a commit to dask/dask that referenced this pull request Oct 5, 2017
* COMPAT: pandas TimeGrouper

xref pandas-dev/pandas#16747

* COMPAT: For pandas 0.21 CategoricalDtype

* COMPAT: For pandas 0.21.0 HTML repr changes

pandas-dev/pandas#16879

* COMPAT: For pandas 0.21.0 numexpr import

pandas-dev/pandas#17710

* COMPAT: Default for inplace is now False in eval

pandas-dev/pandas#11149
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API: DataFrame.eval, inplace option BUG: query modifies the frame when you compare with =
3 participants