Skip to content

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

Merged
merged 2 commits into from
Sep 24, 2017

Conversation

bobhaffner
Copy link
Contributor

@@ -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
Copy link
Contributor

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".

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.
Copy link
Contributor

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 :)

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):
Copy link
Contributor

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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Sep 22, 2017 via email

@jreback
Copy link
Contributor

jreback commented Sep 22, 2017

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.

we already have this which is PY36 means >= 3.6, or you can do not PY35 which is < 3.6.

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Sep 22, 2017 via email

@bobhaffner
Copy link
Contributor Author

Thanks for the feedback, gents. I'll get these fixed

@bobhaffner bobhaffner force-pushed the py36_assign_original_order branch from 0032c23 to eff8af7 Compare September 23, 2017 04:03
@codecov
Copy link

codecov bot commented Sep 23, 2017

Codecov Report

Merging #17632 into master will decrease coverage by 0.01%.
The diff coverage is 83.33%.

Impacted file tree graph

@@            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
Flag Coverage Δ
#multiple 88.99% <83.33%> (-0.01%) ⬇️
#single 40.18% <83.33%> (-0.06%) ⬇️
Impacted Files Coverage Δ
pandas/core/frame.py 97.72% <83.33%> (-0.15%) ⬇️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4004367...eff8af7. Read the comment docs.

@codecov
Copy link

codecov bot commented Sep 23, 2017

Codecov Report

Merging #17632 into master will decrease coverage by 0.01%.
The diff coverage is 83.33%.

Impacted file tree graph

@@            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
Flag Coverage Δ
#multiple 88.99% <83.33%> (-0.01%) ⬇️
#single 40.18% <83.33%> (-0.06%) ⬇️
Impacted Files Coverage Δ
pandas/core/frame.py 97.72% <83.33%> (-0.15%) ⬇️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4004367...eff8af7. Read the comment docs.

@jreback jreback added this to the 0.21.0 milestone Sep 24, 2017
@jreback jreback merged commit 965c1c8 into pandas-dev:master Sep 24, 2017
@jreback
Copy link
Contributor

jreback commented Sep 24, 2017

thanks @bobhaffner

@bobhaffner
Copy link
Contributor Author

It's the least I could do, @jreback. Thanks to you and @TomAugspurger for the guidance and everything else you do for the project

@bobhaffner bobhaffner deleted the py36_assign_original_order branch September 25, 2017 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API: allow dependent assignment?
4 participants