Skip to content

Commit cc5015a

Browse files
authored
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.
1 parent aabda43 commit cc5015a

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

xarray/core/coordinates.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def _update_coords(self, coords):
234234
raise ValueError('cannot add coordinates with new dimensions to '
235235
'a DataArray')
236236
self._data._coords = coords
237+
self._data._indexes = None
237238

238239
@property
239240
def variables(self):

xarray/tests/test_cftimeindex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from xarray.coding.cftimeindex import (
99
CFTimeIndex, _parse_array_of_cftime_strings, _parse_iso8601_with_reso,
1010
_parsed_string_to_bounds, assert_all_valid_date_type, parse_iso8601)
11-
from xarray.tests import assert_array_equal, assert_identical
11+
from xarray.tests import assert_array_equal, assert_allclose, assert_identical
1212

1313
from . import (
1414
has_cftime, has_cftime_1_0_2_1, has_cftime_or_netCDF4, raises_regex,

xarray/tests/test_dataarray.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,11 @@ def test_coords_alignment(self):
12201220
dims='x')
12211221
assert_identical(lhs, expected)
12221222

1223+
def test_set_coords_update_index(self):
1224+
actual = DataArray([1, 2, 3], [('x', [1, 2, 3])])
1225+
actual.coords['x'] = ['a', 'b', 'c']
1226+
assert actual.indexes['x'].equals(pd.Index(['a', 'b', 'c']))
1227+
12231228
def test_coords_replacement_alignment(self):
12241229
# regression test for GH725
12251230
arr = DataArray([0, 1, 2], dims=['abc'])

xarray/tests/test_dataset.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,11 @@ def test_coords_modify(self):
587587
expected = data.merge({'c': 11}).set_coords('c')
588588
assert_identical(expected, actual)
589589

590+
def test_update_index(self):
591+
actual = Dataset(coords={'x': [1, 2, 3]})
592+
actual['x'] = ['a', 'b', 'c']
593+
assert actual.indexes['x'].equals(pd.Index(['a', 'b', 'c']))
594+
590595
def test_coords_setitem_with_new_dimension(self):
591596
actual = Dataset()
592597
actual.coords['foo'] = ('x', [1, 2, 3])

0 commit comments

Comments
 (0)