html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue
https://github.com/pydata/xarray/issues/1755#issuecomment-348504868,https://api.github.com/repos/pydata/xarray/issues/1755,348504868,MDEyOklzc3VlQ29tbWVudDM0ODUwNDg2OA==,1197350,2017-12-01T14:14:25Z,2017-12-01T14:14:25Z,MEMBER,"Ah right. So you would do
```python
cube = cube.swap_dims({'x':'x2'})
```
","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,278453914
https://github.com/pydata/xarray/issues/1755#issuecomment-348491227,https://api.github.com/repos/pydata/xarray/issues/1755,348491227,MDEyOklzc3VlQ29tbWVudDM0ODQ5MTIyNw==,1197350,2017-12-01T13:12:24Z,2017-12-01T13:12:24Z,MEMBER,"swap_dims is not an in-place operation
http://xarray.pydata.org/en/stable/generated/xarray.Dataset.swap_dims.html
It returns a new dataset with swapped dims. If you want to do it in place, there is a keyword argument for that.
Sent from my iPhone
> On Dec 1, 2017, at 7:37 AM, Leevi Annala wrote:
>
> Problem description
>
> This is a problem if I want to swap_dims in DataArray Accessor.
>
> Code Sample
>
> This is what I'm forced to do:
>
> import xarray as xr
> import numpy as np
> @xr.register_dataarray_accessor('testing')
> class TestAccessor(object):
> def __init__(self, xarray_obj):
> self._obj = xarray_obj
> def the_problem(self):
> self._obj = self._obj.swap_dims({'x':'x2'})
> print(self._obj)
>
> arr = np.random.rand(4,3,2)
> cube = xr.DataArray(arr, dims=['ya', 'x', 'y'], coords={'y':[1,3], 'ya':[1,2,3,6], 'x':[1,2,5]})
> cube.coords['x2'] = ('x', [1,2,3])
> cube.swap_dims({'x':'x2'})
> cube.testing.the_problem()
> print(cube)
> this prints:
>
>
> array([[[ 0.659583, 0.167555],
> [ 0.357974, 0.46081 ],
> [ 0.85115 , 0.845257]],
>
> [[ 0.280308, 0.777399],
> [ 0.512527, 0.542036],
> [ 0.838603, 0.799414]],
>
> [[ 0.572031, 0.350464],
> [ 0.205219, 0.812232],
> [ 0.687778, 0.984928]],
>
> [[ 0.803385, 0.63981 ],
> [ 0.089909, 0.499857],
> [ 0.25266 , 0.967909]]])
> Coordinates:
> * y (y) int32 1 3
> * ya (ya) int32 1 2 3 6
> x (x2) int32 1 2 5
> * x2 (x2) int32 1 2 3
>
> array([[[ 0.659583, 0.167555],
> [ 0.357974, 0.46081 ],
> [ 0.85115 , 0.845257]],
>
> [[ 0.280308, 0.777399],
> [ 0.512527, 0.542036],
> [ 0.838603, 0.799414]],
>
> [[ 0.572031, 0.350464],
> [ 0.205219, 0.812232],
> [ 0.687778, 0.984928]],
>
> [[ 0.803385, 0.63981 ],
> [ 0.089909, 0.499857],
> [ 0.25266 , 0.967909]]])
> Coordinates:
> * y (y) int32 1 3
> * ya (ya) int32 1 2 3 6
> * x (x) int32 1 2 5
> x2 (x) int32 1 2 3
> where the two xarrays are clearly different.
>
> I would want to do:
>
> import xarray as xr
> import numpy as np
> @xr.register_dataarray_accessor('testing')
> class TestAccessor(object):
> def __init__(self, xarray_obj):
> self._obj = xarray_obj
> def the_problem(self):
> self._obj.swap_dims({'x':'x2'}, inplace = True)
> print(self._obj)
>
> arr = np.random.rand(4,3,2)
> cube = xr.DataArray(arr, dims=['ya', 'x', 'y'], coords={'y':[1,3], 'ya':[1,2,3,6], 'x':[1,2,5]})
> cube.coords['x2'] = ('x', [1,2,3])
> cube.swap_dims({'x':'x2'})
> cube.testing.the_problem()
> print(cube)
> this would keep the two xarrays the same, as they should be:
>
>
> array([[[ 0.659583, 0.167555],
> [ 0.357974, 0.46081 ],
> [ 0.85115 , 0.845257]],
>
> [[ 0.280308, 0.777399],
> [ 0.512527, 0.542036],
> [ 0.838603, 0.799414]],
>
> [[ 0.572031, 0.350464],
> [ 0.205219, 0.812232],
> [ 0.687778, 0.984928]],
>
> [[ 0.803385, 0.63981 ],
> [ 0.089909, 0.499857],
> [ 0.25266 , 0.967909]]])
> Coordinates:
> * y (y) int32 1 3
> * ya (ya) int32 1 2 3 6
> x (x2) int32 1 2 5
> * x2 (x2) int32 1 2 3
>
> array([[[ 0.659583, 0.167555],
> [ 0.357974, 0.46081 ],
> [ 0.85115 , 0.845257]],
>
> [[ 0.280308, 0.777399],
> [ 0.512527, 0.542036],
> [ 0.838603, 0.799414]],
>
> [[ 0.572031, 0.350464],
> [ 0.205219, 0.812232],
> [ 0.687778, 0.984928]],
>
> [[ 0.803385, 0.63981 ],
> [ 0.089909, 0.499857],
> [ 0.25266 , 0.967909]]])
> Coordinates:
> * y (y) int32 1 3
> * ya (ya) int32 1 2 3 6
> x (x) int32 1 2 5
> * x2 (x) int32 1 2 3
> I have version 0.10.0, the newest on conda-forge.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub, or mute the thread.
>
","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,278453914