# Buggy code
def wrapped_func(self, **kwargs):
    from .dataarray import DataArray

    # bottleneck doesn't allow min_count to be 0, although it should
    # work the same as if min_count = 1
    if self.min_periods is not None and self.min_periods == 0:
        min_count = 1
    else:
        min_count = self.min_periods

    axis = self.obj.get_axis_num(self.dim)

    padded = self.obj.variable
    if self.center:
        shift = (-self.window // 2) + 1

        if (
            LooseVersion(np.__version__) < LooseVersion("1.13")
            and self.obj.dtype.kind == "b"
        ):
            # with numpy < 1.13 bottleneck cannot handle np.nan-Boolean
            # mixed array correctly. We cast boolean array to float.
            padded = padded.astype(float)
        padded = padded.pad_with_fill_value(**{self.dim: (0, -shift)})
        valid = (slice(None),) * axis + (slice(-shift, None),)

    if isinstance(padded.data, dask_array_type):
        values = dask_rolling_wrapper(
            func, self.obj.data, window=self.window, min_count=min_count, axis=axis
        )
    else:
        values = func(padded.data, window=self.window, min_count=min_count, axis=axis)

    if self.center:
        values = values[valid]
    result = DataArray(values, self.obj.coords)

    return result


# Traceback (ValueError)
ValueError                                Traceback (most recent call last)
<ipython-input-57-6acf382cdd3d> in <module>()
4 da_day_clim = da.groupby('S.dayofyear').mean('S')
5 da_day_clim2 = da_day_clim.chunk({'dayofyear': 366})
----> 6 da_day_clim_smooth = da_day_clim2.rolling(dayofyear=31, center=True).mean()

~/anaconda/envs/SubXNAO/lib/python3.6/site-packages/xarray/core/rolling.py in wrapped_func(self, **kwargs)
307             if self.center:
308                 values = values[valid]
--> 309             result = DataArray(values, self.obj.coords)
310
311             return result

~/anaconda/envs/SubXNAO/lib/python3.6/site-packages/xarray/core/dataarray.py in __init__(self, data, coords, dims, name, attrs, encoding, fastpath)
224
225             data = as_compatible_data(data)
--> 226             coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
227             variable = Variable(dims, data, attrs, encoding, fastpath=True)
228

~/anaconda/envs/SubXNAO/lib/python3.6/site-packages/xarray/core/dataarray.py in _infer_coords_and_dims(shape, coords, dims)
79                 raise ValueError('conflicting sizes for dimension %r: '
80                                  'length %s on the data but length %s on '
---> 81                                  'coordinate %r' % (d, sizes[d], s, k))
82
83         if k in sizes and v.shape != (sizes[k],):

ValueError: conflicting sizes for dimension 'dayofyear': length 351 on the data but length 366 on coordinate 'dayofyear'

# Fixed code
def wrapped_func(self, **kwargs):
    from .dataarray import DataArray

    # bottleneck doesn't allow min_count to be 0, although it should
    # work the same as if min_count = 1
    if self.min_periods is not None and self.min_periods == 0:
        min_count = 1
    else:
        min_count = self.min_periods

    axis = self.obj.get_axis_num(self.dim)

    padded = self.obj.variable
    if self.center:
        if (
            LooseVersion(np.__version__) < LooseVersion("1.13")
            and self.obj.dtype.kind == "b"
        ):
            # with numpy < 1.13 bottleneck cannot handle np.nan-Boolean
            # mixed array correctly. We cast boolean array to float.
            padded = padded.astype(float)

        if isinstance(padded.data, dask_array_type):
            # Workaround to make the padded chunk size is larger than
            # self.window-1
            shift = -(self.window - 1)
            offset = -shift - self.window // 2
            valid = (slice(None),) * axis + (
                slice(offset, offset + self.obj.shape[axis]),
            )
        else:
            shift = (-self.window // 2) + 1
            valid = (slice(None),) * axis + (slice(-shift, None),)
        padded = padded.pad_with_fill_value(**{self.dim: (0, -shift)})

    if isinstance(padded.data, dask_array_type):
        values = dask_rolling_wrapper(
            func, padded, window=self.window, min_count=min_count, axis=axis
        )
    else:
        values = func(padded.data, window=self.window, min_count=min_count, axis=axis)

    if self.center:
        values = values[valid]
    result = DataArray(values, self.obj.coords)

    return result