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

Option to not auto-create index during expand_dims #8960

Merged
merged 18 commits into from
Apr 27, 2024

Conversation

TomNicholas
Copy link
Member

@TomNicholas TomNicholas commented Apr 20, 2024

TODO:

xarray/core/dataarray.py Outdated Show resolved Hide resolved
doc/whats-new.rst Outdated Show resolved Hide resolved
@TomNicholas
Copy link
Member Author

@dcherian I've addressed your comments, so would like to merge this.

@TomNicholas TomNicholas added the plan to merge Final call for comments label Apr 26, 2024
@dcherian
Copy link
Contributor

[also good opportunity to extend our stateful tests in a future PR if you want to dip your toes there ;) ]

Co-authored-by: Deepak Cherian <[email protected]>
def test_expand_dims_create_index_data_variable(self, create_index_flag):
# data variables should not gain an index ever
ds = Dataset({"x": 0})
expanded = ds.expand_dims("x", create_index=create_index_flag)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this raise a warning for create_index=True since that option is effectively ignored.

Copy link
Member Author

@TomNicholas TomNicholas Apr 26, 2024

Choose a reason for hiding this comment

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

Why would that raise a warning? You think if a kwarg is irrelevant when passed it should raise a warning?

I also feel like that could create unnecessary warnings, especially if we made a global create_index flag later like was discussed in #8872 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think that if a user explicitly writes ds.expand_dims("x", create_index=True) and we don't create an index, we should either warn or error.

In this case, it seems OK to warn and ask the user to call set_coords("x") first. We want to encourage that behaviour anyway.

I have not thought about the global option yet, but usually we handle that by setting None as the default value and then harmonizing with the global setting.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also OK punting that to a future PR. The current behaviour is pretty broken already.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a UserWarning in 7e8f895.

@TomNicholas TomNicholas enabled auto-merge (squash) April 27, 2024 16:21
@TomNicholas TomNicholas merged commit 214d941 into pydata:main Apr 27, 2024
31 checks passed
@TomNicholas TomNicholas deleted the expand_dims_create_1d_index branch April 27, 2024 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to merge Final call for comments topic-indexing
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants