Skip to content

Commit 3672599

Browse files
committed
Merge pull request #173 from shoyer/edge-cases
Edge cases
2 parents 74c6b92 + d0c1e95 commit 3672599

File tree

6 files changed

+47
-8
lines changed

6 files changed

+47
-8
lines changed

test/test_data_array.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ def test_properties(self):
5656
with self.assertRaisesRegexp(ValueError, 'must be 1-dimensional'):
5757
self.ds['foo'].as_index
5858

59+
def test_encoding(self):
60+
expected = {'foo': 'bar'}
61+
self.dv.encoding['foo'] = 'bar'
62+
self.assertEquals(expected, self.dv.encoding)
63+
64+
expected = {'baz': 0}
65+
self.dv.encoding = expected
66+
self.assertEquals(expected, self.dv.encoding)
67+
self.assertIsNot(expected, self.dv.encoding)
68+
5969
def test_constructor(self):
6070
data = np.random.random((2, 3))
6171

@@ -93,6 +103,10 @@ def test_constructor(self):
93103
actual = DataArray(data, indexes)
94104
self.assertDataArrayIdentical(expected, actual)
95105

106+
indexes = pd.Series([['a', 'b'], [-1, -2, -3]], ['x', 'y'])
107+
actual = DataArray(data, indexes)
108+
self.assertDataArrayIdentical(expected, actual)
109+
96110
expected = Dataset({None: (['x', 'y'], data),
97111
'x': ('x', ['a', 'b'])})[None]
98112
actual = DataArray(data, {'x': ['a', 'b']}, ['x', 'y'])

test/test_variable.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,14 @@ def test_data(self):
294294
v.values = d2
295295
self.assertIs(source_ndarray(v.values), d2)
296296

297-
def test_item(self):
297+
def test_numpy_same_methods(self):
298298
v = Variable([], np.float32(0.0))
299299
self.assertEqual(v.item(), 0)
300300
self.assertIs(type(v.item()), float)
301301

302+
v = Index('x', np.arange(5))
303+
self.assertEqual(2, v.searchsorted(2))
304+
302305
def test_datetime64_conversion(self):
303306
# verify that datetime64 is always converted to ns precision with
304307
# sources preserved

xray/common.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ def __long__(self):
5757
def __array__(self, dtype=None):
5858
return np.asarray(self.values, dtype=dtype)
5959

60-
def item(self):
61-
"""Calls numpy.ndarray.item on this array's values"""
62-
return self.values.item()
63-
6460
def __repr__(self):
6561
return array_repr(self)
6662

xray/data_array.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _infer_indexes_and_dimensions(shape, indexes, dimensions):
2929

3030
if _is_dict_like(indexes):
3131
if dimensions is None:
32-
dimensions = list(indexes)
32+
dimensions = list(indexes.keys())
3333
else:
3434
bad_indexes = [dim for dim in indexes if dim not in dimensions]
3535
if bad_indexes:
@@ -331,6 +331,10 @@ def encoding(self):
331331
serialized."""
332332
return self.variable.encoding
333333

334+
@encoding.setter
335+
def encoding(self, value):
336+
self.variable.encoding = value
337+
334338
@property
335339
def coordinates(self):
336340
utils.alias_warning('coordinates', 'indexes', 3)

xray/ops.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,24 @@
1212
if not PY3:
1313
NUM_BINARY_OPS.append('div')
1414

15+
# methods which pass on the numpy return value unchanged
16+
# be careful not to list methods that we would want to wrap later
17+
NUMPY_SAME_METHODS = ['item', 'searchsorted']
1518
# methods which don't modify the data shape, so the result should still be
1619
# wrapped in an Variable/DataArray
1720
NUMPY_UNARY_METHODS = ['astype', 'argsort', 'clip', 'conj', 'conjugate',
1821
'round']
1922
# methods which remove an axis
2023
NUMPY_REDUCE_METHODS = ['all', 'any', 'argmax', 'argmin', 'max', 'mean', 'min',
2124
'prod', 'ptp', 'std', 'sum', 'var']
22-
# TODO: wrap cumprod, cumsum, take, dot, searchsorted
25+
# TODO: wrap cumprod/cumsum, take, dot, argsort/sort
26+
27+
28+
def _values_method_wrapper(f):
29+
def func(self, *args, **kwargs):
30+
return getattr(self.values, f)(*args, **kwargs)
31+
func.__name__ = f
32+
return func
2333

2434

2535
def _method_wrapper(f):
@@ -81,6 +91,8 @@ def inject_special_operations(cls, priority=50):
8191
setattr(cls, op_str('i' + name),
8292
cls._inplace_binary_op(op('i' + name)))
8393
# patch in numpy methods
94+
for name in NUMPY_SAME_METHODS:
95+
setattr(cls, name, _values_method_wrapper(name))
8496
for name in NUMPY_UNARY_METHODS:
8597
setattr(cls, name, cls._unary_op(_method_wrapper(name)))
8698
inject_reduce_methods(cls)

xray/variable.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def __init__(self, dims, data, attributes=None, encoding=None):
217217
if attributes is None:
218218
attributes = {}
219219
self._attributes = OrderedDict(attributes)
220-
self.encoding = dict({} if encoding is None else encoding)
220+
self._encoding = dict({} if encoding is None else encoding)
221221

222222
@property
223223
def dtype(self):
@@ -360,6 +360,16 @@ def attrs(self):
360360
def attrs(self, value):
361361
self._attributes = OrderedDict(value)
362362

363+
@property
364+
def encoding(self):
365+
"""Dictionary of encodings on this variable.
366+
"""
367+
return self._encoding
368+
369+
@encoding.setter
370+
def encoding(self, value):
371+
self._encoding = dict(value)
372+
363373
def copy(self, deep=True):
364374
"""Returns a copy of this object.
365375

0 commit comments

Comments
 (0)