Skip to content

Commit

Permalink
BUG: ensure indexes are reset when coords are modified (#2707)
Browse files Browse the repository at this point in the history
This was introduced by the recent indexes refactor, but never made it into a
release.
  • Loading branch information
shoyer authored Jan 25, 2019
1 parent aabda43 commit cc5015a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions xarray/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
5 changes: 5 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit cc5015a

Please sign in to comment.