Indexing and Manipulation Functions and Utilities¶
The following functions are useful for performing indexing and other associated operations.
|
Return the indices that would sort the array. |
|
Broadcast an array to a new shape. |
|
Concatenate a list of arrays along a specified axis. |
|
Return number of nonzero values along axes. |
|
Expand the shape of an array by adding new axes at the specified positions. |
|
Returns coordinate matrices from coordinate vectors. |
|
Return a sorted array following the specified order. |
|
Remove single-dimensional entries from the shape of the array. |
|
Stack multiple arrays, creating a new axis. |
|
Return elements selected by integer indices. |
|
Returns elements of an array along an axis. |
- blosc2.argsort(array: Array, order: str | list[str] | None = None, **kwargs: Any) NDArray[source]¶
Return the indices that would sort the array.
This mirrors
numpy.argsort()for 1-D arrays. Plain arrays sort by their values. Structured arrays sort byorderwhen provided, or by their dtype field order whenorder=None. Expression orders such as"abs(x)"are also supported when a matchingfullexpression index exists.- Parameters:
array¶ (
blosc2.Array) – The 1-D array to be ordered.order¶ (str, list of str, optional) – Primary and optional secondary order keys for structured arrays. When omitted, NumPy’s default record order is used for structured dtypes and the array values themselves are used for plain dtypes.
kwargs¶ (Any, optional) – Keyword arguments that are supported by the
empty()constructor.
- Returns:
out – The ordered logical positions as
int64.- Return type:
Notes
When the primary order key has a matching
fullfield or expression index, the permutation is returned directly from that index in ascending stable order. Secondary keys refine ties after the primary indexed order. Without a matchingfullindex,argsort()falls back to materializing the input values and delegating ordering tonumpy.argsort().The result is always a new array materialization. For persistent inputs, the returned permutation is in memory by default; pass storage kwargs such as
urlpath(and typicallymode="w") if the permutation should also be persisted on disk.
- blosc2.broadcast_to(arr: Array, shape: tuple[int, ...]) NDArray[source]¶
Broadcast an array to a new shape. Warning: Computes a lazyexpr, so probably a bit suboptimal
- Parameters:
arr¶ (blosc2.Array) – The array to broadcast.
shape¶ (tuple) – The shape of the desired array.
- Returns:
broadcast (NDArray)
A new array with the given shape.
- blosc2.concat(arrays: list[NDArray], /, axis=0, **kwargs: Any) NDArray[source]¶
Concatenate a list of arrays along a specified axis.
- Parameters:
- Returns:
out – A new NDArray containing the concatenated data.
- Return type:
Examples
>>> import blosc2 >>> import numpy as np >>> arr1 = blosc2.arange(0, 5, dtype=np.int32) >>> arr2 = blosc2.arange(5, 10, dtype=np.int32) >>> result = blosc2.concat([arr1, arr2]) >>> print(result[:]) [0 1 2 3 4 5 6 7 8 9]
- blosc2.count_nonzero(ndarr: blosc2.Array, axis: int | Sequence[int] | None = None) int[source]¶
Return number of nonzero values along axes.
- Parameters:
- Returns:
out – Number of nonzero elements.
- Return type:
int
References
- blosc2.expand_dims(array: NDArray, axis=0) NDArray[source]¶
Expand the shape of an array by adding new axes at the specified positions.
- blosc2.meshgrid(*arrays: blosc2.Array, indexing: str = 'xy') Sequence[NDArray][source]¶
Returns coordinate matrices from coordinate vectors.
- Parameters:
*arrays¶ (blosc2.Array) – An arbitrary number of one-dimensional arrays representing grid coordinates. Each array should have the same numeric data type.
indexing¶ (str) – Cartesian ‘xy’ or matrix ‘ij’ indexing of output. If provided zero or one one-dimensional vector(s) the indexing keyword is ignored. Default: ‘xy’.
- Returns:
out – List of N arrays, where N is the number of provided one-dimensional input arrays, with same dtype. For N one-dimensional arrays having lengths Ni = len(xi),
if matrix indexing ij, then each returned array has shape (N1, N2, N3, …, Nn).
if Cartesian indexing xy, then each returned array has shape (N2, N1, N3, …, Nn).
- Return type:
(List[NDArray])
- blosc2.sort(array: Array, order: str | list[str] | None = None, **kwargs: Any) NDArray[source]¶
Return a sorted array following the specified order.
This is only valid for 1-dim structured arrays.
- Parameters:
array¶ (
blosc2.Array) – The (structured) array to be sorted.order¶ (str, list of str, optional) – Specifies which fields to compare first, second, etc. A single field can be specified as a string. The primary order key may also be an indexed expression such as
"abs(x)"when a matchingfullexpression index exists. Not all fields need to be specified, only the ones by which the array is to be sorted.kwargs¶ (Any, optional) – Keyword arguments that are supported by the
empty()constructor.
- Returns:
out – The sorted array.
- Return type:
Notes
If the primary order key has a matching
fullfield or expression index, rows are gathered directly in ascending stable index order. Secondary keys refine ties after the primary indexed order. Field-based orders without a matching full index fall back to a scan-plus-sort path.Sorting never mutates the input array in place. The result is always a new array materialization. For persistent inputs, the sorted rows are returned as a new in-memory NDArray by default; pass storage kwargs such as
urlpath(and typicallymode="w") if the sorted output should also be persisted on disk.
- blosc2.squeeze(x: Array, axis: int | Sequence[int]) NDArray[source]¶
Remove single-dimensional entries from the shape of the array.
This method modifies the array in-place.
- Parameters:
- Returns:
out – An output array having the same data type and elements as x.
- Return type:
Examples
>>> import blosc2 >>> shape = [1, 23, 1, 11, 1] >>> # Create an array >>> b = blosc2.full(shape, 2**30) >>> b.shape (1, 23, 1, 11, 1) >>> # Squeeze the array >>> blosc2.squeeze(b) >>> b.shape (23, 11)
- blosc2.stack(arrays: list[NDArray], axis=0, **kwargs: Any) NDArray[source]¶
Stack multiple arrays, creating a new axis.
- Parameters:
- Returns:
out – A new NDArray containing the stacked data.
- Return type:
Examples
>>> import blosc2 >>> import numpy as np >>> arr1 = blosc2.arange(0, 6, dtype=np.int32, shape=(2,3)) >>> arr2 = blosc2.arange(6, 12, dtype=np.int32, shape=(2,3)) >>> result = blosc2.stack([arr1, arr2]) >>> print(result.shape) (2, 2, 3)
- blosc2.take(x: Array, indices: Array, axis: int | None = None)[source]¶
Return elements selected by integer indices.
For array inputs, this follows the Array API
takeshape rules: whenaxisisNone, x is conceptually flattened and the output shape isindices.shape; otherwise the indexed axis is replaced byindices.shape. ForCTableandColumninputs, indices select logical rows/values andaxisis not supported.- Parameters:
x¶ (blosc2.Array, CTable, Column, or array-like) – Input object.
NDArrayinputs return anNDArray;CTableinputs return aCTable;Columninputs return aColumn. Other array-like inputs are converted to a Blosc2NDArrayresult.indices¶ (array-like) – Integer indices. Negative indices are normalized relative to the selected axis (or to the flattened array when
axisisNone). For array inputs, indices may have any shape.axis¶ (int | None) – Axis over which to select values for array inputs. If
None, the input array is flattened before selection. Must beNoneforCTableandColumninputs.
- Returns:
out – Selected values, preserving the container type for
NDArray,CTableandColumninputs.- Return type:
- blosc2.take_along_axis(x: Array, indices: Array, axis: int = -1) NDArray[source]¶
Returns elements of an array along an axis.
- Parameters:
x¶ (blosc2.Array) – Input array. Should have one or more dimensions (axes).
indices¶ (array-like) – Array indices. The array must have same number of dimensions as x and have an integer data type.
axis¶ (int) – Axis over which to select values. Default: -1.
- Returns:
out – Selected indices of x.
- Return type: