From cc5015aabe0b08cdf05610ead9e7bfcbeee807c8 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Fri, 25 Jan 2019 11:55:07 -0800 Subject: [PATCH] BUG: ensure indexes are reset when coords are modified (#2707) This was introduced by the recent indexes refactor, but never made it into a release. --- xarray/core/coordinates.py | 1 + xarray/tests/test_cftimeindex.py | 2 +- xarray/tests/test_dataarray.py | 5 +++++ xarray/tests/test_dataset.py | 5 +++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/xarray/core/coordinates.py b/xarray/core/coordinates.py index 7c62d5fd7b5..19e2d009e44 100644 --- a/xarray/core/coordinates.py +++ b/xarray/core/coordinates.py @@ -234,6 +234,7 @@ def _update_coords(self, coords): raise ValueError('cannot add coordinates with new dimensions to ' 'a DataArray') self._data._coords = coords + self._data._indexes = None @property def variables(self): diff --git a/xarray/tests/test_cftimeindex.py b/xarray/tests/test_cftimeindex.py index 09c598272e3..0d6ba6b47c9 100644 --- a/xarray/tests/test_cftimeindex.py +++ b/xarray/tests/test_cftimeindex.py @@ -8,7 +8,7 @@ from xarray.coding.cftimeindex import ( CFTimeIndex, _parse_array_of_cftime_strings, _parse_iso8601_with_reso, _parsed_string_to_bounds, assert_all_valid_date_type, parse_iso8601) -from xarray.tests import assert_array_equal, assert_identical +from xarray.tests import assert_array_equal, assert_allclose, assert_identical from . import ( has_cftime, has_cftime_1_0_2_1, has_cftime_or_netCDF4, raises_regex, diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 32754788eab..23e15aeff24 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1220,6 +1220,11 @@ def test_coords_alignment(self): dims='x') assert_identical(lhs, expected) + def test_set_coords_update_index(self): + actual = DataArray([1, 2, 3], [('x', [1, 2, 3])]) + actual.coords['x'] = ['a', 'b', 'c'] + assert actual.indexes['x'].equals(pd.Index(['a', 'b', 'c'])) + def test_coords_replacement_alignment(self): # regression test for GH725 arr = DataArray([0, 1, 2], dims=['abc']) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 416ae99bedf..23a77b54356 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -587,6 +587,11 @@ def test_coords_modify(self): expected = data.merge({'c': 11}).set_coords('c') assert_identical(expected, actual) + def test_update_index(self): + actual = Dataset(coords={'x': [1, 2, 3]}) + actual['x'] = ['a', 'b', 'c'] + assert actual.indexes['x'].equals(pd.Index(['a', 'b', 'c'])) + def test_coords_setitem_with_new_dimension(self): actual = Dataset() actual.coords['foo'] = ('x', [1, 2, 3])