🍾 Xarray is now 10 years old! 🎉

xarray.Dataset

xarray.Dataset#

class xarray.Dataset(data_vars=None, coords=None, attrs=None)[source]#

A multi-dimensional, in memory, array database.

A dataset resembles an in-memory representation of a NetCDF file, and consists of variables, coordinates and attributes which together form a self describing dataset.

Dataset implements the mapping interface with keys given by variable names and values given by DataArray objects for each variable name.

By default, pandas indexes are created for one dimensional variables with name equal to their dimension (i.e., Dimension coordinate) so those variables can be readily used as coordinates for label based indexing. When a Coordinates object is passed to coords, any existing index(es) built from those coordinates will be added to the Dataset.

To load data from a file or file-like object, use the open_dataset function.

Parameters:
  • data_vars (dict-like, optional) – A mapping from variable names to DataArray objects, Variable objects or to tuples of the form (dims, data[, attrs]) which can be used as arguments to create a new Variable. Each dimension must have the same length in all variables in which it appears.

    The following notations are accepted:

    • mapping {var name: DataArray}

    • mapping {var name: Variable}

    • mapping {var name: (dimension name, array-like)}

    • mapping {var name: (tuple of dimension names, array-like)}

    • mapping {dimension name: array-like} (if array-like is not a scalar it will be automatically moved to coords, see below)

    Each dimension must have the same length in all variables in which it appears.

  • coords (Coordinates or dict-like, optional) – A Coordinates object or another mapping in similar form as the data_vars argument, except that each item is saved on the dataset as a “coordinate”. These variables have an associated meaning: they describe constant/fixed/independent quantities, unlike the varying/measured/dependent quantities that belong in variables.

    The following notations are accepted for arbitrary mappings:

    • mapping {coord name: DataArray}

    • mapping {coord name: Variable}

    • mapping {coord name: (dimension name, array-like)}

    • mapping {coord name: (tuple of dimension names, array-like)}

    • mapping {dimension name: array-like} (the dimension name is implicitly set to be the same as the coord name)

    The last notation implies either that the coordinate value is a scalar or that it is a 1-dimensional array and the coord name is the same as the dimension name (i.e., a Dimension coordinate). In the latter case, the 1-dimensional array will be assumed to give index values along the dimension with the same name.

    Alternatively, a Coordinates object may be used in order to explicitly pass indexes (e.g., a multi-index or any custom Xarray index) or to bypass the creation of a default index for any Dimension coordinate included in that object.

  • attrs (dict-like, optional) – Global attributes to save on this dataset.

Examples

Create data:

>>> np.random.seed(0)
>>> temperature = 15 + 8 * np.random.randn(2, 2, 3)
>>> precipitation = 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]]
>>> time = pd.date_range("2014-09-06", periods=3)
>>> reference_time = pd.Timestamp("2014-09-05")

Initialize a dataset with multiple dimensions:

>>> ds = xr.Dataset(
...     data_vars=dict(
...         temperature=(["x", "y", "time"], temperature),
...         precipitation=(["x", "y", "time"], precipitation),
...     ),
...     coords=dict(
...         lon=(["x", "y"], lon),
...         lat=(["x", "y"], lat),
...         time=time,
...         reference_time=reference_time,
...     ),
...     attrs=dict(description="Weather related data."),
... )
>>> ds
<xarray.Dataset> Size: 288B
Dimensions:         (x: 2, y: 2, time: 3)
Coordinates:
    lon             (x, y) float64 32B -99.83 -99.32 -99.79 -99.23
    lat             (x, y) float64 32B 42.25 42.21 42.63 42.59
  * time            (time) datetime64[ns] 24B 2014-09-06 2014-09-07 2014-09-08
    reference_time  datetime64[ns] 8B 2014-09-05
Dimensions without coordinates: x, y
Data variables:
    temperature     (x, y, time) float64 96B 29.11 18.2 22.83 ... 16.15 26.63
    precipitation   (x, y, time) float64 96B 5.68 9.256 0.7104 ... 4.615 7.805
Attributes:
    description:  Weather related data.

Find out where the coldest temperature was and what values the other variables had:

>>> ds.isel(ds.temperature.argmin(...))
<xarray.Dataset> Size: 48B
Dimensions:         ()
Coordinates:
    lon             float64 8B -99.32
    lat             float64 8B 42.21
    time            datetime64[ns] 8B 2014-09-08
    reference_time  datetime64[ns] 8B 2014-09-05
Data variables:
    temperature     float64 8B 7.182
    precipitation   float64 8B 8.326
Attributes:
    description:  Weather related data.
__init__(data_vars=None, coords=None, attrs=None)[source]#

Methods

__init__([data_vars, coords, attrs])

all([dim, keep_attrs])

Reduce this Dataset's data by applying all along some dimension(s).

any([dim, keep_attrs])

Reduce this Dataset's data by applying any along some dimension(s).

apply(func[, keep_attrs, args])

Backward compatible implementation of map

argmax([dim])

Indices of the maxima of the member variables.

argmin([dim])

Indices of the minima of the member variables.

argsort([axis, kind, order])

Returns the indices that would sort this array.

as_numpy()

Coerces wrapped data and coordinates into numpy arrays, returning a Dataset.

assign([variables])

Assign new data variables to a Dataset, returning a new object with all the original variables in addition to the new ones.

assign_attrs(*args, **kwargs)

Assign new attrs to this object.

assign_coords([coords])

Assign new coordinates to this object.

astype(dtype, *[, order, casting, subok, ...])

Copy of the xarray object, with data cast to a specified type.

bfill(dim[, limit])

Fill NaN values by propagating values backward

broadcast_equals(other)

Two Datasets are broadcast equal if they are equal after broadcasting all variables against each other.

broadcast_like(other[, exclude])

Broadcast this DataArray against another Dataset or DataArray.

chunk([chunks, name_prefix, token, lock, ...])

Coerce all arrays in this dataset into dask arrays with the given chunks.

clip([min, max, keep_attrs])

Return an array whose values are limited to [min, max].

close()

Release any resources linked to this object.

coarsen([dim, boundary, side, coord_func])

Coarsen object for Datasets.

combine_first(other)

Combine two Datasets, default to data_vars of self.

compute(**kwargs)

Manually trigger loading and/or computation of this dataset's data from disk or a remote source into memory and return a new dataset.

conj()

Complex-conjugate all elements.

conjugate()

Return the complex conjugate, element-wise.

convert_calendar(calendar[, dim, align_on, ...])

Convert the Dataset to another calendar.

copy([deep, data])

Returns a copy of this dataset.

count([dim, keep_attrs])

Reduce this Dataset's data by applying count along some dimension(s).

cumprod([dim, skipna, keep_attrs])

Reduce this Dataset's data by applying cumprod along some dimension(s).

cumsum([dim, skipna, keep_attrs])

Reduce this Dataset's data by applying cumsum along some dimension(s).

cumulative(dim[, min_periods])

Accumulating object for Datasets

cumulative_integrate(coord[, datetime_unit])

Integrate along the given coordinate using the trapezoidal rule.

curvefit(coords, func[, reduce_dims, ...])

Curve fitting optimization for arbitrary functions.

diff(dim[, n, label])

Calculate the n-th order discrete difference along given axis.

differentiate(coord[, edge_order, datetime_unit])

Differentiate with the second order accurate central differences.

drop([labels, dim, errors])

Backward compatible method based on drop_vars and drop_sel

drop_dims(drop_dims, *[, errors])

Drop dimensions and associated variables from this dataset.

drop_duplicates(dim, *[, keep])

Returns a new Dataset with duplicate dimension values removed.

drop_encoding()

Return a new Dataset without encoding on the dataset or any of its variables/coords.

drop_indexes(coord_names, *[, errors])

Drop the indexes assigned to the given coordinates.

drop_isel([indexers])

Drop index positions from this Dataset.

drop_sel([labels, errors])

Drop index labels from this dataset.

drop_vars(names, *[, errors])

Drop variables from this dataset.

dropna(dim, *[, how, thresh, subset])

Returns a new dataset with dropped labels for missing values along the provided dimension.

dump_to_store(store, **kwargs)

Store dataset contents to a backends.*DataStore object.

equals(other)

Two Datasets are equal if they have matching variables and coordinates, all of which are equal.

eval(statement, *[, parser])

Calculate an expression supplied as a string in the context of the dataset.

expand_dims([dim, axis])

Return a new object with an additional axis (or axes) inserted at the corresponding position in the array shape.

ffill(dim[, limit])

Fill NaN values by propagating values forward

fillna(value)

Fill missing values in this object.

filter_by_attrs(**kwargs)

Returns a Dataset with variables that match specific conditions.

from_dataframe(dataframe[, sparse])

Convert a pandas.DataFrame into an xarray.Dataset

from_dict(d)

Convert a dictionary into an xarray.Dataset.

get(k[,d])

get_index(key)

Get an index for a dimension, with fall-back to a default RangeIndex

groupby(group[, squeeze, restore_coord_dims])

Returns a DatasetGroupBy object for performing grouped operations.

groupby_bins(group, bins[, right, labels, ...])

Returns a DatasetGroupBy object for performing grouped operations.

head([indexers])

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

identical(other)

Like equals, but also checks all dataset attributes and the attributes on all variables and coordinates.

idxmax([dim, skipna, fill_value, keep_attrs])

Return the coordinate label of the maximum value along a dimension.

idxmin([dim, skipna, fill_value, keep_attrs])

Return the coordinate label of the minimum value along a dimension.

info([buf])

Concise summary of a Dataset variables and attributes.

integrate(coord[, datetime_unit])

Integrate along the given coordinate using the trapezoidal rule.

interp([coords, method, assume_sorted, ...])

Interpolate a Dataset onto new coordinates

interp_calendar(target[, dim])

Interpolates the Dataset to another calendar based on decimal year measure.

interp_like(other[, method, assume_sorted, ...])

Interpolate this object onto the coordinates of another object, filling the out of range values with NaN.

interpolate_na([dim, method, limit, ...])

Fill in NaNs by interpolating according to different methods.

isel([indexers, drop, missing_dims])

Returns a new dataset with each array indexed along the specified dimension(s).

isin(test_elements)

Tests each value in the array for whether it is in test elements.

isnull([keep_attrs])

Test each value in the array for whether it is a missing value.

items()

keys()

load(**kwargs)

Manually trigger loading and/or computation of this dataset's data from disk or a remote source into memory and return this dataset.

load_store(store[, decoder])

Create a new dataset from the contents of a backends.*DataStore object

map(func[, keep_attrs, args])

Apply a function to each data variable in this dataset

map_blocks(func[, args, kwargs, template])

Apply a function to each block of this Dataset.

max([dim, skipna, keep_attrs])

Reduce this Dataset's data by applying max along some dimension(s).

mean([dim, skipna, keep_attrs])

Reduce this Dataset's data by applying mean along some dimension(s).

median([dim, skipna, keep_attrs])

Reduce this Dataset's data by applying median along some dimension(s).

merge(other[, overwrite_vars, compat, join, ...])

Merge the arrays of two datasets into a single dataset.

min([dim, skipna, keep_attrs])

Reduce this Dataset's data by applying min along some dimension(s).

notnull([keep_attrs])

Test each value in the array for whether it is not a missing value.

pad([pad_width, mode, stat_length, ...])

Pad this dataset along one or more dimensions.

persist(**kwargs)

Trigger computation, keeping data as dask arrays

pipe(func, *args, **kwargs)

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

polyfit(dim, deg[, skipna, rcond, w, full, cov])

Least squares polynomial fit.

prod([dim, skipna, min_count, keep_attrs])

Reduce this Dataset's data by applying prod along some dimension(s).

quantile(q[, dim, method, numeric_only, ...])

Compute the qth quantile of the data along the specified dimension.

query([queries, parser, engine, missing_dims])

Return a new dataset with each array indexed along the specified dimension(s), where the indexers are given as strings containing Python expressions to be evaluated against the data variables in the dataset.

rank(dim, *[, pct, keep_attrs])

Ranks the data.

reduce(func[, dim, keep_attrs, keepdims, ...])

Reduce this dataset by applying func along some dimension(s).

reindex([indexers, method, tolerance, copy, ...])

Conform this object onto a new set of indexes, filling in missing values with fill_value.

reindex_like(other[, method, tolerance, ...])

Conform this object onto the indexes of another object, for indexes which the objects share.

rename([name_dict])

Returns a new object with renamed variables, coordinates and dimensions.

rename_dims([dims_dict])

Returns a new object with renamed dimensions only.

rename_vars([name_dict])

Returns a new object with renamed variables including coordinates

reorder_levels([dim_order])

Rearrange index levels using input order.

resample([indexer, skipna, closed, label, ...])

Returns a Resample object for performing resampling operations.

reset_coords([names, drop])

Given names of coordinates, reset them to become variables

reset_encoding()

reset_index(dims_or_levels, *[, drop])

Reset the specified index(es) or multi-index level(s).

roll([shifts, roll_coords])

Roll this dataset by an offset along one or more dimensions.

rolling([dim, min_periods, center])

Rolling window object for Datasets.

rolling_exp([window, window_type])

Exponentially-weighted moving window.

round(*args, **kwargs)

Round an array to the given number of decimals.

sel([indexers, method, tolerance, drop])

Returns a new dataset with each array indexed by tick labels along the specified dimension(s).

set_close(close)

Register the function that releases any resources linked to this object.

set_coords(names)

Given names of one or more variables, set them as coordinates

set_index([indexes, append])

Set Dataset (multi-)indexes using one or more existing coordinates or variables.

set_xindex(coord_names[, index_cls])

Set a new, Xarray-compatible index from one or more existing coordinate(s).

shift([shifts, fill_value])

Shift this dataset by an offset along one or more dimensions.

sortby(variables[, ascending])

Sort object by labels or values (along an axis).

squeeze([dim, drop, axis])

Return a new object with squeezed data.

stack([dimensions, create_index, index_cls])

Stack any number of existing dimensions into a single new dimension.

std([dim, skipna, ddof, keep_attrs])

Reduce this Dataset's data by applying std along some dimension(s).

sum([dim, skipna, min_count, keep_attrs])

Reduce this Dataset's data by applying sum along some dimension(s).

swap_dims([dims_dict])

Returns a new object with swapped dimensions.

tail([indexers])

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

thin([indexers])

Returns a new dataset with each array indexed along every n-th value for the specified dimension(s)

to_array([dim, name])

Deprecated version of to_dataarray

to_dask_dataframe([dim_order, set_index])

Convert this dataset into a dask.dataframe.DataFrame.

to_dataarray([dim, name])

Convert this dataset into an xarray.DataArray

to_dataframe([dim_order])

Convert this dataset into a pandas.DataFrame.

to_dict([data, encoding])

Convert this dataset to a dictionary following xarray naming conventions.

to_netcdf([path, mode, format, group, ...])

Write dataset contents to a netCDF file.

to_pandas()

Convert this dataset into a pandas object without changing the number of dimensions.

to_stacked_array(new_dim, sample_dims[, ...])

Combine variables of differing dimensionality into a DataArray without broadcasting.

to_zarr([store, chunk_store, mode, ...])

Write dataset contents to a zarr group.

transpose(*dims[, missing_dims])

Return a new Dataset object with all array dimensions transposed.

unify_chunks()

Unify chunk size along all chunked dimensions of this Dataset.

unstack([dim, fill_value, sparse])

Unstack existing dimensions corresponding to MultiIndexes into multiple new dimensions.

update(other)

Update this dataset's variables with those from another dataset.

values()

var([dim, skipna, ddof, keep_attrs])

Reduce this Dataset's data by applying var along some dimension(s).

weighted(weights)

Weighted Dataset operations.

where(cond[, other, drop])

Filter elements from this object according to a condition.

Attributes

attrs

Dictionary of global attributes on this dataset

chunks

Mapping from dimension names to block lengths for this dataset's data, or None if the underlying data is not a dask array.

chunksizes

Mapping from dimension names to block lengths for this dataset's data, or None if the underlying data is not a dask array.

coords

Mapping of DataArray objects corresponding to coordinate variables.

data_vars

Dictionary of DataArray objects corresponding to data variables

dims

Mapping from dimension names to lengths.

dtypes

Mapping from data variable names to dtypes.

encoding

Dictionary of global encoding attributes on this dataset

imag

The imaginary part of each data variable.

indexes

Mapping of pandas.Index objects used for label based indexing.

loc

Attribute for location based indexing.

nbytes

Total bytes consumed by the data arrays of all variables in this dataset.

real

The real part of each data variable.

sizes

Mapping from dimension names to lengths.

variables

Low level interface to Dataset contents as dict of Variable objects.

xindexes

Mapping of Index objects used for label based indexing.