Skip to content

changes made to coords using groupby and apply do not persist #1428

Closed
@d-chambers

Description

@d-chambers

I am running Ubuntu 16 with Xarray 0.9.1 on python 3.6.0.

I have found that any changes made to coordinates in a function that is called by a groupby object's apply method do not persist. The following code illustrates the problem:

import numpy as np
import xarray as xr


def change_new_coord(dar):
    """ change the new_coord coord from 1 to 0 """
    dar.coords['new_coord'] = 0
    return dar


# setup data array
data = np.ones((10, 10, 1000))
time = np.linspace(0, 10, 1000)
coords = {'time': time, 'd2': range(10), 'd3': range(10)}
dims = ['d2', 'd3', 'time']
dar = xr.DataArray(data, coords=coords, dims=dims)

# attach coordinate based on d2 and d3
dar.coords['new_coord'] = (('d2', 'd3'), np.ones((10, 10)))

# stack
stacked = dar.stack(z=('d2', 'd3'))

# groupby
gr = stacked.groupby('z')

# apply
out = gr.apply(change_new_coord).unstack('z')

# raises; all values in new_coord should be 0, but they are still 1
assert np.all(out.coords['new_coord'] == 0)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions