html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue https://github.com/pydata/xarray/pull/3126#issuecomment-512031393,https://api.github.com/repos/pydata/xarray/issues/3126,512031393,MDEyOklzc3VlQ29tbWVudDUxMjAzMTM5Mw==,2818208,2019-07-16T22:50:04Z,2019-07-16T22:50:04Z,CONTRIBUTOR,"Okay, a couple changes to the original PR: we now perform a shallow copy of the returned index in `DataWithCoords.get_index` and `Indexes.__getitem__`. The latter is needed because both `Dataset.indexes` and `DataArray.indexes` returns an `Indexes` object. Finally, I added [a bunch of tests for various index accessors](https://github.com/pydata/xarray/pull/3126/files#diff-02d824ae7c673c0c4bf2f30dff024753R3055). This seems pretty robust: ```python >>> import pandas as pd >>> import xarray as xr >>> index = pd.Index(list('abcd'), name='foo') >>> series = pd.Series(range(4), index) >>> array = xr.DataArray.from_series(series) >>> array.get_index('foo').name = 'bar' >>> array.get_index('foo').name 'foo' >>> array['foo'].to_index().name = 'bar' >>> array['foo'].to_index().name 'foo' >>> array.to_index().name = 'bar' >>> array.to_index().name 'foo' >>> array.indexes['foo'].name = 'bar' >>> array.indexes['foo'].name 'foo' ``` Since a `Dataset` is just a collection of `DataArray` objects, the behavior propagates nicely, e.g. ```python >>> dataset = xr.Dataset({'myvar': array}) >>> dataset['myvar'].to_index().name = 'bar' >>> dataset['myvar'].to_index().name 'foo' >>> dataset.indexes['foo'].name = 'bar' >>> dataset.indexes['foo'].name 'foo' ```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,467856527 https://github.com/pydata/xarray/pull/3126#issuecomment-511716413,https://api.github.com/repos/pydata/xarray/issues/3126,511716413,MDEyOklzc3VlQ29tbWVudDUxMTcxNjQxMw==,2818208,2019-07-16T08:20:34Z,2019-07-16T08:21:07Z,CONTRIBUTOR,"Locally, I've moved my changes from `DataArray.to_series` to `DataWithCoords.get_index`, and my tests still pass. After work, I'll write some more tests, e.g. for for `DataArray.to_index`, and push. @shoyer, it seems like this issue happens anywhere the user can modify a Pandas index. For example, the Dataset bug in [my comment yesterday](https://github.com/pydata/xarray/pull/3126#issuecomment-511506569). Do we want all views of indexes to be immutable or do we trust the user to not do stuff like this: ```python >>> dataset.indexes['mutable?'].name = 'yes' >>> dataset.indexes['mutable?'].name 'yes' ```","{""total_count"": 1, ""+1"": 1, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,467856527 https://github.com/pydata/xarray/pull/3126#issuecomment-511506569,https://api.github.com/repos/pydata/xarray/issues/3126,511506569,MDEyOklzc3VlQ29tbWVudDUxMTUwNjU2OQ==,2818208,2019-07-15T17:59:23Z,2019-07-15T17:59:23Z,CONTRIBUTOR,"@max-sixty, `name` is just a mutable property on a Pandas index: ```python >>> dates = pd.date_range('01-Jan-2019', '31-Jan-2019', name='mutable?') >>> series = pd.Series(np.random.randn(dates.size), dates) >>> series.index.name 'mutable?' >>> series.index.name = 'yes' >>> series.index.name 'yes' ``` But you're right that `to_series` is the wrong place. It looks like `to_series` calls `to_index`, which calls [`get_index`](https://github.com/pydata/xarray/blob/master/xarray/core/common.py#L288), which is inherited by other classes such as `Dataset`. For example, the bug persists with `Dataset` despite my fix: ```python >>> dates = pd.date_range('01-Jan-2019', '31-Jan-2019', name='mutable?') >>> series = pd.Series(np.random.randn(dates.size), dates) >>> dataset = xr.Dataset({'foo': series}) >>> dataset.indexes['mutable?'].name = 'yes' >>> dataset.indexes['mutable?'].name 'yes' ``` Does anyone know if `get_index` is the best place for this? Maybe the tests should be somewhere more generic as well.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,467856527