🍾 Xarray is now 10 years old! 🎉

xarray.Dataset.filter_by_attrs

xarray.Dataset.filter_by_attrs#

Dataset.filter_by_attrs(**kwargs)[source]#

Returns a Dataset with variables that match specific conditions.

Can pass in key=value or key=callable. A Dataset is returned containing only the variables for which all the filter tests pass. These tests are either key=value for which the attribute key has the exact value value or the callable passed into key=callable returns True. The callable will be passed a single value, either the value of the attribute key or None if the DataArray does not have an attribute with the name key.

Parameters:

**kwargs

keystr

Attribute name.

valuecallable or obj

If value is a callable, it should return a boolean in the form of bool = func(attr) where attr is da.attrs[key]. Otherwise, value will be compared to the each DataArray’s attrs[key].

Returns:

new (Dataset) – New dataset with variables filtered by attribute.

Examples

>>> temp = 15 + 8 * np.random.randn(2, 2, 3)
>>> precip = 10 * np.random.rand(2, 2, 3)
>>> lon = [[-99.83, -99.32], [-99.79, -99.23]]
>>> lat = [[42.25, 42.21], [42.63, 42.59]]
>>> dims = ["x", "y", "time"]
>>> temp_attr = dict(standard_name="air_potential_temperature")
>>> precip_attr = dict(standard_name="convective_precipitation_flux")
>>> ds = xr.Dataset(
...     dict(
...         temperature=(dims, temp, temp_attr),
...         precipitation=(dims, precip, precip_attr),
...     ),
...     coords=dict(
...         lon=(["x", "y"], lon),
...         lat=(["x", "y"], lat),
...         time=pd.date_range("2014-09-06", periods=3),
...         reference_time=pd.Timestamp("2014-09-05"),
...     ),
... )

Get variables matching a specific standard_name:

>>> ds.filter_by_attrs(standard_name="convective_precipitation_flux")
<xarray.Dataset>
Dimensions:         (x: 2, y: 2, time: 3)
Coordinates:
    lon             (x, y) float64 -99.83 -99.32 -99.79 -99.23
    lat             (x, y) float64 42.25 42.21 42.63 42.59
  * time            (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
    reference_time  datetime64[ns] 2014-09-05
Dimensions without coordinates: x, y
Data variables:
    precipitation   (x, y, time) float64 5.68 9.256 0.7104 ... 7.992 4.615 7.805

Get all variables that have a standard_name attribute:

>>> standard_name = lambda v: v is not None
>>> ds.filter_by_attrs(standard_name=standard_name)
<xarray.Dataset>
Dimensions:         (x: 2, y: 2, time: 3)
Coordinates:
    lon             (x, y) float64 -99.83 -99.32 -99.79 -99.23
    lat             (x, y) float64 42.25 42.21 42.63 42.59
  * time            (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
    reference_time  datetime64[ns] 2014-09-05
Dimensions without coordinates: x, y
Data variables:
    temperature     (x, y, time) float64 29.11 18.2 22.83 ... 18.28 16.15 26.63
    precipitation   (x, y, time) float64 5.68 9.256 0.7104 ... 7.992 4.615 7.805