Skip to content

NumPy

Strategies related to NumPy in the context of Awkward Array.

These strategies are related to the section of Awkward Array User Guide "How to convert to/from NumPy".

Functions:

Name Description
supported_dtype_names

Strategy for names of NumPy dtypes supported by Awkward Array.

supported_dtypes

Strategy for NumPy dtypes supported by Awkward Array.

numpy_dtypes

Strategy for dtypes (simple or array) supported by Awkward Array.

numpy_arrays

Strategy for NumPy arrays from which Awkward Arrays can be created.

from_numpy

Strategy for Awkward Arrays created from NumPy arrays.

supported_dtype_names

supported_dtype_names() -> st.SearchStrategy[str]

Strategy for names of NumPy dtypes supported by Awkward Array.

Examples:

>>> supported_dtype_names().example()
'...'

supported_dtypes

supported_dtypes() -> st.SearchStrategy[np.dtype]

Strategy for NumPy dtypes supported by Awkward Array.

Examples:

>>> supported_dtypes().example()
dtype(...)

numpy_dtypes

numpy_dtypes(
    *,
    dtype: dtype | SearchStrategy[dtype] | None = None,
    allow_array: bool = True,
    max_size: int = 5,
) -> st.SearchStrategy[np.dtype]

Strategy for dtypes (simple or array) supported by Awkward Array.

Examples of simple dtypes are dtype('int32'), dtype('float64')

Array dtypes include ([('f0', 'i4'), ('f1', 'f8')]). They are dtypes of structured NumPy arrays.

Parameters:

Name Type Description Default
dtype dtype | SearchStrategy[dtype] | None

A simple dtype or a strategy for simple dtypes for determining the type of array elements. If None, any supported simple dtype is used.

None
allow_array bool

Generate only simple dtypes if False, else array dtypes as well.

True
max_size int

Maximum number of scalars in a structured dtype.

5

Examples:

>>> numpy_dtypes().example()
dtype(...)

numpy_arrays

numpy_arrays(
    draw: DrawFn,
    *,
    dtype: dtype | SearchStrategy[dtype] | None = None,
    allow_structured: bool = True,
    allow_nan: bool = True,
    min_dims: int = 1,
    max_dims: int | None = None,
    min_size: int = 0,
    max_size: int = 10,
    unique: bool = False,
) -> np.ndarray

Strategy for NumPy arrays from which Awkward Arrays can be created.

Parameters:

Name Type Description Default
dtype dtype | SearchStrategy[dtype] | None

A simple dtype or a strategy for simple dtypes for determining the type of array elements. If None, any supported simple dtype is used.

None
allow_structured bool

Generate only simple arrays if False, else structured arrays as well.

True
allow_nan bool

Generate potentially NaN for relevant dtypes if True.

True
min_dims int

Minimum number of dimensions.

1
max_dims int | None

Maximum number of dimensions. If None, auto-derived from max_size.

None
min_size int

Minimum number of scalars in the array. For structured dtypes, each element counts as multiple scalars (one per field).

0
max_size int

Maximum number of scalars in the array. For structured dtypes, each element counts as multiple scalars (one per field).

10
unique bool

Generate arrays whose elements are pairwise distinct if True. Only simple dtypes are generated (no structured dtypes). The achievable size is bounded by the number of distinct values the dtype can represent; when dtype is None, dtypes that cannot supply min_size distinct values (for example bool when min_size > 2) are not drawn. A pinned dtype that cannot satisfy the request raises.

False

Examples:

>>> n = numpy_arrays().example()
>>> ak.from_numpy(n)
<Array ... type='...'>

from_numpy

from_numpy(
    *,
    dtype: dtype | SearchStrategy[dtype] | None = None,
    allow_structured: bool = True,
    allow_nan: bool = True,
    regulararray: bool | None = None,
    max_size: int = 10,
) -> st.SearchStrategy[ak.Array]

Strategy for Awkward Arrays created from NumPy arrays.

Parameters:

Name Type Description Default
dtype dtype | SearchStrategy[dtype] | None

A simple dtype or a strategy for simple dtypes for determining the type of array elements. If None, any supported simple dtype is used.

None
allow_structured bool

Generate only from simple NumPy arrays if False, else from structured NumPy arrays as well.

True
allow_nan bool

Generate potentially NaN for relevant dtypes if True.

True
regulararray bool | None

Passed to ak.from_numpy. If None (default), randomly generates True or False.

None
max_size int

Maximum number of scalars in the array. For structured dtypes, each element counts as multiple scalars (one per field).

10

Examples:

>>> from_numpy().example()
<Array ... type='...'>