pydata / pydata/xarray

Regression: time attributes on PeriodIndex

Open
#1,565 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

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'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer, then read _getitem_coord in xarray/core/dataarray.py and _get_virtual_variable in xarray/core/dataset.py, where the traceback shows the failure. Verify how PeriodIndex coordinates are handled and confirm that da['time.month'] works again without breaking other virtual coordinate access.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.