Skip to content

MultiIndex level coordinates as Dataset attributes #1006

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

Merged
merged 1 commit into from
Sep 19, 2016
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
2 changes: 1 addition & 1 deletion xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class AttrAccessMixin(object):
@property
def _attr_sources(self):
"""List of places to look-up items for attribute-style access"""
return [self, self.attrs]
return []

def __getattr__(self, name):
if name != '__setstate__':
Expand Down
6 changes: 3 additions & 3 deletions xarray/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ def __delitem__(self, key):
del self._data._coords[key]


class DataArrayLevelCoordinates(AbstractCoordinates):
"""Dictionary like container for DataArray MultiIndex level coordinates.
class LevelCoordinates(AbstractCoordinates):
"""Dictionary like container for MultiIndex level coordinates.

Used for attribute style lookup. Not returned directly by any
public methods.
Expand All @@ -232,7 +232,7 @@ def _names(self):
def variables(self):
level_coords = OrderedDict(
(k, self._data[v].variable.get_level_variable(k))
for k, v in self._data._level_coords.items())
for k, v in self._data._level_coords.items())
return Frozen(level_coords)


Expand Down
4 changes: 2 additions & 2 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from . import utils
from .alignment import align
from .common import AbstractArray, BaseDataObject, squeeze
from .coordinates import (DataArrayCoordinates, DataArrayLevelCoordinates,
from .coordinates import (DataArrayCoordinates, LevelCoordinates,
Indexes)
from .dataset import Dataset
from .pycompat import iteritems, basestring, OrderedDict, zip
Expand Down Expand Up @@ -467,7 +467,7 @@ def __delitem__(self, key):
@property
def _attr_sources(self):
"""List of places to look-up items for attribute-style access"""
return [self.coords, DataArrayLevelCoordinates(self), self.attrs]
return [self.coords, LevelCoordinates(self), self.attrs]

def __contains__(self, key):
return key in self._coords
Expand Down
7 changes: 6 additions & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from . import formatting
from .. import conventions
from .alignment import align
from .coordinates import DatasetCoordinates, Indexes
from .coordinates import DatasetCoordinates, LevelCoordinates, Indexes
from .common import ImplementsDatasetReduce, BaseDataObject
from .merge import (dataset_update_method, dataset_merge_method,
merge_data_and_coords)
Expand Down Expand Up @@ -487,6 +487,11 @@ def __deepcopy__(self, memo=None):
# copy.deepcopy
return self.copy(deep=True)

@property
def _attr_sources(self):
"""List of places to look-up items for attribute-style access"""
return [self, LevelCoordinates(self), self.attrs]

def __contains__(self, key):
"""The 'in' operator will return true or false depending on whether
'key' is an array in the dataset or not.
Expand Down
3 changes: 3 additions & 0 deletions xarray/test/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,9 @@ def test_virtual_variable_multiindex(self):
name='hour', coords=[mindex], dims='x')
self.assertDataArrayIdentical(expected, data['level_date.hour'])

# attribute style access
self.assertDataArrayIdentical(data.level_str, data['level_str'])

def test_time_season(self):
ds = Dataset({'t': pd.date_range('2000-01-01', periods=12, freq='M')})
expected = ['DJF'] * 2 + ['MAM'] * 3 + ['JJA'] * 3 + ['SON'] * 3 + ['DJF']
Expand Down