MCPcopy
hub / github.com/pydata/xarray / test_where_attrs

Function test_where_attrs

xarray/tests/test_computation.py:2201–2285  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2199
2200
2201def test_where_attrs() -> None:
2202 cond = xr.DataArray([True, False], coords={"a": [0, 1]}, attrs={"attr": "cond_da"})
2203 cond["a"].attrs = {"attr": "cond_coord"}
2204 input_cond = cond.copy()
2205 x = xr.DataArray([1, 1], coords={"a": [0, 1]}, attrs={"attr": "x_da"})
2206 x["a"].attrs = {"attr": "x_coord"}
2207 y = xr.DataArray([0, 0], coords={"a": [0, 1]}, attrs={"attr": "y_da"})
2208 y["a"].attrs = {"attr": "y_coord"}
2209
2210 # 3 DataArrays, takes attrs from x
2211 actual = xr.where(cond, x, y, keep_attrs=True)
2212 expected = xr.DataArray([1, 0], coords={"a": [0, 1]}, attrs={"attr": "x_da"})
2213 expected["a"].attrs = {"attr": "x_coord"}
2214 assert_identical(expected, actual)
2215 # Check also that input coordinate attributes weren't modified by reference
2216 assert x["a"].attrs == {"attr": "x_coord"}
2217 assert y["a"].attrs == {"attr": "y_coord"}
2218 assert cond["a"].attrs == {"attr": "cond_coord"}
2219 assert_identical(cond, input_cond)
2220
2221 # 3 DataArrays, drop attrs
2222 actual = xr.where(cond, x, y, keep_attrs=False)
2223 expected = xr.DataArray([1, 0], coords={"a": [0, 1]})
2224 assert_identical(expected, actual)
2225 assert_identical(expected.coords["a"], actual.coords["a"])
2226 # Check also that input coordinate attributes weren't modified by reference
2227 assert x["a"].attrs == {"attr": "x_coord"}
2228 assert y["a"].attrs == {"attr": "y_coord"}
2229 assert cond["a"].attrs == {"attr": "cond_coord"}
2230 assert_identical(cond, input_cond)
2231
2232 # x as a scalar, takes no attrs
2233 actual = xr.where(cond, 0, y, keep_attrs=True)
2234 expected = xr.DataArray([0, 0], coords={"a": [0, 1]})
2235 assert_identical(expected, actual)
2236
2237 # y as a scalar, takes attrs from x
2238 actual = xr.where(cond, x, 0, keep_attrs=True)
2239 expected = xr.DataArray([1, 0], coords={"a": [0, 1]}, attrs={"attr": "x_da"})
2240 expected["a"].attrs = {"attr": "x_coord"}
2241 assert_identical(expected, actual)
2242
2243 # x and y as a scalar, takes no attrs
2244 actual = xr.where(cond, 1, 0, keep_attrs=True)
2245 expected = xr.DataArray([1, 0], coords={"a": [0, 1]})
2246 assert_identical(expected, actual)
2247
2248 # x and y as a scalar, takes no attrs
2249 actual = xr.where(cond, 1, 0, keep_attrs=False)
2250 expected = xr.DataArray([1, 0], coords={"a": [0, 1]})
2251 assert_identical(expected, actual)
2252
2253 # cond and y as a scalar, takes attrs from x
2254 actual = xr.where(True, x, y, keep_attrs=True)
2255 expected = xr.DataArray([1, 1], coords={"a": [0, 1]}, attrs={"attr": "x_da"})
2256 expected["a"].attrs = {"attr": "x_coord"}
2257 assert_identical(expected, actual)
2258

Callers

nothing calls this directly

Calls 4

copyMethod · 0.95
renameMethod · 0.95
assert_identicalFunction · 0.70
whereMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…