🍾 Xarray is now 10 years old! πŸŽ‰

xarray.Dataset.tail

Contents

xarray.Dataset.tail#

Dataset.tail(indexers=None, **indexers_kwargs)[source]#

Returns a new dataset with the last n values of each array for the specified dimension(s).

Parameters:
  • indexers (dict or int, default: 5) – A dict with keys matching dimensions and integer values n or a single integer n applied over all dimensions. One of indexers or indexers_kwargs must be provided.

  • **indexers_kwargs ({dim: n, ...}, optional) – The keyword arguments form of indexers. One of indexers or indexers_kwargs must be provided.

Examples

>>> activity_names = ["Walking", "Running", "Cycling", "Swimming", "Yoga"]
>>> durations = [30, 45, 60, 45, 60]  # in minutes
>>> energies = [150, 300, 250, 400, 100]  # in calories
>>> dataset = xr.Dataset(
...     {
...         "duration": (["activity"], durations),
...         "energy_expenditure": (["activity"], energies),
...     },
...     coords={"activity": activity_names},
... )
>>> sorted_dataset = dataset.sortby("energy_expenditure", ascending=False)
>>> sorted_dataset
<xarray.Dataset>
Dimensions:             (activity: 5)
Coordinates:
  * activity            (activity) <U8 'Swimming' 'Running' ... 'Walking' 'Yoga'
Data variables:
    duration            (activity) int64 45 45 60 30 60
    energy_expenditure  (activity) int64 400 300 250 150 100

# Activities with the least energy expenditures using tail()

>>> sorted_dataset.tail(3)
<xarray.Dataset>
Dimensions:             (activity: 3)
Coordinates:
  * activity            (activity) <U8 'Cycling' 'Walking' 'Yoga'
Data variables:
    duration            (activity) int64 60 30 60
    energy_expenditure  (activity) int64 250 150 100
>>> sorted_dataset.tail({"activity": 3})
<xarray.Dataset>
Dimensions:             (activity: 3)
Coordinates:
  * activity            (activity) <U8 'Cycling' 'Walking' 'Yoga'
Data variables:
    duration            (activity) int64 60 30 60
    energy_expenditure  (activity) int64 250 150 100