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
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

1 participant