def _iter_over_selections(obj, dim, values):
    """Iterate over selections of an xarray object in the provided order."""
    from xarray.core.groupby import _dummy_copy

    dummy = None
    for value in values:
        try:
            obj_sel = obj.sel(**{dim: value})
        except (KeyError, IndexError):
            if dummy is None:
                dummy = _dummy_copy(obj)
            obj_sel = dummy
        yield obj_sel
Iterate over selections of an xarray object in the provided order.
xarray/computation/apply_ufunc.py