MCPcopy Index your code
hub / github.com/pydata/xarray / _set_sparse_data_from_dataframe

Method _set_sparse_data_from_dataframe

xarray/core/dataset.py:7331–7362  ·  view source on GitHub ↗
(
        self, idx: pd.Index, arrays: list[tuple[Hashable, np.ndarray]], dims: tuple
    )

Source from the content-addressed store, hash-verified

7329 return self._to_dataframe(ordered_dims=ordered_dims)
7330
7331 def _set_sparse_data_from_dataframe(
7332 self, idx: pd.Index, arrays: list[tuple[Hashable, np.ndarray]], dims: tuple
7333 ) -> None:
7334 from sparse import COO
7335
7336 coords: np.ndarray[tuple[int, int], np.dtype[np.signedinteger]]
7337 if isinstance(idx, pd.MultiIndex):
7338 coords = np.stack([np.asarray(code) for code in idx.codes], axis=0)
7339 is_sorted = idx.is_monotonic_increasing
7340 shape = tuple(lev.size for lev in idx.levels)
7341 else:
7342 coords = np.arange(idx.size).reshape(1, -1)
7343 is_sorted = True
7344 shape = (idx.size,)
7345
7346 for name, values in arrays:
7347 # In virtually all real use cases, the sparse array will now have
7348 # missing values and needs a fill_value. For consistency, don't
7349 # special case the rare exceptions (e.g., dtype=int without a
7350 # MultiIndex).
7351 dtype, fill_value = xrdtypes.maybe_promote(values.dtype)
7352 values = np.asarray(values, dtype=dtype)
7353
7354 data = COO(
7355 coords,
7356 values,
7357 shape,
7358 has_duplicates=False,
7359 sorted=is_sorted,
7360 fill_value=fill_value,
7361 )
7362 self[name] = (dims, data)
7363
7364 def _set_numpy_data_from_dataframe(
7365 self, idx: pd.Index, arrays: list[tuple[Hashable, np.ndarray]], dims: tuple

Callers 1

from_dataframeMethod · 0.80

Calls 2

arangeMethod · 0.80
stackMethod · 0.45

Tested by

no test coverage detected