MCPcopy Index your code
hub / github.com/pydata/xarray / test_map_keep_attrs

Method test_map_keep_attrs

xarray/tests/test_datatree.py:1127–1148  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1125 weighted_mean(dt.dataset)
1126
1127 def test_map_keep_attrs(self) -> None:
1128 # test DatasetView.map(..., keep_attrs=...)
1129 data = xr.DataArray([1, 2, 3], dims="x", attrs={"da": "attrs"})
1130 ds = xr.Dataset({"data": data}, attrs={"ds": "attrs"})
1131 dt = DataTree(ds)
1132
1133 def func_keep(ds):
1134 # x.mean() removes the attrs of the data_vars
1135 return ds.map(lambda x: x.mean(), keep_attrs=True)
1136
1137 result = xr.map_over_datasets(func_keep, dt)
1138 expected = dt.mean(keep_attrs=True)
1139 xr.testing.assert_identical(result, expected)
1140
1141 # DatasetView.map keeps attrs by default
1142 def func(ds):
1143 # ds.map and x.mean() both keep attrs by default
1144 return ds.map(lambda x: x.mean())
1145
1146 result = xr.map_over_datasets(func, dt)
1147 expected = dt.mean()
1148 xr.testing.assert_identical(result, expected)
1149
1150
1151class TestAccess:

Callers

nothing calls this directly

Calls 3

DataTreeClass · 0.90
map_over_datasetsMethod · 0.80
meanMethod · 0.45

Tested by

no test coverage detected