id,node_id,number,title,user,state,locked,assignee,milestone,comments,created_at,updated_at,closed_at,author_association,active_lock_reason,draft,pull_request,body,reactions,performed_via_github_app,state_reason,repo,type 573768194,MDExOlB1bGxSZXF1ZXN0MzgyMjE1OTYw,3816,Add template xarray object kwarg to map_blocks,2448579,closed,0,,,9,2020-03-02T07:20:19Z,2020-05-06T16:41:59Z,2020-05-06T16:41:54Z,MEMBER,,0,pydata/xarray/pulls/3816," - [x] Closes #3575 - [x] Tests added - [x] Passes `isort -rc . && black . && mypy . && flake8` - [x] Fully documented, including `whats-new.rst` for all changes and `api.rst` for new API This PR adds a `template` kwarg to `map_blocks` so that we can do more complicated things where automated inference of the template fails. `template` is expected to be an xarray object that looks like the result of the `map_blocks` computation. @jhamman To me, this seems a lot easier than defining a `dict` based schema. With dask variables, the memory cost shouldn't be high. It's easy to use standard xarray operations to make something that looks like the result dataset. Here's a notebook prototyping a `to_schema`/`from_schema` approach: https://gist.github.com/dcherian/130ba22d0fbadb616837deb914eaa67e#file-map_blocks_for_metsim_test-ipynb ```python ds = xr.tutorial.load_dataset(""air_temperature"").chunk({""lat"": 5}) template = ds.isel(lat=slice(1, None, 5)) result = xr.map_blocks(lambda x: x.isel(lat=[1]), ds) # fails result = xr.map_blocks(lambda x: x.isel(lat=[1]), ds, template=template) # yay xr.testing.assert_equal(result, template) # passes ``` Todo: - [x] add tests - [x] what if the function returns a variable not in the template? - [x] should we do anything with `template.attrs`? ","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/3816/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,,13221727,pull