|
82 | 82 | from pandas.compat import (range, map, zip, lrange, lmap, lzip, StringIO, u,
|
83 | 83 | OrderedDict, raise_with_traceback)
|
84 | 84 | from pandas import compat
|
| 85 | +from pandas.compat import PY36 |
85 | 86 | from pandas.compat.numpy import function as nv
|
86 | 87 | from pandas.util._decorators import Appender, Substitution
|
87 | 88 | from pandas.util._validators import validate_bool_kwarg
|
@@ -2575,12 +2576,12 @@ def assign(self, **kwargs):
|
2575 | 2576 |
|
2576 | 2577 | Notes
|
2577 | 2578 | -----
|
2578 |
| - Since ``kwargs`` is a dictionary, the order of your |
2579 |
| - arguments may not be preserved. To make things predicatable, |
2580 |
| - the columns are inserted in alphabetical order, at the end of |
2581 |
| - your DataFrame. Assigning multiple columns within the same |
2582 |
| - ``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. |
2584 | 2585 |
|
2585 | 2586 | Examples
|
2586 | 2587 | --------
|
@@ -2620,14 +2621,18 @@ def assign(self, **kwargs):
|
2620 | 2621 | data = self.copy()
|
2621 | 2622 |
|
2622 | 2623 | # do all calculations first...
|
2623 |
| - results = {} |
| 2624 | + results = OrderedDict() |
2624 | 2625 | for k, v in kwargs.items():
|
2625 | 2626 | results[k] = com._apply_if_callable(v, data)
|
2626 | 2627 |
|
| 2628 | + # preserve order for 3.6 and later, but sort by key for 3.5 and earlier |
| 2629 | + if PY36: |
| 2630 | + results = results.items() |
| 2631 | + else: |
| 2632 | + results = sorted(results.items()) |
2627 | 2633 | # ... and then assign
|
2628 |
| - for k, v in sorted(results.items()): |
| 2634 | + for k, v in results: |
2629 | 2635 | data[k] = v
|
2630 |
| - |
2631 | 2636 | return data
|
2632 | 2637 |
|
2633 | 2638 | def _sanitize_column(self, key, value, broadcast=True):
|
|
0 commit comments