issues: 278453914
This data as json
id | node_id | number | title | user | state | locked | assignee | milestone | comments | created_at | updated_at | closed_at | author_association | active_lock_reason | draft | pull_request | body | reactions | performed_via_github_app | state_reason | repo | type |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
278453914 | MDU6SXNzdWUyNzg0NTM5MTQ= | 1755 | Is there a reason for why DataArray.swap_dims() cannot be done in place like Dataset.swap_dims? | 25015426 | closed | 0 | 8 | 2017-12-01T12:37:39Z | 2017-12-02T20:09:17Z | 2017-12-02T10:13:39Z | CONTRIBUTOR | Problem descriptionThis is a problem if I want to swap_dims in DataArray Accessor. Code SampleThis is what I'm forced to do: ```python 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.testing.the_problem()
print(cube)
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 <xarray.DataArray (ya: 4, x: 3, y: 2)> array([[[ 0.659583, 0.167555], [ 0.357974, 0.46081 ], [ 0.85115 , 0.845257]],
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: ```python 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.testing.the_problem()
print(cube)
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 <xarray.DataArray (ya: 4, x: 3, y: 2)> array([[[ 0.659583, 0.167555], [ 0.357974, 0.46081 ], [ 0.85115 , 0.845257]],
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 ``` I have version 0.10.0, the newest on conda-forge. |
{ "url": "https://api.github.com/repos/pydata/xarray/issues/1755/reactions", "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
completed | 13221727 | issue |