-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API: Sort keys for DataFrame.assign #9818
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
Conversation
5cf5951
to
babd7ae
Compare
👍 This looks great to me! |
assert_frame_equal(result, expected) | ||
|
||
def test_assign_alphabetical(self): | ||
df = DataFrame([[1, 2], [3, 4]], columns=['A', 'B']) |
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.
put the issue number here as a comment
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.
✔️
Previously the order was arbitrary. For predicitability, we'll sort before inserting.
babd7ae
to
758a905
Compare
agreed, you may want to add an example in the docs where you show using chained assignment (if for example you have an evaluation order requirement), .eg.
|
I've got one like that in the warning box (df.assign(C = lambda x: x['A'] + x['B'])
.assign(D = lambda x: x['A'] + x['C'])) |
@TomAugspurger small not related note: you pushed your branch to upstream instead of master (no problem for now of course, but we should try to not do that for PRs I think) |
@jorisvandenbossche are you talking about f00d6bb ? I reorganize the whatsnew entry when merging that commit. Or I could be confused about what I did. But I think I pushed this branch to origin. |
no, I was talking about this pr, you apparantly pushed it to upstream (see at the top of the PR, I noticed it because did a |
@jorisvandenbossche I see what you mean now. Sorry about that. I'll clean it up when this gets merged. Speaking of which, any objections to merging? |
this looks ok, go ahead and merge |
API: Sort keys for DataFrame.assign
Closes #9777
Previously the order of new columns from
.assign
was arbitrary. For predictability, we'll sort before inserting.We need to be comfortable with this change since we can't change behavior later with a keyword arg.
Technically we could allow referencing the a column defined within the same call to
assign
as long as they are sorted. e.g.df.assign(C=df.A + df.B, D=df.C**2)
would work, but notdf.assign(df.D=df.A +df.B, C=df.D**2)
. But I don't think we should.cc @mrocklin