-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
preserve kwargs order on assign func for py36plus - #14207 #17632
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
preserve kwargs order on assign func for py36plus - #14207 #17632
Conversation
doc/source/whatsnew/v0.21.0.txt
Outdated
@@ -117,6 +117,7 @@ Other Enhancements | |||
- :func:`MultiIndex.is_monotonic_decreasing` has been implemented. Previously returned ``False`` in all cases. (:issue:`16554`) | |||
- :func:`Categorical.rename_categories` now accepts a dict-like argument as `new_categories` and only updates the categories found in that dict. (:issue:`17336`) | |||
- :func:`read_excel` raises ``ImportError`` with a better message if ``xlrd`` is not installed. (:issue:`17613`) | |||
- :func:`assign` will preserve the original order of **kwargs for Python 3.6+ users |
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 will be
:meth:`DataFrame.assign`
And put **kwargs
in double backticks. And maybe reference the original behavior like "instead of sorting the column names".
pandas/core/frame.py
Outdated
arguments may not be preserved if you are using Python 3.5 and earlier. | ||
To make things predicatable, the columns are inserted in alphabetical | ||
order, at the end of your DataFrame. Assigning multiple columns within | ||
the same ``assign`` is possible, but you cannot reference other columns | ||
created within the same ``assign`` call. |
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 rephrase the first couple sentences to say something like
"For python 3.6 and above, the columns are inserted in the order of **kwargs. For python 3.5 and earlier, since **kwargs is unordered, the columns are inserted in alphabetical order at the end of your DataFrame.`"
And then pick up with "Assigning multiple..."
That emphasizes that 3.6+ is good :)
pandas/core/frame.py
Outdated
for k, v in kwargs.items(): | ||
results[k] = com._apply_if_callable(v, data) | ||
|
||
# sort by key for 3.5 and earlier, but preserve order for 3.6 and later | ||
if sys.version_info <= (3, 5): |
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 pandas.compat.PY36
here
expected = DataFrame([[1, 2, -1, 3], [3, 4, -1, 7]], | ||
columns=list('ABCD')) | ||
|
||
if sys.version_info <= (3, 5): |
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
This should apply to py36+, and we don't want to have to update this with
every python release. We *could* add a `PY_LT_36` in pandas.compat`, but
that seems like overkill for now.
…On Fri, Sep 22, 2017 at 4:34 PM, Jeff Reback ***@***.***> wrote:
***@***.**** requested changes on this pull request.
------------------------------
In pandas/core/frame.py
<#17632 (comment)>:
> for k, v in kwargs.items():
results[k] = com._apply_if_callable(v, data)
+ # sort by key for 3.5 and earlier, but preserve order for 3.6 and later
+ if sys.version_info <= (3, 5):
use pandas.compat.PY36 here
------------------------------
In pandas/tests/frame/test_mutate_columns.py
<#17632 (comment)>:
> # GH 9818
df = DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])
result = df.assign(D=df.A + df.B, C=df.A - df.B)
- expected = DataFrame([[1, 2, -1, 3], [3, 4, -1, 7]],
- columns=list('ABCD'))
+
+ if sys.version_info <= (3, 5):
see above
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#17632 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABQHIrOW9xVe9H__nxlGLxzPCbXEt8VJks5slCf7gaJpZM4PhNZw>
.
|
we already have this which is |
Ah, I thought those were exact. Yep let's do that then.
|
Thanks for the feedback, gents. I'll get these fixed |
0032c23
to
eff8af7
Compare
Codecov Report
@@ Coverage Diff @@
## master #17632 +/- ##
==========================================
- Coverage 91.22% 91.2% -0.02%
==========================================
Files 163 163
Lines 49655 49659 +4
==========================================
- Hits 45297 45291 -6
- Misses 4358 4368 +10
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #17632 +/- ##
==========================================
- Coverage 91.22% 91.2% -0.02%
==========================================
Files 163 163
Lines 49655 49659 +4
==========================================
- Hits 45297 45291 -6
- Misses 4358 4368 +10
Continue to review full report at Codecov.
|
thanks @bobhaffner |
It's the least I could do, @jreback. Thanks to you and @TomAugspurger for the guidance and everything else you do for the project |
git diff upstream/master -u -- "*.py" | flake8 --diff