|
1 | 1 | import numpy as np
|
2 | 2 | import pandas as pd
|
3 | 3 |
|
4 |
| -from .pycompat import basestring, iteritems, suppress, dask_array_type, bytes_type |
| 4 | +from .pycompat import (basestring, iteritems, suppress, dask_array_type, |
| 5 | + OrderedDict) |
5 | 6 | from . import formatting
|
6 |
| -from .utils import SortedKeysDict, not_implemented |
| 7 | +from .utils import SortedKeysDict, not_implemented, Frozen |
7 | 8 |
|
8 | 9 |
|
9 | 10 | class ImplementsArrayReduce(object):
|
@@ -124,6 +125,8 @@ def wrapped_func(self, **kwargs):
|
124 | 125 |
|
125 | 126 |
|
126 | 127 | class AbstractArray(ImplementsArrayReduce, formatting.ReprMixin):
|
| 128 | + """Shared base class for DataArray and Variable.""" |
| 129 | + |
127 | 130 | def __bool__(self):
|
128 | 131 | return bool(self.values)
|
129 | 132 |
|
@@ -186,6 +189,18 @@ def _get_axis_num(self, dim):
|
186 | 189 | raise ValueError("%r not found in array dimensions %r" %
|
187 | 190 | (dim, self.dims))
|
188 | 191 |
|
| 192 | + @property |
| 193 | + def sizes(self): |
| 194 | + """Ordered mapping from dimension names to lengths. |
| 195 | +
|
| 196 | + Immutable. |
| 197 | +
|
| 198 | + See also |
| 199 | + -------- |
| 200 | + Dataset.sizes |
| 201 | + """ |
| 202 | + return Frozen(OrderedDict(zip(self.dims, self.shape))) |
| 203 | + |
189 | 204 |
|
190 | 205 | class AttrAccessMixin(object):
|
191 | 206 | """Mixin class that allows getting keys with attribute access
|
@@ -231,7 +246,43 @@ def __dir__(self):
|
231 | 246 | return sorted(set(dir(type(self)) + extra_attrs))
|
232 | 247 |
|
233 | 248 |
|
234 |
| -class BaseDataObject(AttrAccessMixin): |
| 249 | +class SharedMethodsMixin(object): |
| 250 | + """Shared methods for Dataset, DataArray and Variable.""" |
| 251 | + |
| 252 | + def squeeze(self, dim=None): |
| 253 | + """Return a new object with squeezed data. |
| 254 | +
|
| 255 | + Parameters |
| 256 | + ---------- |
| 257 | + dim : None or str or tuple of str, optional |
| 258 | + Selects a subset of the length one dimensions. If a dimension is |
| 259 | + selected with length greater than one, an error is raised. If |
| 260 | + None, all length one dimensions are squeezed. |
| 261 | +
|
| 262 | + Returns |
| 263 | + ------- |
| 264 | + squeezed : same type as caller |
| 265 | + This object, but with with all or a subset of the dimensions of |
| 266 | + length 1 removed. |
| 267 | +
|
| 268 | + See Also |
| 269 | + -------- |
| 270 | + numpy.squeeze |
| 271 | + """ |
| 272 | + if dim is None: |
| 273 | + dim = [d for d, s in self.sizes.items() if s == 1] |
| 274 | + else: |
| 275 | + if isinstance(dim, basestring): |
| 276 | + dim = [dim] |
| 277 | + if any(self.sizes[k] > 1 for k in dim): |
| 278 | + raise ValueError('cannot select a dimension to squeeze out ' |
| 279 | + 'which has length greater than one') |
| 280 | + return self.isel(**{d: 0 for d in dim}) |
| 281 | + |
| 282 | + |
| 283 | +class BaseDataObject(SharedMethodsMixin, AttrAccessMixin): |
| 284 | + """Shared base class for Dataset and DataArray.""" |
| 285 | + |
235 | 286 | def _calc_assign_results(self, kwargs):
|
236 | 287 | results = SortedKeysDict()
|
237 | 288 | for k, v in kwargs.items():
|
@@ -615,19 +666,6 @@ def __exit__(self, exc_type, exc_value, traceback):
|
615 | 666 | __or__ = __div__ = __eq__ = __ne__ = not_implemented
|
616 | 667 |
|
617 | 668 |
|
618 |
| -def squeeze(xarray_obj, dims, dim=None): |
619 |
| - """Squeeze the dims of an xarray object.""" |
620 |
| - if dim is None: |
621 |
| - dim = [d for d, s in iteritems(dims) if s == 1] |
622 |
| - else: |
623 |
| - if isinstance(dim, basestring): |
624 |
| - dim = [dim] |
625 |
| - if any(dims[k] > 1 for k in dim): |
626 |
| - raise ValueError('cannot select a dimension to squeeze out ' |
627 |
| - 'which has length greater than one') |
628 |
| - return xarray_obj.isel(**dict((d, 0) for d in dim)) |
629 |
| - |
630 |
| - |
631 | 669 | def _maybe_promote(dtype):
|
632 | 670 | """Simpler equivalent of pandas.core.common._maybe_promote"""
|
633 | 671 | # N.B. these casting rules should match pandas
|
|
0 commit comments