Skip to content

BUG: ensure indexes are reset when coords are modified #2707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions xarray/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,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 @@ -10,7 +10,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, requires_cftime)
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 @@ -1222,6 +1222,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 @@ -589,6 +589,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