Skip to content

Commit 255bc8e

Browse files
authored
docstrings for isnull and notnull (#4618)
* 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
1 parent d1faca5 commit 255bc8e

File tree

4 files changed

+143
-5
lines changed

4 files changed

+143
-5
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ Documentation
109109
By `Sahid Velji <https://github.com/sahidvelji>`_.
110110
- Update link to NumPy docstring standard in the :doc:`contributing` guide (:pull:`4558`).
111111
By `Sahid Velji <https://github.com/sahidvelji>`_.
112+
- Add docstrings to ``isnull`` and ``notnull``, and fix the displayed signature
113+
(:issue:`2760`, :pull:`4618`).
114+
By `Justus Magin <https://github.com/keewis>`_.
112115

113116
Internal Changes
114117
~~~~~~~~~~~~~~~~

xarray/core/common.py

+72
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,78 @@ def close(self: Any) -> None:
12681268
self._file_obj.close()
12691269
self._file_obj = None
12701270

1271+
def isnull(self, keep_attrs: bool = None):
1272+
"""Test each value in the array for whether it is a missing value.
1273+
1274+
Returns
1275+
-------
1276+
isnull : DataArray or Dataset
1277+
Same type and shape as object, but the dtype of the data is bool.
1278+
1279+
See Also
1280+
--------
1281+
pandas.isnull
1282+
1283+
Examples
1284+
--------
1285+
>>> array = xr.DataArray([1, np.nan, 3], dims="x")
1286+
>>> array
1287+
<xarray.DataArray (x: 3)>
1288+
array([ 1., nan, 3.])
1289+
Dimensions without coordinates: x
1290+
>>> array.isnull()
1291+
<xarray.DataArray (x: 3)>
1292+
array([False, True, False])
1293+
Dimensions without coordinates: x
1294+
"""
1295+
from .computation import apply_ufunc
1296+
1297+
if keep_attrs is None:
1298+
keep_attrs = _get_keep_attrs(default=False)
1299+
1300+
return apply_ufunc(
1301+
duck_array_ops.isnull,
1302+
self,
1303+
dask="allowed",
1304+
keep_attrs=keep_attrs,
1305+
)
1306+
1307+
def notnull(self, keep_attrs: bool = None):
1308+
"""Test each value in the array for whether it is not a missing value.
1309+
1310+
Returns
1311+
-------
1312+
notnull : DataArray or Dataset
1313+
Same type and shape as object, but the dtype of the data is bool.
1314+
1315+
See Also
1316+
--------
1317+
pandas.notnull
1318+
1319+
Examples
1320+
--------
1321+
>>> array = xr.DataArray([1, np.nan, 3], dims="x")
1322+
>>> array
1323+
<xarray.DataArray (x: 3)>
1324+
array([ 1., nan, 3.])
1325+
Dimensions without coordinates: x
1326+
>>> array.notnull()
1327+
<xarray.DataArray (x: 3)>
1328+
array([ True, False, True])
1329+
Dimensions without coordinates: x
1330+
"""
1331+
from .computation import apply_ufunc
1332+
1333+
if keep_attrs is None:
1334+
keep_attrs = _get_keep_attrs(default=False)
1335+
1336+
return apply_ufunc(
1337+
duck_array_ops.notnull,
1338+
self,
1339+
dask="allowed",
1340+
keep_attrs=keep_attrs,
1341+
)
1342+
12711343
def isin(self, test_elements):
12721344
"""Tests each value in the array for whether it is in test elements.
12731345

xarray/core/ops.py

-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
# methods which don't modify the data shape, so the result should still be
4444
# wrapped in an Variable/DataArray
4545
NUMPY_UNARY_METHODS = ["argsort", "clip", "conj", "conjugate"]
46-
PANDAS_UNARY_FUNCTIONS = ["isnull", "notnull"]
4746
# methods which remove an axis
4847
REDUCE_METHODS = ["all", "any"]
4948
NAN_REDUCE_METHODS = [
@@ -334,10 +333,6 @@ def inject_all_ops_and_reduce_methods(cls, priority=50, array_only=True):
334333
for name in NUMPY_UNARY_METHODS:
335334
setattr(cls, name, cls._unary_op(_method_wrapper(name)))
336335

337-
for name in PANDAS_UNARY_FUNCTIONS:
338-
f = _func_slash_method_wrapper(getattr(duck_array_ops, name), name=name)
339-
setattr(cls, name, cls._unary_op(f))
340-
341336
f = _func_slash_method_wrapper(duck_array_ops.around, name="round")
342337
setattr(cls, "round", cls._unary_op(f))
343338

xarray/core/variable.py

+68
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,74 @@ def _coarsen_reshape(self, windows, boundary, side):
20872087

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

2090+
def isnull(self, keep_attrs: bool = None):
2091+
"""Test each value in the array for whether it is a missing value.
2092+
2093+
Returns
2094+
-------
2095+
isnull : Variable
2096+
Same type and shape as object, but the dtype of the data is bool.
2097+
2098+
See Also
2099+
--------
2100+
pandas.isnull
2101+
2102+
Examples
2103+
--------
2104+
>>> var = xr.Variable("x", [1, np.nan, 3])
2105+
>>> var
2106+
<xarray.Variable (x: 3)>
2107+
array([ 1., nan, 3.])
2108+
>>> var.isnull()
2109+
<xarray.Variable (x: 3)>
2110+
array([False, True, False])
2111+
"""
2112+
from .computation import apply_ufunc
2113+
2114+
if keep_attrs is None:
2115+
keep_attrs = _get_keep_attrs(default=False)
2116+
2117+
return apply_ufunc(
2118+
duck_array_ops.isnull,
2119+
self,
2120+
dask="allowed",
2121+
keep_attrs=keep_attrs,
2122+
)
2123+
2124+
def notnull(self, keep_attrs: bool = None):
2125+
"""Test each value in the array for whether it is not a missing value.
2126+
2127+
Returns
2128+
-------
2129+
notnull : Variable
2130+
Same type and shape as object, but the dtype of the data is bool.
2131+
2132+
See Also
2133+
--------
2134+
pandas.notnull
2135+
2136+
Examples
2137+
--------
2138+
>>> var = xr.Variable("x", [1, np.nan, 3])
2139+
>>> var
2140+
<xarray.Variable (x: 3)>
2141+
array([ 1., nan, 3.])
2142+
>>> var.notnull()
2143+
<xarray.Variable (x: 3)>
2144+
array([ True, False, True])
2145+
"""
2146+
from .computation import apply_ufunc
2147+
2148+
if keep_attrs is None:
2149+
keep_attrs = _get_keep_attrs(default=False)
2150+
2151+
return apply_ufunc(
2152+
duck_array_ops.notnull,
2153+
self,
2154+
dask="allowed",
2155+
keep_attrs=keep_attrs,
2156+
)
2157+
20902158
@property
20912159
def real(self):
20922160
return type(self)(self.dims, self.data.real, self._attrs)

0 commit comments

Comments
 (0)