pandas icon indicating copy to clipboard operation
pandas copied to clipboard

Fix binary operations on attrs for Series and DataFrame

Open fbourgey opened this issue 1 year ago • 1 comments

fbourgey avatar Aug 28 '24 00:08 fbourgey

@mroeschke should everything be rewritten using finalize then?

fbourgey avatar Oct 16 '24 12:10 fbourgey

Yes, or __finalize__ needs to be probably be called somewhere

mroeschke avatar Oct 29 '24 20:10 mroeschke

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

github-actions[bot] avatar Nov 29 '24 00:11 github-actions[bot]

@mroeschke, @WillAyd, I tried using __finalize__ instead. What do you think?

fbourgey avatar Dec 02 '24 22:12 fbourgey

I think it looks good but will defer to @mroeschke

WillAyd avatar Dec 03 '24 16:12 WillAyd

I don't think the CI failures are related. This still lgtm - @mroeschke can you take another look?

WillAyd avatar Feb 03 '25 15:02 WillAyd

@WillAyd @mroeschke, any chance I could get an update?

fbourgey avatar Mar 28 '25 12:03 fbourgey

@mroeschke I suggested something. I kept some checks for attrs in _cmp_method and _arithm_method in pandas/core/frame.py

fbourgey avatar Mar 30 '25 21:03 fbourgey

This looks like a good job. However, I noticed some conditional logic added in frame.py (_cmp_method, _arith_method, _construct_result) and series.py (_construct_result):

if not getattr(self, "attrs", None) and getattr(other, "attrs", None):
    self.__finalize__(other)

This logic seems to attempt copying attributes from the other (right) operand if self (left) doesn't initially have attributes. This appears potentially incorrect, as standard pandas behavior (and the new test) suggests the result should only inherit attributes from the left operand (self). The final out.__finalize__(self) call in _construct_result should already handle this correctly.

Could we simplify the implementation by removing these conditional self.__finalize__(other) calls and relying solely on out.__finalize__(self)? This seems like it would align better with the intended behavior and the added test case. However I might be wrong, just wanted to point this out in case something there was unintentional.

a-holm avatar Mar 31 '25 07:03 a-holm

Thanks for sticking with this @fbourgey!

mroeschke avatar Apr 03 '25 19:04 mroeschke

Thanks for the help @mroeschke!

fbourgey avatar Apr 03 '25 19:04 fbourgey