Skip to content

Commit eff8af7

Browse files
committed
changed version checking if and docstring
1 parent ebaff83 commit eff8af7

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Other Enhancements
117117
- :func:`MultiIndex.is_monotonic_decreasing` has been implemented. Previously returned ``False`` in all cases. (:issue:`16554`)
118118
- :func:`Categorical.rename_categories` now accepts a dict-like argument as `new_categories` and only updates the categories found in that dict. (:issue:`17336`)
119119
- :func:`read_excel` raises ``ImportError`` with a better message if ``xlrd`` is not installed. (:issue:`17613`)
120-
- :func:`assign` will preserve the original order of **kwargs for Python 3.6+ users
120+
- :meth:`DataFrame.assign` will preserve the original order of ``**kwargs`` for Python 3.6+ users instead of sorting the column names
121121

122122

123123
.. _whatsnew_0210.api_breaking:

pandas/core/frame.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
from pandas.compat import (range, map, zip, lrange, lmap, lzip, StringIO, u,
8383
OrderedDict, raise_with_traceback)
8484
from pandas import compat
85+
from pandas.compat import PY36
8586
from pandas.compat.numpy import function as nv
8687
from pandas.util._decorators import Appender, Substitution
8788
from pandas.util._validators import validate_bool_kwarg
@@ -2575,12 +2576,12 @@ def assign(self, **kwargs):
25752576
25762577
Notes
25772578
-----
2578-
Since ``kwargs`` is a dictionary, the order of your
2579-
arguments may not be preserved if you are using Python 3.5 and earlier.
2580-
To make things predicatable, the columns are inserted in alphabetical
2581-
order, at the end of your DataFrame. Assigning multiple columns within
2582-
the same ``assign`` is possible, but you cannot reference other columns
2583-
created within the same ``assign`` call.
2579+
For python 3.6 and above, the columns are inserted in the order of
2580+
**kwargs. For python 3.5 and earlier, since **kwargs is unordered,
2581+
the columns are inserted in alphabetical order at the end of your
2582+
DataFrame. Assigning multiple columns within the same ``assign``
2583+
is possible, but you cannot reference other columns created within
2584+
the same ``assign`` call.
25842585
25852586
Examples
25862587
--------
@@ -2624,11 +2625,11 @@ def assign(self, **kwargs):
26242625
for k, v in kwargs.items():
26252626
results[k] = com._apply_if_callable(v, data)
26262627

2627-
# sort by key for 3.5 and earlier, but preserve order for 3.6 and later
2628-
if sys.version_info <= (3, 5):
2629-
results = sorted(results.items())
2630-
else:
2628+
# preserve order for 3.6 and later, but sort by key for 3.5 and earlier
2629+
if PY36:
26312630
results = results.items()
2631+
else:
2632+
results = sorted(results.items())
26322633
# ... and then assign
26332634
for k, v in results:
26342635
data[k] = v

pandas/tests/frame/test_mutate_columns.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
from pandas.compat import range, lrange
66
import numpy as np
7-
import sys
7+
from pandas.compat import PY36
88

99
from pandas import DataFrame, Series, Index, MultiIndex
1010

@@ -67,12 +67,12 @@ def test_assign_order(self):
6767
df = DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])
6868
result = df.assign(D=df.A + df.B, C=df.A - df.B)
6969

70-
if sys.version_info <= (3, 5):
71-
expected = DataFrame([[1, 2, -1, 3], [3, 4, -1, 7]],
72-
columns=list('ABCD'))
73-
else:
70+
if PY36:
7471
expected = DataFrame([[1, 2, 3, -1], [3, 4, 7, -1]],
7572
columns=list('ABDC'))
73+
else:
74+
expected = DataFrame([[1, 2, -1, 3], [3, 4, -1, 7]],
75+
columns=list('ABCD'))
7676
assert_frame_equal(result, expected)
7777
result = df.assign(C=df.A - df.B, D=df.A + df.B)
7878

0 commit comments

Comments
 (0)