Skip to content

Commit 9eec56c

Browse files
authored
Raise error when assigning to IndexVariable.values & IndexVariable.data (#3862)
* Raise error when assigning IndexVariable.values, IndexVariable.data Fixes #3470 * fix existing tests * Add new test * whats-new * Fix more existing tests * Update doc/whats-new.rst * fix docs * update whats-new
1 parent 2d0b85e commit 9eec56c

File tree

6 files changed

+26
-9
lines changed

6 files changed

+26
-9
lines changed

doc/plotting.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ Additionally, the boolean kwarg ``add_guide`` can be used to prevent the display
657657

658658
.. ipython:: python
659659
660-
ds.w.values = [1, 2, 3, 5]
660+
ds = ds.assign(w=[1, 2, 3, 5])
661661
@savefig ds_discrete_legend_hue_scatter.png
662662
ds.plot.scatter(x='A', y='B', hue='w', hue_style='discrete')
663663

doc/whats-new.rst

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ v0.15.1 (unreleased)
2222
Breaking changes
2323
~~~~~~~~~~~~~~~~
2424

25+
- Raise an error when assigning to the ``.values`` or ``.data`` attribute of
26+
dimension coordinates i.e. ``IndexVariable`` objects. This has been broken since
27+
v0.12.0. Please use :py:meth:`DataArray.assign_coords` or :py:meth:`Dataset.assign_coords`
28+
instead. (:issue:`3470`, :pull:`3862`)
29+
By `Deepak Cherian <https://github.com/dcherian>`_
30+
2531
New Features
2632
~~~~~~~~~~~~
2733

xarray/core/variable.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -2104,9 +2104,17 @@ def load(self):
21042104
# https://github.com/python/mypy/issues/1465
21052105
@Variable.data.setter # type: ignore
21062106
def data(self, data):
2107-
Variable.data.fset(self, data)
2108-
if not isinstance(self._data, PandasIndexAdapter):
2109-
self._data = PandasIndexAdapter(self._data)
2107+
raise ValueError(
2108+
f"Cannot assign to the .data attribute of dimension coordinate a.k.a IndexVariable {self.name!r}. "
2109+
f"Please use DataArray.assign_coords, Dataset.assign_coords or Dataset.assign as appropriate."
2110+
)
2111+
2112+
@Variable.values.setter # type: ignore
2113+
def values(self, values):
2114+
raise ValueError(
2115+
f"Cannot assign to the .values attribute of dimension coordinate a.k.a IndexVariable {self.name!r}. "
2116+
f"Please use DataArray.assign_coords, Dataset.assign_coords or Dataset.assign as appropriate."
2117+
)
21102118

21112119
def chunk(self, chunks=None, name=None, lock=False):
21122120
# Dummy - do not chunk. This method is invoked e.g. by Dataset.chunk()

xarray/tests/test_accessor_dt.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_strftime(self):
8080
def test_not_datetime_type(self):
8181
nontime_data = self.data.copy()
8282
int_data = np.arange(len(self.data.time)).astype("int8")
83-
nontime_data["time"].values = int_data
83+
nontime_data = nontime_data.assign_coords(time=int_data)
8484
with raises_regex(TypeError, "dt"):
8585
nontime_data.time.dt
8686

@@ -213,7 +213,7 @@ def setup(self):
213213
def test_not_datetime_type(self):
214214
nontime_data = self.data.copy()
215215
int_data = np.arange(len(self.data.time)).astype("int8")
216-
nontime_data["time"].values = int_data
216+
nontime_data = nontime_data.assign_coords(time=int_data)
217217
with raises_regex(TypeError, "dt"):
218218
nontime_data.time.dt
219219

xarray/tests/test_dask.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ def test_token_changes_when_data_changes(obj):
12761276
assert t3 != t2
12771277

12781278
# Change IndexVariable
1279-
obj.coords["x"] *= 2
1279+
obj = obj.assign_coords(x=obj.x * 2)
12801280
with raise_if_dask_computes():
12811281
t4 = dask.base.tokenize(obj)
12821282
assert t4 != t3

xarray/tests/test_variable.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -538,15 +538,18 @@ def test_copy_index_with_data(self):
538538
orig = IndexVariable("x", np.arange(5))
539539
new_data = np.arange(5, 10)
540540
actual = orig.copy(data=new_data)
541-
expected = orig.copy()
542-
expected.data = new_data
541+
expected = IndexVariable("x", np.arange(5, 10))
543542
assert_identical(expected, actual)
544543

545544
def test_copy_index_with_data_errors(self):
546545
orig = IndexVariable("x", np.arange(5))
547546
new_data = np.arange(5, 20)
548547
with raises_regex(ValueError, "must match shape of object"):
549548
orig.copy(data=new_data)
549+
with raises_regex(ValueError, "Cannot assign to the .data"):
550+
orig.data = new_data
551+
with raises_regex(ValueError, "Cannot assign to the .values"):
552+
orig.values = new_data
550553

551554
def test_replace(self):
552555
var = Variable(("x", "y"), [[1.5, 2.0], [3.1, 4.3]], {"foo": "bar"})

0 commit comments

Comments
 (0)