Skip to content

Commit 4be2e38

Browse files
committed
Disable setting DataArray.dims inplace
The existing behavior was undocumented and non-obvious -- it's not clear what happens with the associated coordinates (i.e., should it be more like rename or swap_dims?).
1 parent c783c08 commit 4be2e38

File tree

2 files changed

+4
-15
lines changed

2 files changed

+4
-15
lines changed

xray/core/dataarray.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,8 @@ def dims(self):
309309

310310
@dims.setter
311311
def dims(self, value):
312-
with self._set_new_dataset() as ds:
313-
if not len(value) == self.ndim:
314-
raise ValueError('%s dimensions supplied but data has ndim=%s'
315-
% (len(value), self.ndim))
316-
name_map = dict(zip(self.dims, value))
317-
ds.rename(name_map, inplace=True)
318-
if self.name in name_map:
319-
self._name = name_map[self.name]
312+
raise AttributeError('you cannot assign dims on a DataArray. Use '
313+
'.rename() or .swap_dims() instead.')
320314

321315
def _item_key_to_dict(self, key):
322316
if utils.is_dict_like(key):

xray/test/test_dataarray.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,8 @@ def test_dims(self):
6969
arr = self.dv
7070
self.assertEqual(arr.dims, ('x', 'y'))
7171

72-
arr.dims = ('w', 'z')
73-
self.assertEqual(arr.dims, ('w', 'z'))
74-
75-
x = Dataset({'x': ('x', np.arange(5))})['x']
76-
x.dims = ('y',)
77-
self.assertEqual(x.dims, ('y',))
78-
self.assertEqual(x.name, 'y')
72+
with self.assertRaisesRegexp(AttributeError, 'you cannot assign'):
73+
arr.dims = ('w', 'z')
7974

8075
def test_encoding(self):
8176
expected = {'foo': 'bar'}

0 commit comments

Comments
 (0)