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

CFTimeIndex changes to normal Index after renaming #3522

Closed
ahuang11 opened this issue Nov 13, 2019 · 3 comments · Fixed by #3532
Closed

CFTimeIndex changes to normal Index after renaming #3522

ahuang11 opened this issue Nov 13, 2019 · 3 comments · Fixed by #3532

Comments

@ahuang11
Copy link
Contributor

MCVE Code Sample

Since this code crashes for me and I don't have xarray master, I can't create a tested MCVE Code sample at the moment, but I think something along the lines of:

import xarray as xr
ds = xr.Dataset(coords={'time': xr.cftime_range(start='2000', periods=6, freq='2MS', calendar='noleap'), 'something': [0, 1, 2, 3]})
print(ds.indexes['time'])
ds = ds.rename({'something': 'something_else'})
print(ds.indexes['time'])

Expected Output

CFTimeIndex([2000-05-01 12:00:00, 2000-05-02 12:00:00,
             ...
             2001-04-01 12:00:00],
            dtype='object', name='time', length=425)

After renaming

Index([2000-05-01 12:00:00, 2000-05-02 12:00:00,
             ...
             2001-04-01 12:00:00],
            dtype='object', name='time', length=425)

Problem Description

CFTimeIndex changes to normal Index after renaming

Output of xr.show_versions()

xarray: 0.14.0
pandas: 0.25.2
numpy: 1.17.2
scipy: 1.3.1
netCDF4: 1.5.3
pydap: None
h5netcdf: 0.7.4
h5py: 2.9.0
Nio: None
zarr: None
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.1.0
cfgrib: None
iris: None
bottleneck: 1.2.1
dask: 2.6.0
distributed: 2.6.0
matplotlib: 3.1.1
cartopy: 0.17.0
seaborn: None
numbagg: None
setuptools: 41.4.0
pip: 19.3.1
conda: None
pytest: 5.2.1
IPython: 7.9.0
sphinx: None

@mathause
Copy link
Collaborator

mathause commented Nov 13, 2019

The culprit may be in ds._rename_indexes:

index = pd.Index(v, name=new_name)

where the index gets piped through pd.Index (even if it is not renamed):

import pandas as pd
pd.Index(ds.indexes['time'])

returns an Index instead of a CFTimeIndex

@keewis
Copy link
Collaborator

keewis commented Nov 13, 2019

also, you can simplify that to

print(
    ds.indexes,
    ds.rename().indexes,
    ds.rename_dims().indexes,
    ds.rename_vars().indexes,
    sep="\n",
)

and the type of the index still gets dropped (on master)

@max-sixty
Copy link
Collaborator

Nice spot @mathause . We should be constructing from the type of v. pd.Index also has a method _shallow_copy IIRC we could call

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants