fields.buffers.core.UnytArrayBuffer.from_array#
- classmethod UnytArrayBuffer.from_array(obj: Any, dtype: Any | None = None, units: Unit | str | None = None, *args, registry: UnitRegistry | None = None, bypass_validation: bool = False, name: str | None = None, **kwargs) UnytArrayBuffer [source]#
Construct a new unit-aware buffer from any array-like input.
This method wraps the input in an
unyt_array
, preserving or applying physical units as needed. It handles input that is already aunyt_array
orunyt_quantity
, a plainnumpy.ndarray
, or any array-like object (e.g., list, tuple) that can be converted vianumpy.array()
.- Parameters:
obj (
array-like
) – The input data to interpret as a buffer. Regardless of the input type, the data will be coerced to aunyt_array
and then wrapped inUnytArrayBuffer
.units (
str
orUnit
, optional) – Physical units to attach to the resulting array. If not provided and obj has attached units, they are preserved. Otherwise defaults to dimensionless.dtype (
data-type
, optional) – Desired data type of the resulting array. If not specified, inferred from input.registry (
UnitRegistry
, optional) – Registry to associate with the units, if applicable.bypass_validation (
bool
, defaultFalse
) – If True, skip unit and value validation (faster but unsafe for malformed input).name (
str
, optional) – Optional name for the array (useful for annotation).**kwargs – Additional keyword arguments passed to
numpy.array()
if the input must be coerced.
- Returns:
A buffer instance wrapping the resulting
unyt_array
.- Return type:
- Raises:
TypeError – If the input cannot be converted into a
unyt_array
.
Notes
Input Type Behavior:
If obj is already a
unyt_array
orunyt_quantity
, it is forwarded directly (converted if units is specified).If obj is a
numpy.ndarray
, it is passed tounyt_array
with all metadata.Otherwise, the input is cast via
numpy.array()
and then wrapped.