Skip to content
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

ensure rename does not change index type #3532

Merged
merged 4 commits into from
Nov 15, 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
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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 @@ -2667,7 +2667,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