MCPcopy Create free account
hub / github.com/dask/dask / stack

Function stack

dask/array/core.py:5505–5617  ·  view source on GitHub ↗

Stack arrays along a new axis Given a sequence of dask arrays, form a new dask array by stacking them along a new dimension (axis=0 by default) Parameters ---------- seq: list of dask.arrays axis: int Dimension along which to align all of the arrays allow_u

(seq, axis=0, allow_unknown_chunksizes=False)

Source from the content-addressed store, hash-verified

5503
5504
5505def stack(seq, axis=0, allow_unknown_chunksizes=False):
5506 """
5507 Stack arrays along a new axis
5508
5509 Given a sequence of dask arrays, form a new dask array by stacking them
5510 along a new dimension (axis=0 by default)
5511
5512 Parameters
5513 ----------
5514 seq: list of dask.arrays
5515 axis: int
5516 Dimension along which to align all of the arrays
5517 allow_unknown_chunksizes: bool
5518 Allow unknown chunksizes, such as come from converting from dask
5519 dataframes. Dask.array is unable to verify that chunks line up. If
5520 data comes from differently aligned sources then this can cause
5521 unexpected results.
5522
5523 Examples
5524 --------
5525
5526 Create slices
5527
5528 >>> import dask.array as da
5529 >>> import numpy as np
5530
5531 >>> data = [da.from_array(np.ones((4, 4)), chunks=(2, 2))
5532 ... for i in range(3)]
5533
5534 >>> x = da.stack(data, axis=0)
5535 >>> x.shape
5536 (3, 4, 4)
5537
5538 >>> da.stack(data, axis=1).shape
5539 (4, 3, 4)
5540
5541 >>> da.stack(data, axis=-1).shape
5542 (4, 4, 3)
5543
5544 Result is a new dask Array
5545
5546 See Also
5547 --------
5548 concatenate
5549 """
5550 from dask.array import wrap
5551
5552 seq = [asarray(a, allow_unknown_chunksizes=allow_unknown_chunksizes) for a in seq]
5553
5554 if not seq:
5555 raise ValueError("Need array(s) to stack")
5556 if not allow_unknown_chunksizes and not all(x.shape == seq[0].shape for x in seq):
5557 idx = first(i for i in enumerate(seq) if i[1].shape != seq[0].shape)
5558 raise ValueError(
5559 "Stacked arrays must have the same shape. The first array had shape "
5560 f"{seq[0].shape}, while array {idx[0] + 1} has shape {idx[1].shape}."
5561 )
5562

Callers 7

argwhereFunction · 0.90
ravel_multi_indexFunction · 0.90
indicesFunction · 0.90
test_stackFunction · 0.90
test_dtype_complexFunction · 0.90
asarrayFunction · 0.70
asanyarrayFunction · 0.70

Calls 10

allFunction · 0.90
meta_from_arrayFunction · 0.90
unify_chunksFunction · 0.85
from_collectionsMethod · 0.80
asarrayFunction · 0.70
ArrayClass · 0.70
concatFunction · 0.50
tokenizeFunction · 0.50
astypeMethod · 0.45
emptyMethod · 0.45

Tested by 2

test_stackFunction · 0.72
test_dtype_complexFunction · 0.72