Skip to content

Commit 7e0e7b1

Browse files
committed
Merge pull request #174 from shoyer/add-isnull
Add isnull and notnull (wrapping pandas)
2 parents 3672599 + e6203c1 commit 7e0e7b1

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

doc/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ Computations
209209
DataArray.std
210210
DataArray.sum
211211
DataArray.var
212+
DataArray.isnull
213+
DataArray.notnull
214+
212215

213216
Comparisons
214217
~~~~~~~~~~~

test/test_data_array.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,15 @@ def test_array_interface(self):
315315
bar = Variable(['x', 'y'], np.zeros((10, 20)))
316316
self.assertDataArrayEqual(self.dv, np.maximum(self.dv, bar))
317317

318+
def test_is_null(self):
319+
x = np.random.RandomState(42).randn(5, 6)
320+
x[x < 0] = np.nan
321+
original = DataArray(x, [-np.arange(5), np.arange(6)], ['x', 'y'])
322+
expected = DataArray(pd.isnull(x), [-np.arange(5), np.arange(6)],
323+
['x', 'y'])
324+
self.assertDataArrayIdentical(expected, original.isnull())
325+
self.assertDataArrayIdentical(~expected, original.notnull())
326+
318327
def test_math(self):
319328
x = self.x
320329
v = self.v

xray/data_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ def __array_wrap__(self, obj, context=None):
791791
def _unary_op(f):
792792
@functools.wraps(f)
793793
def func(self, *args, **kwargs):
794-
return self.__array_wrap__(f(self.variable, *args, **kwargs))
794+
return self.__array_wrap__(f(self.values, *args, **kwargs))
795795
return func
796796

797797
def _check_indexes_compat(self, other):

xray/ops.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import operator
22

33
import numpy as np
4+
import pandas as pd
45

56
from .pycompat import PY3
67

@@ -19,10 +20,11 @@
1920
# wrapped in an Variable/DataArray
2021
NUMPY_UNARY_METHODS = ['astype', 'argsort', 'clip', 'conj', 'conjugate',
2122
'round']
23+
PANDAS_UNARY_FUNCTIONS = ['isnull', 'notnull']
2224
# methods which remove an axis
2325
NUMPY_REDUCE_METHODS = ['all', 'any', 'argmax', 'argmin', 'max', 'mean', 'min',
2426
'prod', 'ptp', 'std', 'sum', 'var']
25-
# TODO: wrap cumprod/cumsum, take, dot, argsort/sort
27+
# TODO: wrap cumprod/cumsum, take, dot, sort
2628

2729

2830
def _values_method_wrapper(f):
@@ -95,4 +97,6 @@ def inject_special_operations(cls, priority=50):
9597
setattr(cls, name, _values_method_wrapper(name))
9698
for name in NUMPY_UNARY_METHODS:
9799
setattr(cls, name, cls._unary_op(_method_wrapper(name)))
100+
for name in PANDAS_UNARY_FUNCTIONS:
101+
setattr(cls, name, cls._unary_op(getattr(pd, name)))
98102
inject_reduce_methods(cls)

0 commit comments

Comments
 (0)