MCPcopy
hub / github.com/pydata/xarray / to_dataarray

Method to_dataarray

xarray/core/dataset.py:7147–7182  ·  view source on GitHub ↗

Convert this dataset into an xarray.DataArray The data variables of this dataset will be broadcast against each other and stacked along the first axis of the new array. All coordinates of this dataset will remain coordinates. Parameters ---------- di

(
        self, dim: Hashable = "variable", name: Hashable | None = None
    )

Source from the content-addressed store, hash-verified

7145 return data
7146
7147 def to_dataarray(
7148 self, dim: Hashable = "variable", name: Hashable | None = None
7149 ) -> DataArray:
7150 """Convert this dataset into an xarray.DataArray
7151
7152 The data variables of this dataset will be broadcast against each other
7153 and stacked along the first axis of the new array. All coordinates of
7154 this dataset will remain coordinates.
7155
7156 Parameters
7157 ----------
7158 dim : Hashable, default: "variable"
7159 Name of the new dimension.
7160 name : Hashable or None, optional
7161 Name of the new data array.
7162
7163 Returns
7164 -------
7165 array : xarray.DataArray
7166 """
7167 from xarray.core.dataarray import DataArray
7168
7169 data_vars = [self.variables[k] for k in self.data_vars]
7170 broadcast_vars = broadcast_variables(*data_vars)
7171 data = duck_array_ops.stack([b.data for b in broadcast_vars], axis=0)
7172
7173 dims = (dim,) + broadcast_vars[0].dims
7174 variable = Variable(dims, data, self.attrs, fastpath=True)
7175
7176 coords = {k: v.variable for k, v in self.coords.items()}
7177 indexes = dict(self._indexes)
7178 new_dim_index = PandasIndex(list(self.data_vars), dim)
7179 indexes[dim] = new_dim_index
7180 coords.update(new_dim_index.create_variables())
7181
7182 return DataArray._construct_direct(variable, coords, name, indexes)
7183
7184 def to_array(
7185 self, dim: Hashable = "variable", name: Hashable | None = None

Calls 8

create_variablesMethod · 0.95
broadcast_variablesFunction · 0.90
VariableClass · 0.90
PandasIndexClass · 0.90
itemsMethod · 0.80
stackMethod · 0.45
updateMethod · 0.45
_construct_directMethod · 0.45