🍾 Xarray is now 10 years old! 🎉

xarray.register_dataset_accessor

xarray.register_dataset_accessor#

xarray.register_dataset_accessor(name)[source]#

Register a custom property on xarray.Dataset objects.

Parameters:

name (str) – Name under which the accessor should be registered. A warning is issued if this name conflicts with a preexisting attribute.

Examples

In your library code:

>>> @xr.register_dataset_accessor("geo")
... class GeoAccessor:
...     def __init__(self, xarray_obj):
...         self._obj = xarray_obj
...
...     @property
...     def center(self):
...         # return the geographic center point of this dataset
...         lon = self._obj.latitude
...         lat = self._obj.longitude
...         return (float(lon.mean()), float(lat.mean()))
...
...     def plot(self):
...         # plot this array's data on a map, e.g., using Cartopy
...         pass
...

Back in an interactive IPython session:

>>> ds = xr.Dataset(
...     {"longitude": np.linspace(0, 10), "latitude": np.linspace(0, 20)}
... )
>>> ds.geo.center
(10.0, 5.0)
>>> ds.geo.plot()  # plots data on a map