Skip to content

Conversation

@jsignell
Copy link
Contributor

@jsignell jsignell commented Dec 8, 2025

Based on how keep_attrs=False behaves in other methods, I think it should drop all attrs.

Demo

import xarray as xr
import numpy as np

rng = np.random.default_rng(1)
arr = xr.DataArray(
    rng.integers(0, 3, (3, 3)), coords={"a": np.arange(3), "b": np.arange(3)}
)
arr["a"].attrs = {"foo": "bar", "baz": 1}
ds = xr.Dataset({"arr": arr})

print(xr.where(ds > 1, 1.0, 0.0, keep_attrs=True)["a"].attrs)    # {}
print(xr.where(ds > 1, 1.0, 0.0, keep_attrs=False)["a"].attrs)   # {}

ones = xr.ones_like(ds)
print(xr.where(ds > 1, ones, 0.0, keep_attrs=True)["a"].attrs)   # {"foo": "bar", "baz": 1}
print(xr.where(ds > 1, ones, 0.0, keep_attrs=False)["a"].attrs)  # {}

@jsignell jsignell changed the title Where scalar Change behavior of keep_attrs in xr.where when x is a scalar Dec 8, 2025
@jsignell jsignell requested a review from keewis December 8, 2025 19:24
Copy link
Collaborator

@keewis keewis left a comment

Choose a reason for hiding this comment

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

looks good to me now (and the trick with _replace is interesting, I didn't know that existed). You might want to add a entry to whats-new.rst under "bug fixes", though.

@jsignell
Copy link
Contributor Author

jsignell commented Dec 9, 2025

looks good to me now (and the trick with _replace is interesting, I didn't know that existed). You might want to add a entry to whats-new.rst under "bug fixes", though.

Yeah I will add. I am also updating the where docs to add more info on keep_attrs options

@jsignell
Copy link
Contributor Author

jsignell commented Dec 9, 2025

Thanks for the reviews @keewis! I will merge this when it passes unless you have more feedback.

Copy link
Collaborator

@keewis keewis left a comment

Choose a reason for hiding this comment

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

some comments on the docstring changes

keep_attrs : bool or str or callable, optional
How to treat attrs. If True, keep the attrs of `x`.
keep_attrs : bool or {"drop", "identical", "no_conflicts", "drop_conflicts", "override"} or callable, optional
- 'drop' or False (default): empty attrs on returned xarray object.
Copy link
Collaborator

Choose a reason for hiding this comment

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

False is not the default, we pull the default from OPTIONS["keep_attrs"] (which we might have changed to True by now?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops! Maybe I should move True up to be first in the list as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case True is the default when OPTIONS["keep_attrs"] in (True, "default")

Coordinates:
* lat (lat) int64 80B 0 1 2 3 4 5 6 7 8 9
Preserve the attrs on `cond` by using `keep_attrs="drop_conflicts"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

that's a side-effect of there being no attributes on scalars (so it may be worth pointing that out). If we want to do that exactly for all cases, we'd have to use a lambda / a function: keep_attrs=lambda attrs: attrs[0] if attrs else {}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ymm yeah. I tried to make it clearer that this is specifically for the case where x is a scalar.

Copy link
Collaborator

@keewis keewis left a comment

Choose a reason for hiding this comment

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

thanks, this looks good to me now!

@jsignell jsignell merged commit 5ea3abb into pydata:main Dec 9, 2025
40 checks passed
@jsignell jsignell deleted the where-scalar branch December 9, 2025 16:28
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.

Inconsistent behavior of xarray.where for coordinate attributes

2 participants