Skip to content

Commit

Permalink
ensure rename does not change index type (#3532)
Browse files Browse the repository at this point in the history
* ensure rename does not change index type

* test requires cftime

* test orig.indexes[time].name is conserved

* use index.rename()
  • Loading branch information
mathause authored and max-sixty committed Nov 15, 2019
1 parent aa876cf commit 68b004f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ New Features

Bug fixes
~~~~~~~~~
- Ensure an index of type ``CFTimeIndex`` is not converted to a ``DatetimeIndex`` when
calling :py:meth:`Dataset.rename` (also :py:meth:`Dataset.rename_dims`
and :py:meth:`xr.Dataset.rename_vars`). By `Mathias Hauser <https://github.com/mathause>`_
(:issue:`3522`).
- Fix a bug in `set_index` in case that an existing dimension becomes a level variable of MultiIndex. (:pull:`3520`)
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
- Harmonize `_FillValue`, `missing_value` during encoding and decoding steps. (:pull:`3502`)
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2665,7 +2665,7 @@ def _rename_indexes(self, name_dict, dims_set):
verify_integrity=False,
)
else:
index = pd.Index(v, name=new_name)
index = v.rename(new_name)
indexes[new_name] = index
return indexes

Expand Down
49 changes: 49 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy as np
import pandas as pd
import pytest
from pandas.core.indexes.datetimes import DatetimeIndex

import xarray as xr
from xarray import (
Expand All @@ -22,6 +23,7 @@
open_dataset,
set_options,
)
from xarray.coding.cftimeindex import CFTimeIndex
from xarray.core import dtypes, indexing, utils
from xarray.core.common import duck_array_ops, full_like
from xarray.core.npcompat import IS_NEP18_ACTIVE
Expand Down Expand Up @@ -2458,6 +2460,53 @@ def test_rename_vars(self):
with pytest.raises(ValueError):
original.rename_vars(names_dict_bad)

@requires_cftime
def test_rename_does_not_change_CFTimeIndex_type(self):
# make sure CFTimeIndex is not converted to DatetimeIndex #3522

time = xr.cftime_range(start="2000", periods=6, freq="2MS", calendar="noleap")
orig = Dataset(coords={"time": time})

renamed = orig.rename(time="time_new")
assert "time_new" in renamed.indexes
assert isinstance(renamed.indexes["time_new"], CFTimeIndex)
assert renamed.indexes["time_new"].name == "time_new"

# check original has not changed
assert "time" in orig.indexes
assert isinstance(orig.indexes["time"], CFTimeIndex)
assert orig.indexes["time"].name == "time"

# note: rename_dims(time="time_new") drops "ds.indexes"
renamed = orig.rename_dims()
assert isinstance(renamed.indexes["time"], CFTimeIndex)

renamed = orig.rename_vars()
assert isinstance(renamed.indexes["time"], CFTimeIndex)

def test_rename_does_not_change_DatetimeIndex_type(self):
# make sure DatetimeIndex is conderved on rename

time = pd.date_range(start="2000", periods=6, freq="2MS")
orig = Dataset(coords={"time": time})

renamed = orig.rename(time="time_new")
assert "time_new" in renamed.indexes
assert isinstance(renamed.indexes["time_new"], DatetimeIndex)
assert renamed.indexes["time_new"].name == "time_new"

# check original has not changed
assert "time" in orig.indexes
assert isinstance(orig.indexes["time"], DatetimeIndex)
assert orig.indexes["time"].name == "time"

# note: rename_dims(time="time_new") drops "ds.indexes"
renamed = orig.rename_dims()
assert isinstance(renamed.indexes["time"], DatetimeIndex)

renamed = orig.rename_vars()
assert isinstance(renamed.indexes["time"], DatetimeIndex)

def test_swap_dims(self):
original = Dataset({"x": [1, 2, 3], "y": ("x", list("abc")), "z": 42})
expected = Dataset({"z": 42}, {"x": ("y", [1, 2, 3]), "y": list("abc")})
Expand Down

0 comments on commit 68b004f

Please sign in to comment.