-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: df.assign accepting dependent **kwargs (#14207) #18852
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
pandas/core/frame.py
Outdated
columns created within the same ``assign`` call. For python 3.6 and | ||
above it is possible to reference columns created in an assignment. | ||
To this end you have to respect the order of |*|*kwargs and use | ||
callables referencing the assigned columns. |
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.
python --> Python
eralier --> earlier, <-- note the comma
Add comma after "To this end"
"|*|*kwargs" <-- what's 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.
sorry I fear the |*|*kwargs part is an artifact of somewhere else, will also change the earlier typo
with pytest.raises(KeyError): | ||
df.assign(C=df.A, D=lambda x: x['A'] + x['C']) | ||
if not PY36: | ||
with pytest.raises(KeyError): |
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 note as to why this is occurring. Also, I presume there is no real error message for this KeyError
.
|
||
def test_assign_dependent(self): | ||
df = DataFrame({'A': [1, 2], 'B': [3, 4]}) | ||
if PY36: |
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.
- Reference issue number below function declaration
- You might want to consider decorating with a test-skipper so that we don't run this test on anything below 3.6 (also no need to create the
DataFrame
if there's nothing afterwards).
doc/source/whatsnew/v0.22.0.txt
Outdated
@@ -139,6 +139,7 @@ Other Enhancements | |||
- :func:`read_excel()` has gained the ``nrows`` parameter (:issue:`16645`) | |||
- :func:``DataFrame.to_json`` and ``Series.to_json`` now accept an ``index`` argument which allows the user to exclude the index from the JSON output (:issue:`17394`) | |||
- ``IntervalIndex.to_tuples()`` has gained the ``na_tuple`` parameter to control whether NA is returned as a tuple of NA, or NA itself (:issue:`18756`) | |||
- :func:``DataFrame.assign()`` now acceepts dependent kwargs, e.g. `df.assign(b=1, c=lambda x:x['b'])` does not throw an exception anymore. (:issue: `14207) |
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.
previously this would raise (put the what it did raise), write the issue like
(:issue:`14207`)
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.
Also:
:func:``DataFrame.assign()``
-->:func:`DataFrame.assign`
(single ticks, no parenthesis)- acceepts --> accepts
`df.assign(b=1, c=lambda x:x['b'])`
-->``df.assign(b=1, c=lambda x: x['b'])``
(double ticks)
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 think it'd be worth breaking this out into its own section and highlight a case where this is an API change. Something like
.. warning::
This may subtly change the behavior of your code when you're
using ``assign`` to update an existing column. Previously, callables
refering to other variables being updated would get the "old" values
.. code-block:: ipython
In [2]: df = pd.DataFrame({"A": [1, 2, 3]})
In [3]: df.assign(A=lambda df: df.A + 1, C=lambda df: df.A * -1)
Out[3]:
A C
0 2 -1
1 3 -2
2 4 -3
Now, callables will get the "new" value
.. ipython:: python
df = pd.DataFrame({"A": [1, 2, 3]})
df.assign(A=lambda df: df.A + 1, C=lambda df: df.A * -1)
pandas/core/frame.py
Outdated
results[k] = com._apply_if_callable(v, data) | ||
|
||
# sort by key for 3.5 and earlier | ||
results = sorted(results.items()) |
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.
this is incorrectly indented
with pytest.raises(KeyError): | ||
df.assign(C=df.A, D=lambda x: x['A'] + x['C']) | ||
|
||
def test_assign_dependent(self): |
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 skipif decorator here if not PY36
df.assign(C=lambda df: df.A, D=lambda df: df['A'] + df['C']) | ||
with pytest.raises(KeyError): | ||
df.assign(C=df.A, D=lambda x: x['A'] + x['C']) | ||
if not PY36: |
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.
make this into a separate test (from the PY36) part and add a skipif PY36 decorator
thanks for the valuable feedback. If I want to recommit my pull request, is a rebase required or can I also just add a new commit with the appropriate changes. (Sorry I'm new to that). I think I will issue a new pull request (with the appropriate change) later today |
|
The tests pass and hopefully I fixed the errors. However, in the whatsnew section the exception traceback is printed out (when I ran it) due different python versions. What could be done there to avoid printing the traceback? |
doc/source/whatsnew/v0.22.0.txt
Outdated
@@ -119,6 +119,56 @@ Current Behavior | |||
|
|||
s.rank(na_option='top') | |||
|
|||
.. _whatsnew_0220.enhancements.assign_dependent: |
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.
rebase on master. can you move to 0.23 (docs were renamed), prob easiest to just check this file from master and past in new one
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.
(also 0220 -> 0230)
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 think I'll manage to rebase until Christmas.
the whatsnew page I constructed contains the traceback (since a change between python versions is shown) Is this desired? If not, is there a way to just print the code and output as in the document and not run the code actually?
f958371
to
469ed4c
Compare
I hopefully rebased correctly. Additionally, I could fix the "errors" in the whatsnew description and squashed all commits into one |
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.
doc comments. you don't need to squash fyi.
doc/source/whatsnew/v0.22.0.txt
Outdated
@@ -7,8 +7,3 @@ This is a major release from 0.21.1 and includes a number of API changes, | |||
deprecations, new features, enhancements, and performance improvements along | |||
with a large number of bug fixes. We recommend that all users upgrade to this | |||
version. |
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.
revert this file
doc/source/whatsnew/v0.23.0.txt
Outdated
``.assign()`` accepts dependent arguments | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
The :func:`DataFrame.assign()` now accepts dependent kwargs. In earlier versions this throws a Keyerror exception anymore. (:issue: `14207) |
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.
you don't need to say what it did in prior version (here)
doc/source/whatsnew/v0.23.0.txt
Outdated
|
||
The :func:`DataFrame.assign()` now accepts dependent kwargs. In earlier versions this throws a Keyerror exception anymore. (:issue: `14207) | ||
|
||
Specifically, defining a new column inside assign may be referenced in the same assign statement if a callable is used. For example |
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()
; use double-backticks.
doc/source/whatsnew/v0.23.0.txt
Outdated
|
||
Specifically, defining a new column inside assign may be referenced in the same assign statement if a callable is used. For example | ||
|
||
.. code-block:: ipython |
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.
use an ipython block (this executes the code), you don't need to show the results they will be rendered when they execute
doc/source/whatsnew/v0.23.0.txt
Outdated
using ``assign`` to update an existing column. Previously, callables | ||
refering to other variables being updated would get the "old" values | ||
|
||
.. code-block:: ipython |
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.
you can use a code block here as this is previous behavior
use
Previous Behaviour:
doc/source/whatsnew/v0.23.0.txt
Outdated
1 3 -2 | ||
2 4 -3 | ||
|
||
Now, callables will get the "new" value |
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.
New Behaviour:
doc/source/whatsnew/v0.23.0.txt
Outdated
Now, callables will get the "new" value | ||
|
||
.. code-block:: ipython | ||
|
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.
ipython block
doc/source/whatsnew/v0.23.0.txt
Outdated
``.assign()`` accepts dependent arguments | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
The :func:`DataFrame.assign()` now accepts dependent kwargs. In earlier versions this throws a Keyerror exception anymore. (:issue: `14207) |
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.
put a reference to the doc-section on .assign
in the docs which also needs updating.
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.
Apologies, but what is the correct path to refer to? What should be the updated content of the .assign
docs?
doc/source/whatsnew/v0.23.0.txt
Outdated
|
||
This may subtly change the behavior of your code when you're | ||
using ``assign`` to update an existing column. Previously, callables | ||
refering to other variables being updated would get the "old" values |
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.
referring
Codecov Report
@@ Coverage Diff @@
## master #18852 +/- ##
=========================================
Coverage ? 91.61%
=========================================
Files ? 150
Lines ? 48796
Branches ? 0
=========================================
Hits ? 44704
Misses ? 4092
Partials ? 0
Continue to review full report at Codecov.
|
Why is the coverage changing in only the documentation is changed? |
coverage is a little bit bogus :< |
doc/source/whatsnew/v0.23.0.txt
Outdated
|
||
Specifically, defining a new column inside ``.assign()`` may be referenced in the same assign statement if a callable is used. For example | ||
|
||
.. ipython:: python |
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.
if you use ipython, then you don't need to show the code, it executes
doc/source/whatsnew/v0.23.0.txt
Outdated
|
||
Previous Behaviour: | ||
|
||
.. code-block:: ipython |
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.
so this is correct here
doc/source/whatsnew/v0.23.0.txt
Outdated
|
||
New Behaviour: | ||
|
||
.. ipython:: python |
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.
but you don't need all this, just
df.assign(....) # complete the line of course
generally we would simply execute and show the DataFrame creation in an ipython block at the top of the sub-section, then do a code-block for previous, and an ipython block for enw
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.
see above
|
||
@pytest.mark.skipif(PY36, reason="""Issue #14207: valid for python | ||
3.6 and above""") | ||
def test_assign_bad_old_version(self): |
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.
can you name this something more relevant
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.
do you mean the name of the function or the reason?
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.
name of the function
doc/source/whatsnew/v0.23.0.txt
Outdated
|
||
Specifically, defining a new column inside ``.assign()`` may be referenced in the same assign statement if a callable is used. For example | ||
|
||
.. ipython:: python |
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.
pls see how other things are formatted in the whatsnew.
you need only to do
.. ipython:: python
df = pd.DataFrame({'A': [1, 2, 3]})
df
df.assign(B=df.A, C=lambda x: x['A'] + x['B'])
doc/source/whatsnew/v0.23.0.txt
Outdated
``.assign()`` accepts dependent arguments | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
The :func:`DataFrame.assign()` now accepts dependent kwargs. (:issue:`14207`) |
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.
a little more expl of what a dependenet kwarg is
doc/source/whatsnew/v0.23.0.txt
Outdated
|
||
New Behaviour: | ||
|
||
.. ipython:: python |
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.
see above
doc/source/whatsnew/v0.23.0.txt
Outdated
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
The :func:`DataFrame.assign()` now accepts dependent kwargs for python version later than 3.6 (see also `PEP 468 | ||
<https://www.python.org/dev/peps/pep-0468/>`_). Now the keyword-value pairs passed to `.assign()` may depend on their predecessors if the values are callables. (:issue:`14207`) |
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.
use :func:`DataFrame.assign`
here.
doc/source/whatsnew/v0.23.0.txt
Outdated
|
||
df = pd.DataFrame({'A': [1, 2, 3]}) | ||
df | ||
df.assign(B=df.A, C=lambda x:x['A']+ x['B']) |
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.
indent the same
pandas/core/frame.py
Outdated
results[k] = com._apply_if_callable(v, data) | ||
|
||
# preserve order for 3.6 and later, but sort by key for 3.5 and earlier | ||
# for 3.6 preserve order of kwargs |
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.
say >= 3.6
pandas/core/frame.py
Outdated
else: | ||
# for 3.5 or earlier: do all calculations first... |
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.
<= 3.5
doc/source/whatsnew/v0.23.0.txt
Outdated
|
||
.. warning:: | ||
|
||
This may subtly change the behavior of your code when you're |
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 this warning to the main docs as well in dsintro.rst
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 are the Main docs? Docstring of the assign function?
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.
http://pandas-docs.github.io/pandas-docs-travis/dsintro.html#assigning-new-columns-in-method-chains
That's in doc/source/dsintro.rst.
pandas/core/frame.py
Outdated
the same ``assign`` call. | ||
is possible, but for python 3.5 and earlier, you cannot reference | ||
other columns created within the same ``assign`` call. | ||
For python 3.6 and above it is possible to reference columns created |
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.
and Python 3.6 and later
Python 3.5 and earlier
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 talk about 3.6 and later first, since they're the future.
Assigning multiple columns within the same ``assign`` is possible. For Python 3.6 and above, later items in '\*\*kwargs' may refer to newly created or modified columns in 'df'; items are computed and assigned into 'df' in order. For Python 3.5 and below, the order of keyword arguments is not specified, you cannot refer to newly created or modified columns. All items are computed first, and then assigned in alphabetical order.
This can replace the entire "Notes" section I think.
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.
Also, could you add the example you've been using to the "Examples" section.
doc/source/whatsnew/v0.23.0.txt
Outdated
``.assign()`` accepts dependent arguments | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
The :func:`DataFrame.assign()` now accepts dependent kwargs for python version later than 3.6 (see also `PEP 468 |
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.
"kwargs" -> "keyword arguments"
doc/source/whatsnew/v0.23.0.txt
Outdated
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
The :func:`DataFrame.assign()` now accepts dependent kwargs for python version later than 3.6 (see also `PEP 468 | ||
<https://www.python.org/dev/peps/pep-0468/>`_). Now the keyword-value pairs passed to `.assign()` may depend on their predecessors if the values are callables. (:issue:`14207`) |
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.
"keyword-value pairs" -> "keyword arguments".
I'm not sure if "predecessors" is the right word. How about
"Later keyword arguments may now refer to earlier ones. (:issue:`14207`)"
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.
Sounds indeed better. Should I drop the reference to callables? (Sorry, obviously my auto correct messed up)
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.
Maybe keep "callables", so
"Later keyword arguments may now refer to earlier ones if the argument is a callable. (:issue:`14207`)"
pandas/core/frame.py
Outdated
the same ``assign`` call. | ||
is possible, but for python 3.5 and earlier, you cannot reference | ||
other columns created within the same ``assign`` call. | ||
For python 3.6 and above it is possible to reference columns created |
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 talk about 3.6 and later first, since they're the future.
Assigning multiple columns within the same ``assign`` is possible. For Python 3.6 and above, later items in '\*\*kwargs' may refer to newly created or modified columns in 'df'; items are computed and assigned into 'df' in order. For Python 3.5 and below, the order of keyword arguments is not specified, you cannot refer to newly created or modified columns. All items are computed first, and then assigned in alphabetical order.
This can replace the entire "Notes" section I think.
pandas/core/frame.py
Outdated
the same ``assign`` call. | ||
is possible, but for python 3.5 and earlier, you cannot reference | ||
other columns created within the same ``assign`` call. | ||
For python 3.6 and above it is possible to reference columns created |
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.
Also, could you add the example you've been using to the "Examples" section.
Could you add a
|
98ce484
to
75403c8
Compare
Sorry, I am not sure why travis failed on some 3.5 builds. Any ideas? Maybe I messed something up when reading? |
I restarted the builds to check, but you might need to rebase onto |
I saw some issues yesterday and already performed a rebase (I hope correctly). I think I know where the issues are coming from: I added an example in frame.py (as @TomAugspurger suggested) as a doctest which will only work for python 3.6 and above. Is it possible to skip the doctest for older python versions? If not can I post examples in the doctoring of a function also using restructured texts code-blocks? |
doc/source/dsintro.rst
Outdated
@@ -507,9 +507,41 @@ of one argument to be called on the ``DataFrame``. A *copy* of the original | |||
DataFrame is returned, with the new values inserted. | |||
|
|||
.. warning:: | |||
Starting from Python 3.6 ``**kwargs`` is an ordered dictionary and ``assign`` |
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.
use :func`DataFrame.assign`
doc/source/dsintro.rst
Outdated
@@ -507,9 +507,41 @@ of one argument to be called on the ``DataFrame``. A *copy* of the original | |||
DataFrame is returned, with the new values inserted. | |||
|
|||
.. warning:: | |||
Starting from Python 3.6 ``**kwargs`` is an ordered dictionary and ``assign`` | |||
respects the order of the keyword arguments. It is allowed to write |
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.
reword It is allowed to write
doc/source/dsintro.rst
Outdated
.. ipython:: | ||
:verbatim: | ||
|
||
In [1]: # Allowed for Python 3.6 and later |
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.
comments on a separate line. don't user verbatim, just write the expression
doc/source/dsintro.rst
Outdated
This may subtly change the behavior of your code when you're | ||
using ``.assign()`` to update an existing column. Prior to Python 3.6, | ||
callables referring to other variables being updated would get the "old" values | ||
|
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.
This previous does not need to be here, ONLY in the whatsnew new
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.
remove from here down
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, so implicitly, if one uses pandas 0.23 it is assumed that also python >= 3.6, right?
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.
why would you say that?
remove it because it is redundant (you said it above)
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.
2 minor doc changes. ping on green.
doc/source/dsintro.rst
Outdated
|
||
All expressions are computed first, and then assigned. So you can't refer | ||
to another column being assigned in the same call to ``assign``. For example: | ||
In [1]: df.assign(C = lambda x: x['A'] + x['B'], |
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.
when using an ipython:: python block, it executes the code, thus we cannot have the In [1]:
there
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.
using a simple python:: block will always execute the code and yield errors since df is (currently) not defined appropriately. So - it seems - there are two possibilities to resolve this:
- use verbatim and just show the syntax of the call (which you refused in my last commit)
- use the iris DataFrame from the section or df from a section before and modify the call w.r.t those frames.
Which way to go?
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.
Best to just make a small dataframe for the example:
.. ipython:: python
df = DataFrame({"A": [1, 2, 3],
"B": [4, 5, 6]})
df.assign(C=lambda x: x['A'] + x['B'],
D=lambda x: x['A'] + x['C'])
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.
Call it something other than df
though, since creating a new df
will break examples further down.
doc/source/dsintro.rst
Outdated
(df.assign(C = lambda x: x['A'] + x['B']) | ||
.assign(D = lambda x: x['A'] + x['C'])) | ||
This may subtly change the behavior of your code when you're | ||
using ``.assign()`` to update an existing column. Prior to Python 3.6, |
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.
:func:`DataFrame`
so what is the next thing I should do in this pull request? some rebasing required? Or just sit back and wait? |
@TomAugspurger if you'd verify this reads ok. |
is there anything left I need to do? |
can you rebase on master |
just for clarification: rebasing the changes you made to my branch on the current master branch of pandas-dev? I think I will be able to manage this over the weekend |
yes just rebase against master and push again |
Specifically, 'df.assign(b=1, c=lambda x:x['b'])' does not throw an exception in python 3.6 and above. Further details are discussed in Issues pandas-dev#14207 and pandas-dev#18797. populates dsintro and frame.py with examples and warning - adds example to frame.py - reworked warning in dsintro - reworked Notes in frame.py Remains open: frame.py probably is responsible vor travis not passing: doc test that requires python 3.6
25153d3
to
b7ec738
Compare
sorry, did the rebase just now. Had some urgent family matters to attend. Hope the rebase is correct. |
thanks! |
Specifically, 'df.assign(b=1, c=lambda x:x['b'])'
does not throw an exception in python 3.6 and above.
Further details are discussed in Issues #14207 and #18797.
closes #14207
closes #18797
git diff upstream/master -u -- "*.py" | flake8 --diff