Skip to content
Permalink
Browse files

Merge pull request #1219 from ioam/xarr_groupby_drop

Handle bug in gridded groupby when dropping dimensions
  • Loading branch information...
jlstevens committed Mar 21, 2017
2 parents df160aa + b613c68 commit 2961d7db9fa37254bfba97530e76ae7ca36a8648
Showing with 28 additions and 5 deletions.
  1. +2 −2 holoviews/core/data/grid.py
  2. +3 −2 holoviews/core/data/iris.py
  3. +1 −1 holoviews/core/data/xarray.py
  4. +22 −0 tests/testdataset.py
@@ -188,7 +188,7 @@ def groupby(cls, dataset, dim_names, container_type, group_type, **kwargs):
group_kwargs['kdims'] = kdims
group_kwargs.update(kwargs)

drop_dim = len(group_kwargs['kdims']) != len(kdims)
drop_dim = any(d not in group_kwargs['kdims'] for d in kdims)

# Find all the keys along supplied dimensions
keys = [dataset.data[d.name] for d in dimensions]
@@ -206,7 +206,7 @@ def groupby(cls, dataset, dim_names, container_type, group_type, **kwargs):
group_data = {dataset.vdims[0].name: np.atleast_1d(group_data)}
for dim, v in zip(dim_names, unique_key):
group_data[dim] = np.atleast_1d(v)
else:
elif not drop_dim:
for vdim in dataset.vdims:
group_data[vdim.name] = np.squeeze(group_data[vdim.name])
group_data = group_type(group_data, **group_kwargs)
@@ -117,7 +117,8 @@ def init(cls, eltype, data, kdims, vdims):

@classmethod
def validate(cls, dataset):
pass
if len(dataset.vdims) > 1:
raise ValueError("Iris cubes do not support more than one value dimension")


@classmethod
@@ -187,7 +188,7 @@ def groupby(cls, dataset, dims, container_type=HoloMap, group_type=None, **kwarg
group_kwargs['kdims'] = slice_dims
group_kwargs.update(kwargs)

drop_dim = len(group_kwargs['kdims']) != len(slice_dims)
drop_dim = any(d not in group_kwargs['kdims'] for d in slice_dims)

unique_coords = product(*[cls.values(dataset, d, expanded=False)
for d in dims])
@@ -105,7 +105,7 @@ def groupby(cls, dataset, dimensions, container_type, group_type, **kwargs):
kdims=element_dims)
group_kwargs.update(kwargs)

drop_dim = len(group_kwargs['kdims']) != len(element_dims)
drop_dim = any(d not in group_kwargs['kdims'] for d in element_dims)

# XArray 0.7.2 does not support multi-dimensional groupby
# Replace custom implementation when
@@ -877,6 +877,22 @@ def test_dataset_groupby_drop_dims_dynamic(self):
partial = ds.to(Dataset, kdims=['x'], vdims=['Val'], groupby='y', dynamic=True)
self.assertEqual(partial[19]['Val'], array[:, -1, :].T.flatten())

def test_dataset_groupby_drop_dims_with_vdim(self):
array = np.random.rand(3, 20, 10)
ds = Dataset({'x': range(10), 'y': range(20), 'z': range(3), 'Val': array, 'Val2': array*2},
kdims=['x', 'y', 'z'], vdims=['Val', 'Val2'])
with DatatypeContext([self.datatype, 'columns', 'dataframe']):
partial = ds.to(Dataset, kdims=['Val'], vdims=['Val2'], groupby='y')
self.assertEqual(partial.last['Val'], array[:, -1, :].T.flatten())

def test_dataset_groupby_drop_dims_dynamic_with_vdim(self):
array = np.random.rand(3, 20, 10)
ds = Dataset({'x': range(10), 'y': range(20), 'z': range(3), 'Val': array, 'Val2': array*2},
kdims=['x', 'y', 'z'], vdims=['Val', 'Val2'])
with DatatypeContext([self.datatype, 'columns', 'dataframe']):
partial = ds.to(Dataset, kdims=['Val'], vdims=['Val2'], groupby='y', dynamic=True)
self.assertEqual(partial[19]['Val'], array[:, -1, :].T.flatten())


class IrisDatasetTest(GridDatasetTest):
"""
@@ -929,6 +945,12 @@ def test_dataset_sample_hm(self):
def test_dataset_sample_hm_alias(self):
raise SkipTest("Not supported")

def test_dataset_groupby_drop_dims_with_vdim(self):
raise SkipTest("Not supported")

def test_dataset_groupby_drop_dims_dynamic_with_vdim(self):
raise SkipTest("Not supported")


class XArrayDatasetTest(GridDatasetTest):
"""

0 comments on commit 2961d7d

Please sign in to comment.
You can’t perform that action at this time.