-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
updating row data within apply from other rows doesn't work #8662
Comments
Not really sure what you are trying to do. This will get you started.
Your function doesn't work because you have duplicates in |
when i change my function not to update back the first row but rather a copy of the first row it works as expected but shouldn't it work even when i update the first row directly in apply? def f(x): CurrentPeriodEnd Investor InvestorID PriorPeriodEnd |
their is some detection logic to see if you have mutated the return from groupby-apply. in general this is not a good (and is completely a non-performant soln). This might be an edge case, but since you have duplicates and you are not handling them this is not a bug. You don't want to mutate things, its bad practice and can lead to very subtle issues. |
tell you what. I'll mark it as a bug. can you update the top of the issue with 2 examples both copy-pastable that should yield the same results? and if you want to look at it would be great! (those 2 examples are basically the tests). I won't have time for a while. |
thanks for looking into this. I have updated the top comment with both versions of the function - one which mutates and doesn't work and the other one which copies and works. I still don't understand where the duplicates come into play? Once the dataframe is split into groups by investor id, I don't have any duplicates in x - neither indexes nor columns. I'm using positional reference iloc to set values of the first row with values of the second row. Also I debugged this code and it picks up singular values (in my case dates) both from the assignor and assignee (once the assignment of the value is made). I also used the print statements within the apply function and everything is printing as expected. If you could clarify or demonstrate where I have duplicates in x, it would be great |
on 2nd thought, I think this is almost the same bug as #8467 (but in a different case). So should be straightforward to fix (I think). want to give a shot? basically step thru the code with the working one, and note what is happening (in the reassembly portion). |
Closing in favor of #12653 |
I'm grouping rows by column investor id. within apply, when a group has more than 1 row, i'm copying a value in the second row in the group into the a column of the first row in that group. I'm using groupby with apply but the assembled result is incorrect - all rows except row 1 show the same investor B - seems like a bug?
this function updates the first positional row in a group with values from the second row
where a group has 2 rows or more
this is not an expected result - the investor column should not have repeating BBB values
the function below copies the first row first before making updates and it works as expected
this is what the result should look like
The text was updated successfully, but these errors were encountered: