Skip to content

Commit

Permalink
docstrings for isnull and notnull (#4618)
Browse files Browse the repository at this point in the history
* add docstrings to isnull and notnull

* switch isnull and notnull from monkey patched to true functions

* update whats-new.rst

* add the missing keep_attrs parameter

* add back the variable methods

* fix the variable creation
  • Loading branch information
keewis authored Nov 30, 2020
1 parent d1faca5 commit 255bc8e
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 5 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ Documentation
By `Sahid Velji <https://github.com/sahidvelji>`_.
- Update link to NumPy docstring standard in the :doc:`contributing` guide (:pull:`4558`).
By `Sahid Velji <https://github.com/sahidvelji>`_.
- Add docstrings to ``isnull`` and ``notnull``, and fix the displayed signature
(:issue:`2760`, :pull:`4618`).
By `Justus Magin <https://github.com/keewis>`_.

Internal Changes
~~~~~~~~~~~~~~~~
Expand Down
72 changes: 72 additions & 0 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,78 @@ def close(self: Any) -> None:
self._file_obj.close()
self._file_obj = None

def isnull(self, keep_attrs: bool = None):
"""Test each value in the array for whether it is a missing value.
Returns
-------
isnull : DataArray or Dataset
Same type and shape as object, but the dtype of the data is bool.
See Also
--------
pandas.isnull
Examples
--------
>>> array = xr.DataArray([1, np.nan, 3], dims="x")
>>> array
<xarray.DataArray (x: 3)>
array([ 1., nan, 3.])
Dimensions without coordinates: x
>>> array.isnull()
<xarray.DataArray (x: 3)>
array([False, True, False])
Dimensions without coordinates: x
"""
from .computation import apply_ufunc

if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)

return apply_ufunc(
duck_array_ops.isnull,
self,
dask="allowed",
keep_attrs=keep_attrs,
)

def notnull(self, keep_attrs: bool = None):
"""Test each value in the array for whether it is not a missing value.
Returns
-------
notnull : DataArray or Dataset
Same type and shape as object, but the dtype of the data is bool.
See Also
--------
pandas.notnull
Examples
--------
>>> array = xr.DataArray([1, np.nan, 3], dims="x")
>>> array
<xarray.DataArray (x: 3)>
array([ 1., nan, 3.])
Dimensions without coordinates: x
>>> array.notnull()
<xarray.DataArray (x: 3)>
array([ True, False, True])
Dimensions without coordinates: x
"""
from .computation import apply_ufunc

if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)

return apply_ufunc(
duck_array_ops.notnull,
self,
dask="allowed",
keep_attrs=keep_attrs,
)

def isin(self, test_elements):
"""Tests each value in the array for whether it is in test elements.
Expand Down
5 changes: 0 additions & 5 deletions xarray/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
# methods which don't modify the data shape, so the result should still be
# wrapped in an Variable/DataArray
NUMPY_UNARY_METHODS = ["argsort", "clip", "conj", "conjugate"]
PANDAS_UNARY_FUNCTIONS = ["isnull", "notnull"]
# methods which remove an axis
REDUCE_METHODS = ["all", "any"]
NAN_REDUCE_METHODS = [
Expand Down Expand Up @@ -334,10 +333,6 @@ def inject_all_ops_and_reduce_methods(cls, priority=50, array_only=True):
for name in NUMPY_UNARY_METHODS:
setattr(cls, name, cls._unary_op(_method_wrapper(name)))

for name in PANDAS_UNARY_FUNCTIONS:
f = _func_slash_method_wrapper(getattr(duck_array_ops, name), name=name)
setattr(cls, name, cls._unary_op(f))

f = _func_slash_method_wrapper(duck_array_ops.around, name="round")
setattr(cls, "round", cls._unary_op(f))

Expand Down
68 changes: 68 additions & 0 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2087,6 +2087,74 @@ def _coarsen_reshape(self, windows, boundary, side):

return variable.data.reshape(shape), tuple(axes)

def isnull(self, keep_attrs: bool = None):
"""Test each value in the array for whether it is a missing value.
Returns
-------
isnull : Variable
Same type and shape as object, but the dtype of the data is bool.
See Also
--------
pandas.isnull
Examples
--------
>>> var = xr.Variable("x", [1, np.nan, 3])
>>> var
<xarray.Variable (x: 3)>
array([ 1., nan, 3.])
>>> var.isnull()
<xarray.Variable (x: 3)>
array([False, True, False])
"""
from .computation import apply_ufunc

if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)

return apply_ufunc(
duck_array_ops.isnull,
self,
dask="allowed",
keep_attrs=keep_attrs,
)

def notnull(self, keep_attrs: bool = None):
"""Test each value in the array for whether it is not a missing value.
Returns
-------
notnull : Variable
Same type and shape as object, but the dtype of the data is bool.
See Also
--------
pandas.notnull
Examples
--------
>>> var = xr.Variable("x", [1, np.nan, 3])
>>> var
<xarray.Variable (x: 3)>
array([ 1., nan, 3.])
>>> var.notnull()
<xarray.Variable (x: 3)>
array([ True, False, True])
"""
from .computation import apply_ufunc

if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)

return apply_ufunc(
duck_array_ops.notnull,
self,
dask="allowed",
keep_attrs=keep_attrs,
)

@property
def real(self):
return type(self)(self.dims, self.data.real, self._attrs)
Expand Down

0 comments on commit 255bc8e

Please sign in to comment.