xarray extension for data comparison
xarray-compare is a third-party Python package which provides extra data-comparison features. After importing the package, several DataArray methods (dataarray.is*()) will be available.
dataarray.is*()
Python: 3.6, 3.7, or 3.8 (tested by the author)
Dependencies: See pyproject.toml
$ pip install xarray-compare
.isbetween(lower, upper): Test whether each value in a DataArray falls within an interval
.isbetween(lower, upper)
.ismatch(pattern): Test whether each string in a DataArray matches a regex pattern
.ismatch(pattern)
Methods of “not-in” version are also provided for readability.
.isnotin(values): Equivalent to ~dataarray.isin(values) (.isin() is an xarray’s builtin)
.isnotin(values)
~dataarray.isin(values)
.isin()
.isnotbetween(lower, upper): Equivalent to ~dataarray.isbetween(lower, upper)
.isnotbetween(lower, upper)
~dataarray.isbetween(lower, upper)
.isnotmatch(pattern): Equivalent to ~dataarray.ismatch(pattern)
.isnotmatch(pattern)
~dataarray.ismatch(pattern)
xarray-compare is a just-import package. After importing it, methods become available from normal DataArray instances.
import xarray as xr import xarray_compare
A method returns a boolean DataArray each value of which is True where that of the input DataArray fulfills the condition and False otherwise. This is why it works well with the dataarray.where() method.
True
False
dataarray.where()
da = xr.DataArray([0, 1, 1, 2, 3, 5, 8, 13]) da.where(da.isbetween(1, 4), drop=True) # <xarray.DataArray (dim_0: 4)> # array([1., 1., 2., 3.]) # Dimensions without coordinates: dim_0
da = xr.DataArray(['a', 'aa', 'ab', 'bc']) da.where(da.ismatch("^a+$"), drop=True) # <xarray.DataArray (dim_0: 2)> # array(['a', 'aa'], dtype=object) # Dimensions without coordinates: dim_0