Repository navigation
Open
Labels
Description
The following used to work with xarray 0.9.5 but doesn't anymore with 0.9.6 or master:
import xarray as xr
import pandas as pd
import numpy as np
time = pd.period_range('2000-01', '2000-12', freq='M')
da = xr.DataArray(np.arange(12), dims=['time'], coords={'time':time})
da['time.month']---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/.pyvirtualenvs/py3/lib/python3.5/site-packages/xarray/core/dataarray.py in _getitem_coord(self, key)
458 try:
--> 459 var = self._coords[key]
460 except KeyError:
KeyError: 'time.month'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-1-41829b924596> in <module>()
4 time = pd.period_range('2000-01', '2000-12', freq='M')
5 da = xr.DataArray(np.arange(12), dims=['time'], coords={'time':time})
----> 6 da['time.month']
~/.pyvirtualenvs/py3/lib/python3.5/site-packages/xarray/core/dataarray.py in __getitem__(self, key)
467 def __getitem__(self, key):
468 if isinstance(key, basestring):
--> 469 return self._getitem_coord(key)
470 else:
471 # orthogonal array indexing
~/.pyvirtualenvs/py3/lib/python3.5/site-packages/xarray/core/dataarray.py in _getitem_coord(self, key)
461 dim_sizes = dict(zip(self.dims, self.shape))
462 _, key, var = _get_virtual_variable(
--> 463 self._coords, key, self._level_coords, dim_sizes)
464
465 return self._replace_maybe_drop_dims(var, name=key)
~/.pyvirtualenvs/py3/lib/python3.5/site-packages/xarray/core/dataset.py in _get_virtual_variable(variables, key, level_vars, dim_sizes)
82 data = getattr(ref_var.dt, var_name).data
83 else:
---> 84 data = getattr(ref_var, var_name).data
85 virtual_var = Variable(ref_var.dims, data)
86
AttributeError: 'IndexVariable' object has no attribute 'month'
Activity
shoyer commentedon Sep 11, 2017
I never intended to support time attributes on PeriodIndex objects so there was never any test coverage here, but I guess it has been working! Nonetheless this should be pretty easy to fix.
fmaussion commentedon Sep 11, 2017
I was wondering which PR was responsible for the regression, but I guess I don't understand the internals well enough to classify this as "easy fix" for me ;)
shoyer commentedon Sep 11, 2017
Looks like #1356, which introduced a
is_datatime_like()check to the dtype of a variable before adding time components.fmaussion commentedon Sep 11, 2017
Indeed! Will look into it.
fmaussion commentedon Sep 11, 2017
OK, so the problem is that when given to
is_datetime_likehere, thepd.PeriodIndexis already transformed into an array of dtypeobject.Before @darothen 's #1356, the piece if code that was executed is
date = ref_var.to_index(), which has the nice property to return aPeriodIndex, as shown here from my debugger:shoyer commentedon Sep 11, 2017
It's only an
dtype=objectarray externally: internally, the data is still stored as apandas.PeriodIndex. I think you'll find that insideref_var._data.array.Potentially we could some sort of API for surfacing this information, e.g., a
pandas_dtypeproperty toxarray.Variable.fmaussion commentedon Sep 13, 2017
It's probably cleaner, because the dtype of PeriodIndex can be several things, i.e.
'period[M]'in my case.stale commentedon Aug 14, 2019
In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity
If this issue remains relevant, please comment here or remove the
stalelabel; otherwise it will be marked as closed automaticallyhding1981 commentedon Jul 20, 2021
I have similar problems with time coordinate. How did you solve your problem in the end?
I also list my problem here.
The following are my commands in a jupyter notebook
import xarray as xr
import numpy as np
import pandas as pd
import sys
dset=xr.open_dataset("input/ts_Amon_CNRM-CM6-1_piControl_r1i1p1f2_gr_185001-234912.nc",decode_times=False)
dset['time'] = pd.period_range(start='1850-01-15', end='2349-12-15', freq='M')
varname="ts"
anom = dset[varname].groupby('time.month')-dset[varname].groupby('time.month').mean('time', keep_attrs=True)
Then, I got the following error message.
KeyError Traceback (most recent call last)
~/miniconda3/envs/python_tutorial/lib/python3.9/site-packages/xarray/core/dataarray.py in _getitem_coord(self, key)
692 try:
--> 693 var = self._coords[key]
694 except KeyError:
KeyError: 'time.month'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
in
7 dset['time'] = pd.period_range(start='1850-01-15', end='2349-12-15', freq='M')
8 varname="ts"
----> 9 anom = dset[varname].groupby('time.month')-dset[varname].groupby('time.month').mean('time', keep_attrs=True)
~/miniconda3/envs/python_tutorial/lib/python3.9/site-packages/xarray/core/common.py in groupby(self, group, squeeze, restore_coord_dims)
703 )
704
--> 705 return self._groupby_cls(
706 self, group, squeeze=squeeze, restore_coord_dims=restore_coord_dims
707 )
~/miniconda3/envs/python_tutorial/lib/python3.9/site-packages/xarray/core/groupby.py in init(self, obj, group, squeeze, grouper, bins, restore_coord_dims, cut_kwargs)
313 f"Received {group!r} instead."
314 )
--> 315 group = obj[group]
316 if len(group) == 0:
317 raise ValueError(f"{group.name} must not be empty")
~/miniconda3/envs/python_tutorial/lib/python3.9/site-packages/xarray/core/dataarray.py in getitem(self, key)
702 def getitem(self, key: Any) -> "DataArray":
703 if isinstance(key, str):
--> 704 return self._getitem_coord(key)
705 else:
706 # xarray-style array indexing
~/miniconda3/envs/python_tutorial/lib/python3.9/site-packages/xarray/core/dataarray.py in _getitem_coord(self, key)
694 except KeyError:
695 dim_sizes = dict(zip(self.dims, self.shape))
--> 696 _, key, var = _get_virtual_variable(
697 self._coords, key, self._level_coords, dim_sizes
698 )
~/miniconda3/envs/python_tutorial/lib/python3.9/site-packages/xarray/core/dataset.py in _get_virtual_variable(variables, key, level_vars, dim_sizes)
179 data = getattr(ref_var.dt, var_name).data
180 else:
--> 181 data = getattr(ref_var, var_name).data
182 virtual_var = Variable(ref_var.dims, data)
183
AttributeError: 'IndexVariable' object has no attribute 'month'
max-sixty commentedon Jul 20, 2021
@hding1981 IIUC this isn't supported at the moment. Though with the approaching index refactor, we may be able to support this more easily
hding1981 commentedon Jul 20, 2021
I am calculating monthly mean anomalies from a CMIP6 file, which is attached and ended by .docx (Please remove .docx from its name after downloading it).
This file has reasonable time coordinate, which has been confirmed by ncdump and cdo. But it shows NaT after a time point by reading the file using xr.open_dataset. I printed out outputs of processing this file using some commands in a jyputer notebook in a pdf file, which is also attached. I really have no idea why xr.open_datase cannot read the time coordinate properly.
Then, I thought maybe I can redefine its time axis by pd.date_range. But it is also not working.
Thank you so much!
Untitled5 - Jupyter Notebook.pdf
ts_Amon_CNRM-CM6-1_piControl_r1i1p1f2_gr_185001-234912.tmp.nc.docx
max-sixty commentedon Jul 20, 2021
@hding1981 you would need to make a MCVE like this: https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports in a new issue. It's unlikely someone can help you debug your data.
DresdenGman commentedon Nov 24, 2026
I re-checked this on xarray 2026.9.0: with a
period_range('2000-01', '2000-12', freq='M')time coordinate, bothda['time.month']andda.time.dt.monthraiseTypeError: Cannot interpret 'period[M]' as a data type, while the same calls on adate_rangecoordinate return[1..12]. The original 2017AttributeErroris gone, but it's still broken.Is time-component access on
PeriodIndexcoordinates still something you want to support, or is it intentionally unsupported and better handled as a docs note? Happy to wait for confirmation before doing anything.mvanhorn commentedon Nov 25, 2026
The accessor and dotted coordinate lookup passed the pandas period dtype to NumPy's subtype checks. NumPy cannot interpret that dtype, so lookup failed before pandas could extract the requested field.
Both lookup paths now recognize
PeriodDtype, and the NumPy subtype checks skip pandas extension dtypes. Field extraction and formatting preserve the period dtype when constructing a pandas Series from aPeriodIndexorPeriodArray.#11943