xarray.DataArray

class xarray.DataArray(data=<NA>, coords=None, dims=None, name=None, attrs=None, encoding=None, indexes=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.

dims

Dimension names associated with this array.

Type

tuple

values

Access or modify DataArray values as a numpy array.

Type

np.ndarray

coords

Dictionary of DataArray objects that label values along each dimension.

Type

dict-like

name

Name of this array.

Type

str or None

attrs

Dictionary for holding arbitrary metadata.

Type

dict

__init__(self, data: Any = <NA>, coords: Union[Sequence[Tuple], Mapping[Hashable, Any], NoneType] = None, dims: Union[Hashable, Sequence[Hashable], NoneType] = None, name: Hashable = None, attrs: Mapping = None, encoding=None, indexes: Dict[Hashable, pandas.core.indexes.base.Index] = None, fastpath: bool = 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. The following notations are accepted:

    • mapping {dimension name: array-like}

    • sequence of tuples that are valid arguments for xarray.Variable() - (dims, data) - (dims, data, attrs) - (dims, data, attrs, encoding)

    Additionally, it is possible to define a coord whose name does not match the dimension name, or a coord based on multiple dimensions, with one of the following notations:

    • mapping {coord name: DataArray}

    • mapping {coord name: Variable}

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

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

  • dims (hashable or sequence of hashable, optional) – Name(s) of the data dimension(s). Must be either a hashable (only for 1D data) or a sequence of hashables 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.

Methods

__init__(self, data, coords, …[, encoding])

param data

Values for this array. Must be an numpy.ndarray, ndarray like,

all(self[, dim, axis])

Reduce this DataArray’s data by applying all along some dimension(s).

any(self[, dim, axis])

Reduce this DataArray’s data by applying any along some dimension(s).

argmax(self[, dim, axis, skipna])

Reduce this DataArray’s data by applying argmax along some dimension(s).

argmin(self[, dim, axis, skipna])

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(self, \*args, \*\*kwargs)

Assign new attrs to this object.

assign_coords(self[, coords])

Assign new coordinates to this object.

astype(dtype[, order, casting, subok, copy])

Copy of the array, cast to a specified type.

bfill(self, dim, limit)

Fill NaN values by propogating values backward

broadcast_equals(self, other)

Two DataArrays are broadcast equal if they are equal after broadcasting them against each other such that they have the same dimensions.

broadcast_like(self, other, …)

Broadcast this DataArray against another Dataset or DataArray.

chunk(self, chunks, numbers.Number, …)

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(self)

Close any files linked to this object

coarsen(self, dim, int] = None, boundary, …)

Coarsen object.

combine_first(self, other)

Combine two DataArray objects, with union of coordinates.

compute(self, \*\*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(self, deep, data)

Returns a copy of this array.

count(self[, dim, axis])

Reduce this DataArray’s data by applying count along some dimension(s).

cumprod(self[, dim, axis, skipna])

Apply cumprod along some dimension of DataArray.

cumsum(self[, dim, axis, skipna])

Apply cumsum along some dimension of DataArray.

diff(self, dim, n, label)

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

differentiate(self, coord, edge_order, …)

Differentiate the array with the second order accurate central differences.

dot(self, other, dims, Sequence[Hashable], …)

Perform dot product of two DataArrays along their shared dims.

drop(self, labels[, dim, errors])

Drop coordinates or index labels from this DataArray.

dropna(self, dim, how, thresh)

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

equals(self, other)

True if two DataArrays have the same dimensions, coordinates and values; otherwise False.

expand_dims(self, dim, Hashable, …[, axis])

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

ffill(self, dim, limit)

Fill NaN values by propogating values forward

fillna(self, 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, sparse)

Convert a pandas.Series into an xarray.DataArray.

get_axis_num(self, dim, Iterable[Hashable]])

Return axis number(s) corresponding to dimension(s) in this array.

get_index(self, key)

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

groupby(self, group, squeeze, restore_coord_dims)

Returns a GroupBy object for performing grouped operations.

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

Returns a GroupBy object for performing grouped operations.

head(self, indexers, int], int] = None, …)

Return a new DataArray whose data is given by the the first n values along the specified dimension(s).

identical(self, other)

Like equals, but also checks the array name and attributes, and attributes on all coordinates.

integrate(self, dim, Sequence[Hashable]], …)

integrate the array with the trapezoidal rule.

interp(self, coords, Any] = None, method, …)

Multidimensional interpolation of variables.

interp_like(self, other, …)

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

interpolate_na(self[, dim])

Interpolate values according to different methods.

isel(self, indexers, Any] = None, drop, …)

Return a new DataArray whose data is given by integer indexing along the specified dimension(s).

isin(self, test_elements)

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

isnull(self, \*args, \*\*kwargs)

item(*args)

Copy an element of an array to a standard Python scalar and return it.

load(self, \*\*kwargs)

Manually trigger loading of this array’s data from disk or a remote source into memory and return this array.

map_blocks(self, func, args, kwargs, Any] = None)

Apply a function to each chunk of this DataArray.

max(self[, dim, axis, skipna])

Reduce this DataArray’s data by applying max along some dimension(s).

mean(self[, dim, axis, skipna])

Reduce this DataArray’s data by applying mean along some dimension(s).

median(self[, dim, axis, skipna])

Reduce this DataArray’s data by applying median along some dimension(s).

min(self[, dim, axis, skipna])

Reduce this DataArray’s data by applying min along some dimension(s).

notnull(self, \*args, \*\*kwargs)

persist(self, \*\*kwargs)

Trigger computation in constituent dask arrays

pipe(self, func, ~T], Tuple[Callable[…, …)

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

prod(self[, dim, axis, skipna])

Reduce this DataArray’s data by applying prod along some dimension(s).

quantile(self, q, dim, Sequence[Hashable], …)

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

rank(self, dim, pct, keep_attrs)

Ranks the data.

reduce(self, func, Any], dim, Hashable, …)

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

reindex(self, indexers, Any] = None, method)

Conform this object onto the indexes of another object, filling in missing values with fill_value.

reindex_like(self, other, …[, tolerance, …])

Conform this object onto the indexes of another object, filling in missing values with fill_value.

rename(self, new_name_or_name_dict, …)

Returns a new DataArray with renamed coordinates or a new name.

reorder_levels(self, dim_order, …)

Rearrange index levels using input order.

resample(self, indexer, str] = None[, …])

Returns a Resample object for performing resampling operations.

reset_coords(self, names, Hashable, …)

Given names of coordinates, reset them to become variables.

reset_index(self, dims_or_levels, …)

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

roll(self, shifts, int] = None, roll_coords, …)

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

rolling(self, dim, int] = None, min_periods, …)

Rolling window object.

rolling_exp(self, window, int] = None, …)

Exponentially-weighted moving window.

round(self, \*args, \*\*kwargs)

searchsorted(v[, side, sorter])

Find indices where elements of v should be inserted in a to maintain order.

sel(self, indexers, Any] = None, method[, …])

Return a new DataArray whose data is given by selecting index labels along the specified dimension(s).

set_index(self, indexes, Union[Hashable, …)

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

shift(self, shifts, int] = None, fill_value, …)

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

sortby(self, variables, ForwardRef, …)

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

squeeze(self, dim, Iterable[Hashable], …)

Return a new object with squeezed data.

stack(self, dimensions, …)

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

std(self[, dim, axis, skipna])

Reduce this DataArray’s data by applying std along some dimension(s).

sum(self[, dim, axis, skipna])

Reduce this DataArray’s data by applying sum along some dimension(s).

swap_dims(self, dims_dict, Hashable])

Returns a new DataArray with swapped dimensions.

tail(self, indexers, int], int] = None, …)

Return a new DataArray whose data is given by the the last n values along the specified dimension(s).

thin(self, indexers, int], int] = None, …)

Return a new DataArray whose data is given by each n value along the specified dimension(s).

to_cdms2(self)

Convert this array into a cdms2.Variable

to_dataframe(self, name)

Convert this array and its coordinates into a tidy pandas.DataFrame.

to_dataset(self, dim, \*, name)

Convert a DataArray to a Dataset.

to_dict(self, data)

Convert this xarray.DataArray into a dictionary following xarray naming conventions.

to_index(self)

Convert this variable to a pandas.Index.

to_iris(self)

Convert this array into a iris.cube.Cube

to_masked_array(self, copy)

Convert this array into a numpy.ma.MaskedArray

to_netcdf(self, \*args, \*\*kwargs)

Write DataArray contents to a netCDF file.

to_pandas(self)

Convert this array into a pandas object with the same shape.

to_series(self)

Convert this array into a pandas.Series.

to_unstacked_dataset(self, dim[, level])

Unstack DataArray expanding to Dataset along a given level of a stacked coordinate.

transpose(self, \*dims, transpose_coords)

Return a new DataArray object with transposed dimensions.

unify_chunks(self)

Unify chunk size along all chunked dimensions of this DataArray.

unstack(self, dim, Sequence[Hashable], …)

Unstack existing dimensions corresponding to MultiIndexes into multiple new dimensions.

var(self[, dim, axis, skipna])

Reduce this DataArray’s data by applying var along some dimension(s).

where(self, cond[, other])

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

Mapping 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 for DataArray’s

real

shape

size

sizes

Ordered mapping from dimension names to lengths.

str

Vectorized string functions for string-like arrays.

values

The array’s data as a numpy.ndarray

variable

Low level interface to the Variable object for this DataArray.