Skip to content

Keep attrs & Add a 'keep_coords' argument to Dataset.apply #688

Closed
@max-sixty

Description

@max-sixty

Generally this isn't a problem, since the coords are carried over by the resulting DataArrays:

In [11]:

ds = xray.Dataset({
        'a':pd.DataFrame(pd.np.random.rand(10,3)),
        'b':pd.Series(pd.np.random.rand(10))
    })
ds.coords['c'] = pd.Series(pd.np.random.rand(10))
ds
Out[11]:
<xray.Dataset>
Dimensions:  (dim_0: 10, dim_1: 3)
Coordinates:
  * dim_0    (dim_0) int64 0 1 2 3 4 5 6 7 8 9
  * dim_1    (dim_1) int64 0 1 2
    c        (dim_0) float64 0.9318 0.2899 0.3853 0.6235 0.9436 0.7928 ...
Data variables:
    a        (dim_0, dim_1) float64 0.5707 0.9485 0.3541 0.5987 0.406 0.7992 ...
    b        (dim_0) float64 0.4106 0.2316 0.5804 0.6393 0.5715 0.6463 ...
In [12]:

ds.apply(lambda x: x*2)
Out[12]:
<xray.Dataset>
Dimensions:  (dim_0: 10, dim_1: 3)
Coordinates:
    c        (dim_0) float64 0.9318 0.2899 0.3853 0.6235 0.9436 0.7928 ...
  * dim_0    (dim_0) int64 0 1 2 3 4 5 6 7 8 9
  * dim_1    (dim_1) int64 0 1 2
Data variables:
    a        (dim_0, dim_1) float64 1.141 1.897 0.7081 1.197 0.812 1.598 ...
    b        (dim_0) float64 0.8212 0.4631 1.161 1.279 1.143 1.293 0.3507 ...

But if there's an operation that removes the coords from the DataArrays, the coords are not there on the result (notice c below).
Should the Dataset retain them? Either always or with a keep_coords argument, similar to keep_attrs.

In [13]:

ds = xray.Dataset({
        'a':pd.DataFrame(pd.np.random.rand(10,3)),
        'b':pd.Series(pd.np.random.rand(10))
    })
ds.coords['c'] = pd.Series(pd.np.random.rand(10))
ds
Out[13]:
<xray.Dataset>
Dimensions:  (dim_0: 10, dim_1: 3)
Coordinates:
  * dim_0    (dim_0) int64 0 1 2 3 4 5 6 7 8 9
  * dim_1    (dim_1) int64 0 1 2
    c        (dim_0) float64 0.4121 0.2507 0.6326 0.4031 0.6169 0.441 0.1146 ...
Data variables:
    a        (dim_0, dim_1) float64 0.4813 0.2479 0.5158 0.2787 0.06672 ...
    b        (dim_0) float64 0.2638 0.5788 0.6591 0.7174 0.3645 0.5655 ...
In [14]:

ds.apply(lambda x: x.to_pandas()*2)
Out[14]:
<xray.Dataset>
Dimensions:  (dim_0: 10, dim_1: 3)
Coordinates:
  * dim_0    (dim_0) int64 0 1 2 3 4 5 6 7 8 9
  * dim_1    (dim_1) int64 0 1 2
Data variables:
    a        (dim_0, dim_1) float64 0.9627 0.4957 1.032 0.5574 0.1334 0.8289 ...
    b        (dim_0) float64 0.5275 1.158 1.318 1.435 0.7291 1.131 0.1903 ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions