Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rafa-guedes opened this issue Nov 7, 2019 · 2 comments · Fixed by #4195
Closed
Labels
topic-metadata Relating to the handling of metadata (i.e. attrs and encoding)

Comments

@rafa-guedes
Copy link
Contributor

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'
@max-sixty
Copy link
Collaborator

Confirmed as a bug. Not sure exactly what's going on, it may be on the numpy side. NB this does work (swapping order):

In [4]: dset * np.float64(1.0)
Out[4]:
<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.3565 0.09687 0.4536 ... 0.9714 0.6803
Attributes:
    attr1:    val1
    attr2:    val2

@rafa-guedes
Copy link
Contributor Author

Perhaps reflected operators (i.e., __rmul__) could be defined differently somewhere? I cannot see anything obvious within xarray.

@dcherian dcherian added the topic-metadata Relating to the handling of metadata (i.e. attrs and encoding) label May 26, 2020
dcherian added a commit to dcherian/xarray that referenced this issue Jul 2, 2020
dcherian added a commit to dcherian/xarray that referenced this issue Aug 15, 2020
dcherian added a commit that referenced this issue Oct 14, 2020
* Propagate attrs with unary, binary functions

Closes #3490
Closes #4065
Closes #3433
Closes #3595

* Un xfail test

* bugfix

* Some progress. Still need keep_attrs in DataArray._unary_op

* Fix dataset attrs

* whats-new

* small fix

* Fix imag, real

* fix variable tests

* fix multiple return variables.

* review comments

* Update doc/whats-new.rst

* Propagate attrs with DataArray unary ops

* More tests

* Small cleanup

* Review comments.

* Fix duplication

Co-authored-by: Maximilian Roos <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-metadata Relating to the handling of metadata (i.e. attrs and encoding)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants