Skip to content
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

Fix dataarray drop attrs #10030

Merged
merged 12 commits into from
Feb 9, 2025
Merged

Conversation

j-haacker
Copy link
Contributor

This PR is related to Issue #10027

It fixes the issue and adds tests to prevent similar issues in future.

I did not document the change in whats-new.

I'm also not 100 % happy with the solution. I would welcome an experienced contributor to review the changes.

Copy link

welcome bot commented Feb 6, 2025

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

xarray/core/dataarray.py Outdated Show resolved Hide resolved
@j-haacker j-haacker force-pushed the fix_dataarray_drop_attrs branch from 8695590 to f3d7414 Compare February 8, 2025 14:11
@max-sixty
Copy link
Collaborator

A welcome follow-up would be adding attrs to _replace, like we have in Dataset. I'm not sure why it's missing tbh. But no obligation.

Could we add a whatsnew and then merge?

@j-haacker j-haacker force-pushed the fix_dataarray_drop_attrs branch from e1f39a7 to f2175fe Compare February 9, 2025 01:57
@j-haacker
Copy link
Contributor Author

I added attrs to _replace by replacing the attrs of the variable because attrs cannot be passed to the DataArray constructor by themselves.

I also updated the whats-new.rst.

Copy link
Collaborator

@max-sixty max-sixty left a comment

Choose a reason for hiding this comment

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

Excellent! Thanks!

@max-sixty
Copy link
Collaborator

@headtr1ck you need to approve for us to merge

Copy link
Collaborator

@headtr1ck headtr1ck left a comment

Choose a reason for hiding this comment

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

Will approve to not delay this further, but I think there is still a flaw in the logic that should be fixed.

xarray/core/dataarray.py Show resolved Hide resolved
if attrs is _default:
attrs = copy.copy(self.attrs)
else:
variable = self.variable.copy()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this is correct.
This will overwrite a given variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, true, I'll fix this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks a lot @headtr1ck , sorry to have missed this

xarray/core/dataarray.py Show resolved Hide resolved
@j-haacker
Copy link
Contributor Author

j-haacker commented Feb 9, 2025

I fixed the issue @headtr1ck raised, that replacing a variable and attributes simultaneously would "skip" the variable.

Further, I added tests for _replace. However:

  1. I noticed the methods let by one "_" don't have tests. Are they tested somewhere else or not meant to be tested?
  2. I'm not sure about the desired replacement logic if a variable is replaced that has attributes.

(2) I made the assumption that if a variable and attributes are replaced simultaneously, the passed attributes override the variable attributes. And further, if the replacement variable contains attributes they should replace any original attributes. I made both explicit in the test. I'm happy to change the behavior or add a warning.

@max-sixty
Copy link
Collaborator

Great — we do try and test private methods, but sometimes assume they're implicitly tested. When making a change to them, adding a test like you most recently did makes a lot of sense.

Thanks a lot @j-haacker ! Appreciate you making the underlying code better as well as fixing the immediate problem!

@max-sixty max-sixty merged commit 54946eb into pydata:main Feb 9, 2025
29 checks passed
Copy link

welcome bot commented Feb 9, 2025

Congratulations on completing your first pull request! Welcome to Xarray! We are proud of you, and hope to see you again! celebration gif

@j-haacker j-haacker deleted the fix_dataarray_drop_attrs branch February 9, 2025 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataArray().drop_attrs(deep=False) doesn't do anything
3 participants