Skip to content

Dataset global attributes dropped when performing operations against numpy data type #3490

Closed
@rafa-guedes

Description

@rafa-guedes

Operations against numpy data types seem cause global attributes in dataset to be dropped, example below. I also noticed in a real dataset with multiple dimensions that the order of dset.coords was swapped.

In [1]: import numpy as np                                                                                                                                               

In [2]: import pandas as pd                                                                                                                                              

In [3]: import xarray as xr                                                                                                                                              

In [4]: dset = xr.DataArray( 
   ...:     np.random.rand(4, 3), 
   ...:     [("time", pd.date_range("2000-01-01", periods=4)), ("space", ["IA", "IL", "IN"])], 
   ...:     name="test", 
   ...: ).to_dataset() 
   ...: dset.attrs = {"attr1": "val1", "attr2": "val2"}                                                                                                                  

In [5]: 1.0 * dset                                                                                                                                                       
Out[5]: 
<xarray.Dataset>
Dimensions:  (space: 3, time: 4)
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  * space    (space) <U2 'IA' 'IL' 'IN'
Data variables:
    test     (time, space) float64 0.3114 0.8757 0.4467 ... 0.2784 0.8502 0.581
Attributes:
    attr1:    val1
    attr2:    val2

In [6]: np.float64(1.0) * dset                                                                                                                                           
Out[6]: 
<xarray.Dataset>
Dimensions:  (space: 3, time: 4)
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  * space    (space) <U2 'IA' 'IL' 'IN'
Data variables:
    test     (time, space) float64 0.3114 0.8757 0.4467 ... 0.2784 0.8502 0.581

In [7]: xr.__version__                                                                                                                                                  
Out[7]: '0.14.0'

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-metadataRelating to the handling of metadata (i.e. attrs and encoding)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions