Skip to content

Commit b6409f0

Browse files
authored
map_blocks: allow user function to add new unindexed dimension. (#3817)
1 parent 5354679 commit b6409f0

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ New Features
4343
arguments and should instead pass a single list of dimensions.
4444
(:pull:`3802`)
4545
By `Maximilian Roos <https://github.com/max-sixty>`_
46+
- :py:func:`map_blocks` can now apply functions that add new unindexed dimensions.
47+
By `Deepak Cherian <https://github.com/dcherian>`_
4648
- The new ``Dataset._repr_html_`` and ``DataArray._repr_html_`` (introduced
4749
in 0.14.1) is now on by default. To disable, use
4850
``xarray.set_options(display_style="text")``.
@@ -60,7 +62,6 @@ New Features
6062
(:issue:`3843`, :pull:`3844`)
6163
By `Aaron Spring <https://github.com/aaronspring>`_.
6264

63-
6465
Bug fixes
6566
~~~~~~~~~
6667
- Fix :py:meth:`Dataset.interp` when indexing array shares coordinates with the

xarray/core/parallel.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ def _wrapper(func, obj, to_array, args, kwargs):
386386
var_chunks.append(input_chunks[dim])
387387
elif dim in indexes:
388388
var_chunks.append((len(indexes[dim]),))
389+
elif dim in template.dims:
390+
# new unindexed dimension
391+
var_chunks.append((template.sizes[dim],))
389392

390393
data = dask.array.Array(
391394
hlg, name=gname_l, chunks=var_chunks, dtype=template[name].dtype

xarray/tests/test_dask.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,7 @@ def test_map_blocks_to_array(map_ds):
11471147
lambda x: x.to_dataset(),
11481148
lambda x: x.drop_vars("x"),
11491149
lambda x: x.expand_dims(k=[1, 2, 3]),
1150+
lambda x: x.expand_dims(k=3),
11501151
lambda x: x.assign_coords(new_coord=("y", x.y * 2)),
11511152
lambda x: x.astype(np.int32),
11521153
# TODO: [lambda x: x.isel(x=1).drop_vars("x"), map_da],
@@ -1167,6 +1168,7 @@ def test_map_blocks_da_transformations(func, map_da):
11671168
lambda x: x.drop_vars("a"),
11681169
lambda x: x.drop_vars("x"),
11691170
lambda x: x.expand_dims(k=[1, 2, 3]),
1171+
lambda x: x.expand_dims(k=3),
11701172
lambda x: x.rename({"a": "new1", "b": "new2"}),
11711173
# TODO: [lambda x: x.isel(x=1)],
11721174
],

0 commit comments

Comments
 (0)