Skip to content

Commit 144b832

Browse files
author
datajanko
committed
improves assign section in dsintro
- reworks warning - uses :func ... - improved wording - removed comment in code sample due to redundancy Remains open: frame.py probably is responsible vor travis not passing: doc test that requires python 3.6
1 parent 75403c8 commit 144b832

File tree

1 file changed

+5
-44
lines changed

1 file changed

+5
-44
lines changed

doc/source/dsintro.rst

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -507,57 +507,18 @@ of one argument to be called on the ``DataFrame``. A *copy* of the original
507507
DataFrame is returned, with the new values inserted.
508508

509509
.. warning::
510-
Starting from Python 3.6 ``**kwargs`` is an ordered dictionary and ``assign``
511-
respects the order of the keyword arguments. It is allowed to write
510+
Starting from Python 3.6 ``**kwargs`` is an ordered dictionary and :func`DataFrame.assign`
511+
respects the order of the keyword arguments. You may now use assign in the following way:
512512

513-
.. ipython::
514-
:verbatim:
513+
.. ipython:: python
515514
516-
In [1]: # Allowed for Python 3.6 and later
517-
df.assign(C = lambda x: x['A'] + x['B'],
518-
D = lambda x: x['A'] + x['C'])
515+
In [1]: df.assign(C = lambda x: x['A'] + x['B'],
516+
D = lambda x: x['A'] + x['C'])
519517
520518
This may subtly change the behavior of your code when you're
521519
using ``.assign()`` to update an existing column. Prior to Python 3.6,
522520
callables referring to other variables being updated would get the "old" values
523521

524-
Previous Behaviour:
525-
526-
.. code-block:: ipython
527-
528-
In [2]: df = pd.DataFrame({"A": [1, 2, 3]})
529-
530-
In [3]: df.assign(A=lambda df: df.A + 1, C=lambda df: df.A * -1)
531-
Out[3]:
532-
A C
533-
0 2 -1
534-
1 3 -2
535-
2 4 -3
536-
537-
New Behaviour:
538-
539-
.. ipython:: python
540-
541-
df.assign(A=df.A+1, C= lambda df: df.A* -1)
542-
543-
For Python 3.5 and earlier the function signature of ``assign`` is ``**kwargs``,
544-
a dictionary, the order of the new columns in the resulting DataFrame cannot be guaranteed
545-
to match the order you pass in. To make things predictable, items are inserted
546-
alphabetically (by key) at the end of the DataFrame.
547-
548-
All expressions are computed first, and then assigned. So you can't refer
549-
to another column being assigned in the same call to ``assign``. For example:
550-
551-
.. ipython::
552-
:verbatim:
553-
554-
In [1]: # Don't do this, bad reference to `C`
555-
df.assign(C = lambda x: x['A'] + x['B'],
556-
D = lambda x: x['A'] + x['C'])
557-
In [2]: # Instead, break it into two assigns
558-
(df.assign(C = lambda x: x['A'] + x['B'])
559-
.assign(D = lambda x: x['A'] + x['C']))
560-
561522
Indexing / Selection
562523
~~~~~~~~~~~~~~~~~~~~
563524
The basics of indexing are as follows:

0 commit comments

Comments
 (0)