🍾 Xarray is now 10 years old! 🎉

xarray.Variable

xarray.Variable#

class xarray.Variable(dims, data, attrs=None, encoding=None, fastpath=False)[source]#

A netcdf-like variable consisting of dimensions, data and attributes which describe a single Array. A single Variable object is not fully described outside the context of its parent Dataset (if you want such a fully described object, use a DataArray instead).

The main functional difference between Variables and numpy arrays is that numerical operations on Variables implement array broadcasting by dimension name. For example, adding an Variable with dimensions (‘time’,) to another Variable with dimensions (‘space’,) results in a new Variable with dimensions (‘time’, ‘space’). Furthermore, numpy reduce operations like mean or sum are overwritten to take a “dimension” argument instead of an “axis”.

Variables are light-weight objects used as the building block for datasets. They are more primitive objects, so operations with them provide marginally higher performance than using DataArrays. However, manipulating data in the form of a Dataset or DataArray should almost always be preferred, because they can use more complete metadata in context of coordinate labels.

__init__(dims, data, attrs=None, encoding=None, fastpath=False)[source]#
Parameters:
  • dims (str or sequence of str) – Name(s) of the 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.

  • data (array_like) – Data array which supports numpy-like data access.

  • attrs (dict-like or None, optional) – Attributes to assign to the new variable. If None (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’ and ‘dtype’. Well-behaved code to serialize a Variable should ignore unrecognized encoding items.

Methods

__init__(dims, data[, attrs, encoding, fastpath])

Parameters:
  • dims (str or sequence of str) -- Name(s) of the the data dimension(s). Must be either a string (only

all([dim])

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

any([dim])

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

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

Index or indices of the maximum of the Variable over one or more dimensions.

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

Index or indices of the minimum of the Variable over one or more dimensions.

argsort([axis, kind, order])

Returns the indices that would sort this array.

as_numpy()

Coerces wrapped data into a numpy array, returning a Variable.

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

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

broadcast_equals(other[, equiv])

True if two Variables have the values after being broadcast against each other; otherwise False.

broadcast_to([dim])

Broadcast the NamedArray to a new shape.

chunk([chunks, name, lock, inline_array, ...])

Coerce this array's data into a dask array with the given chunks.

clip([min, max])

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

coarsen(windows, func[, boundary, side, ...])

Apply reduction function.

coarsen_reshape(windows, boundary, side)

Construct a reshaped-array for coarsen

compute(**kwargs)

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

concat(variables[, dim, positions, ...])

Concatenate variables along a new or existing dimension.

conj()

Complex-conjugate all elements.

conjugate()

Return the complex conjugate, element-wise.

copy([deep, data])

Returns a copy of this object.

count([dim])

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

cumprod([dim, skipna])

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

cumsum([dim, skipna])

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

drop_encoding()

Return a new Variable without encoding.

equals(other[, equiv])

True if two Variables have the same dimensions and values; otherwise False.

expand_dims([dim])

Expand the dimensions of the NamedArray.

fillna(value)

get_axis_num(dim)

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

identical(other[, equiv])

Like equals, but also checks attributes.

isel([indexers, missing_dims])

Return a new array indexed along the specified dimension(s).

isnull([keep_attrs])

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

item(*args)

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

load(**kwargs)

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

max([dim, skipna])

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

mean([dim, skipna])

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

median([dim, skipna])

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

min([dim, skipna])

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

no_conflicts(other[, equiv])

True if the intersection of two Variable's non-null data is equal; otherwise false.

notnull([keep_attrs])

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

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

Return a new Variable with padded data.

permute_dims(*dim[, missing_dims])

Return a new object with transposed dimensions.

prod([dim, skipna, min_count])

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

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

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

rank(dim[, pct])

Ranks the data.

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

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

reset_encoding()

roll([shifts])

Return a new Variable with rolld data.

rolling_window(dim, window, window_dim[, ...])

Make a rolling_window along dim and add a new_dim to the last place.

round(*args, **kwargs)

Round an array to the given number of decimals.

searchsorted(v[, side, sorter])

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

set_dims(dims[, shape])

Return a new variable with given set of dimensions.

shift([shifts, fill_value])

Return a new Variable with shifted data.

squeeze([dim])

Return a new object with squeezed data.

stack([dimensions])

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

std([dim, skipna, ddof])

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

sum([dim, skipna, min_count])

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

to_base_variable()

Return this variable as a base xarray.Variable

to_coord()

to_coord has been deprecated.

to_dict([data, encoding])

Dictionary representation of variable.

to_index()

Convert this variable to a pandas.Index

to_index_variable()

Return this variable as an xarray.IndexVariable

to_numpy()

Coerces wrapped data to numpy and returns a numpy.ndarray

to_variable()

to_variable has been deprecated.

transpose(*dims[, missing_dims])

Return a new Variable object with transposed dimensions.

unstack([dimensions])

Unstack an existing dimension into multiple new dimensions.

var([dim, skipna, ddof])

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

where(cond[, other])

Attributes

T

Return a new object with transposed dimensions.

attrs

Dictionary of local attributes on this NamedArray.

chunks

Tuple of block lengths for this NamedArray's data, in order of dimensions, or None if the underlying data is not a dask array.

chunksizes

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

data

The Variable's data as an array.

dims

Tuple of dimension names with which this NamedArray is associated.

dtype

Data-type of the array’s elements.

encoding

Dictionary of encodings on this variable.

imag

The imaginary part of the variable.

nbytes

Total bytes consumed by the elements of the data array.

ndim

Number of array dimensions.

real

The real part of the variable.

shape

Get the shape of the array.

size

Number of elements in the array.

sizes

Ordered mapping from dimension names to lengths.

values

The variable's data as a numpy.ndarray