xarray.DataArray

class xarray.DataArray(data, coords=None, dims=None, name=None, attrs=None, encoding=None, fastpath=False)

N-dimensional array with labeled coordinates and dimensions.

DataArray provides a wrapper around numpy ndarrays that uses labeled dimensions and coordinates to support metadata aware operations. The API is similar to that for the pandas Series or DataFrame, but DataArray objects can have any number of dimensions, and their contents have fixed data types.

Additional features over raw numpy arrays:

  • Apply operations over dimensions by name: x.sum('time').
  • Select or assign values by integer location (like numpy): x[:10] or by label (like pandas): x.loc['2014-01-01'] or x.sel(time='2014-01-01').
  • Mathematical operations (e.g., x - y) vectorize across multiple dimensions (known in numpy as “broadcasting”) based on dimension names, regardless of their original order.
  • Keep track of arbitrary metadata in the form of a Python dictionary: x.attrs
  • Convert to a pandas Series: x.to_series().

Getting items from or doing mathematical operations with a DataArray always returns another DataArray.

Attributes

dims Tuple of dimension names associated with this array.
values The array’s data as a numpy.ndarray
coords Dictionary-like container of coordinate arrays.
name The name of this array.
attrs Dictionary storing arbitrary metadata with this array.
__init__(data, coords=None, dims=None, name=None, attrs=None, encoding=None, fastpath=False)
Parameters:

data : array_like

Values for this array. Must be an numpy.ndarray, ndarray like, or castable to an ndarray. If a self-described xarray or pandas object, attempts are made to use this array’s metadata to fill in other unspecified arguments. A view of the array’s data is used instead of a copy if possible.

coords : sequence or dict of array_like objects, optional

Coordinates (tick labels) to use for indexing along each dimension. If dict-like, should be a mapping from dimension names to the corresponding coordinates. If sequence-like, should be a sequence of tuples where the first element is the dimension name and the second element is the corresponding coordinate array_like object.

dims : str or sequence of str, optional

Name(s) of the data dimension(s). Must be either a string (only for 1D data) or a sequence of strings with length equal to the number of dimensions. If this argument is omitted, dimension names are taken from coords (if possible) and otherwise default to ['dim_0', ... 'dim_n'].

name : str or None, optional

Name of this array.

attrs : dict_like or None, optional

Attributes to assign to the new instance. By default, an empty attribute dictionary is initialized.

encoding : dict_like or None, optional

Dictionary specifying how to encode this array’s data into a serialized format like netCDF4. Currently used keys (for netCDF) include ‘_FillValue’, ‘scale_factor’, ‘add_offset’, ‘dtype’, ‘units’ and ‘calendar’ (the later two only for datetime arrays). Unrecognized keys are ignored.

Methods

__init__(data[, coords, dims, name, attrs, …])
Parameters:
all([dim, axis, keep_attrs]) Reduce this DataArray’s data by applying all along some dimension(s).
any([dim, axis, keep_attrs]) Reduce this DataArray’s data by applying any along some dimension(s).
argmax([dim, axis, skipna, keep_attrs]) Reduce this DataArray’s data by applying argmax along some dimension(s).
argmin([dim, axis, skipna, keep_attrs]) Reduce this DataArray’s data by applying argmin along some dimension(s).
argsort([axis, kind, order]) Returns the indices that would sort this array.
assign_attrs(*args, **kwargs) Assign new attrs to this object.
assign_coords(**kwargs) Assign new coordinates to this object.
astype(dtype[, order, casting, subok, copy]) Copy of the array, cast to a specified type.
bfill(dim[, limit]) Fill NaN values by propogating values backward
broadcast_equals(other) Two DataArrays are broadcast equal if they are equal after broadcasting them against each other such that they have the same dimensions.
chunk([chunks, name_prefix, token, lock]) Coerce this array’s data into a dask arrays with the given chunks.
clip([min, max, out]) Return an array whose values are limited to [min, max].
close() Close any files linked to this object
combine_first(other) Combine two DataArray objects, with union of coordinates.
compute(**kwargs) Manually trigger loading of this array’s data from disk or a remote source into memory and return a new array.
conj() Complex-conjugate all elements.
conjugate() Return the complex conjugate, element-wise.
copy([deep]) Returns a copy of this array.
count([dim, axis, keep_attrs]) Reduce this DataArray’s data by applying count along some dimension(s).
cumprod([dim, axis, skipna, keep_attrs]) Apply cumprod along some dimension of DataArray.
cumsum([dim, axis, skipna, keep_attrs]) Apply cumsum along some dimension of DataArray.
diff(dim[, n, label]) Calculate the n-th order discrete difference along given axis.
dot(other[, dims]) Perform dot product of two DataArrays along their shared dims.
drop(labels[, dim]) Drop coordinates or index labels from this DataArray.
dropna(dim[, how, thresh]) Returns a new array with dropped labels for missing values along the provided dimension.
equals(other) True if two DataArrays have the same dimensions, coordinates and values; otherwise False.
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 propogating values forward
fillna(value) Fill missing values in this object.
from_cdms2(variable) Convert a cdms2.Variable into an xarray.DataArray
from_dict(d) Convert a dictionary into an xarray.DataArray
from_iris(cube) Convert a iris.cube.Cube into an xarray.DataArray
from_series(series) Convert a pandas.Series into an xarray.DataArray.
get_axis_num(dim) Return axis number(s) corresponding to dimension(s) in this array.
get_index(key) Get an index for a dimension, with fall-back to a default RangeIndex
groupby(group[, squeeze]) Returns a GroupBy object for performing grouped operations.
groupby_bins(group, bins[, right, labels, …]) Returns a GroupBy object for performing grouped operations.
identical(other) Like equals, but also checks the array name and attributes, and attributes on all coordinates.
interpolate_na([dim, method, limit, …]) Interpolate values according to different methods.
isel([drop]) Return a new DataArray whose dataset is given by integer indexing along the specified dimension(s).
isel_points([dim]) Return a new DataArray whose dataset is given by pointwise integer indexing along the specified dimension(s).
isin(test_elements) Tests each value in the array for whether it is in the supplied list.
isnull(*args, **kwargs)
item(*args) Copy an element of an array to a standard Python scalar and return it.
load(**kwargs) Manually trigger loading of this array’s data from disk or a remote source into memory and return this array.
max([dim, axis, skipna, keep_attrs]) Reduce this DataArray’s data by applying max along some dimension(s).
mean([dim, axis, skipna, keep_attrs]) Reduce this DataArray’s data by applying mean along some dimension(s).
median([dim, axis, skipna, keep_attrs]) Reduce this DataArray’s data by applying median along some dimension(s).
min([dim, axis, skipna, keep_attrs]) Reduce this DataArray’s data by applying min along some dimension(s).
notnull(*args, **kwargs)
persist(**kwargs) Trigger computation in constituent dask arrays
pipe(func, *args, **kwargs) Apply func(self, *args, **kwargs)
prod([dim, axis, skipna, keep_attrs]) Reduce this DataArray’s data by applying prod along some dimension(s).
quantile(q[, dim, interpolation, keep_attrs]) Compute the qth quantile of the data along the specified dimension.
rank(dim[, pct, keep_attrs]) Ranks the data.
reduce(func[, dim, axis, keep_attrs]) Reduce this array by applying func along some dimension(s).
reindex([method, tolerance, copy]) Conform this object onto a new set of indexes, filling in missing values with NaN.
reindex_like(other[, method, tolerance, copy]) Conform this object onto the indexes of another object, filling in missing values with NaN.
rename(new_name_or_name_dict) Returns a new DataArray with renamed coordinates or a new name.
reorder_levels([inplace]) Rearrange index levels using input order.
resample([freq, dim, how, skipna, closed, …]) Returns a Resample object for performing resampling operations.
reset_coords([names, drop, inplace]) Given names of coordinates, reset them to become variables.
reset_index(dims_or_levels[, drop, inplace]) Reset the specified index(es) or multi-index level(s).
roll(**shifts) Roll this array by an offset along one or more dimensions.
rolling([min_periods, center]) Rolling window object.
round(*args, **kwargs)
searchsorted(v[, side, sorter]) Find indices where elements of v should be inserted in a to maintain order.
sel([method, tolerance, drop]) Return a new DataArray whose dataset is given by selecting index labels along the specified dimension(s).
sel_points([dim, method, tolerance]) Return a new DataArray whose dataset is given by pointwise selection of index labels along the specified dimension(s).
set_index([append, inplace]) Set DataArray (multi-)indexes using one or more existing coordinates.
shift(**shifts) Shift this array 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) Stack any number of existing dimensions into a single new dimension.
std([dim, axis, skipna, keep_attrs]) Reduce this DataArray’s data by applying std along some dimension(s).
sum([dim, axis, skipna, keep_attrs]) Reduce this DataArray’s data by applying sum along some dimension(s).
swap_dims(dims_dict) Returns a new DataArray with swapped dimensions.
to_cdms2() Convert this array into a cdms2.Variable
to_dataframe([name]) Convert this array and its coordinates into a tidy pandas.DataFrame.
to_dataset([dim, name]) Convert a DataArray to a Dataset.
to_dict() Convert this xarray.DataArray into a dictionary following xarray naming conventions.
to_index() Convert this variable to a pandas.Index.
to_iris() Convert this array into a iris.cube.Cube
to_masked_array([copy]) Convert this array into a numpy.ma.MaskedArray
to_netcdf(*args, **kwargs) Write DataArray contents to a netCDF file.
to_pandas() Convert this array into a pandas object with the same shape.
to_series() Convert this array into a pandas.Series.
transpose(*dims) Return a new DataArray object with transposed dimensions.
unstack(dim) Unstack an existing dimension corresponding to a MultiIndex into multiple new dimensions.
var([dim, axis, skipna, keep_attrs]) Reduce this DataArray’s data by applying var along some dimension(s).
where(cond[, other, drop]) Filter elements from this object according to a condition.

Attributes

T
attrs Dictionary storing arbitrary metadata with this array.
chunks Block dimensions for this array’s data or None if it’s not a dask array.
coords Dictionary-like container of coordinate arrays.
data The array’s data as a dask or numpy array
dims Tuple of dimension names associated with this array.
dt Access datetime fields for DataArrays with datetime-like dtypes.
dtype
encoding Dictionary of format-specific settings for how this array should be serialized.
imag
indexes OrderedDict of pandas.Index objects used for label based indexing
loc Attribute for location based indexing like pandas.
name The name of this array.
nbytes
ndim
plot Access plotting functions
real
shape
size
sizes Ordered mapping from dimension names to lengths.
values The array’s data as a numpy.ndarray
variable Low level interface to the Variable object for this DataArray.