pipe

UnitsAwareDataArray.pipe(func: Callable[[...], T] | tuple[Callable[[...], T], str], *args: Any, **kwargs: Any) T

Apply func(self, *args, **kwargs)

This method replicates the pandas method of the same name.

Parameters:
  • func (callable) – function to apply to this xarray object (Dataset/DataArray). args, and kwargs are passed into func. Alternatively a (callable, data_keyword) tuple where data_keyword is a string indicating the keyword of callable that expects the xarray object.

  • *args – positional arguments passed into func.

  • **kwargs – a dictionary of keyword arguments passed into func.

Returns:

object – the return type of func.

Return type:

Any

Notes

Use .pipe when chaining together functions that expect xarray or pandas objects, e.g., instead of writing

f(g(h(ds), arg1=a), arg2=b, arg3=c)

You can write

(ds.pipe(h).pipe(g, arg1=a).pipe(f, arg2=b, arg3=c))

If you have a function that takes the data as (say) the second argument, pass a tuple indicating which keyword expects the data. For example, suppose f takes its data as arg2:

(ds.pipe(h).pipe(g, arg1=a).pipe((f, "arg2"), arg1=a, arg3=c))

Examples

>>> x = xr.Dataset(
...     {
...         "temperature_c": (
...             ("lat", "lon"),
...             20 * np.random.rand(4).reshape(2, 2),
...         ),
...         "precipitation": (("lat", "lon"), np.random.rand(4).reshape(2, 2)),
...     },
...     coords={"lat": [10, 20], "lon": [150, 160]},
... )
>>> x
<xarray.Dataset> Size: 96B
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 16B 10 20
  * lon            (lon) int64 16B 150 160
Data variables:
    temperature_c  (lat, lon) float64 32B 10.98 14.3 12.06 10.9
    precipitation  (lat, lon) float64 32B 0.4237 0.6459 0.4376 0.8918
>>> def adder(data, arg):
...     return data + arg
...
>>> def div(data, arg):
...     return data / arg
...
>>> def sub_mult(data, sub_arg, mult_arg):
...     return (data * mult_arg) - sub_arg
...
>>> x.pipe(adder, 2)
<xarray.Dataset> Size: 96B
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 16B 10 20
  * lon            (lon) int64 16B 150 160
Data variables:
    temperature_c  (lat, lon) float64 32B 12.98 16.3 14.06 12.9
    precipitation  (lat, lon) float64 32B 2.424 2.646 2.438 2.892
>>> x.pipe(adder, arg=2)
<xarray.Dataset> Size: 96B
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 16B 10 20
  * lon            (lon) int64 16B 150 160
Data variables:
    temperature_c  (lat, lon) float64 32B 12.98 16.3 14.06 12.9
    precipitation  (lat, lon) float64 32B 2.424 2.646 2.438 2.892
>>> (
...     x.pipe(adder, arg=2)
...     .pipe(div, arg=2)
...     .pipe(sub_mult, sub_arg=2, mult_arg=2)
... )
<xarray.Dataset> Size: 96B
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 16B 10 20
  * lon            (lon) int64 16B 150 160
Data variables:
    temperature_c  (lat, lon) float64 32B 10.98 14.3 12.06 10.9
    precipitation  (lat, lon) float64 32B 0.4237 0.6459 0.4376 0.8918

See also

pandas.DataFrame.pipe