Numpy support

ndtypes-python currently has limited NumPy interoperability.

from ndtypes import ndt
import numpy as np

Dtype conversion

Convert a NumPy dtype to a Datashape dtype

>>> x = np.zeros(dtype="U64", shape=[10,2])
>>> ndt.type(x.dtype)
ndt.type("fixed_string[64, 'utf32']")

# Aligned structs are supported
>>> dt = np.dtype([('foo', 'i4'),('bar', 'f4'), ('baz', 'S10')], align=True)
>>> x = np.array([(1,2.,'Hello'), (2,3.,"World")], dtype=dt)
>>> ndt.type(x.dtype)
ndt.type("{foo: int32, bar: float32, baz: fixed_string[10, 'ascii']}")

Convert a Datashape dtype to a NumPy dtype

>>> t = ndt.type("int64")
>>> t.as_numpy()

# Structs are currently not supported in this direction
>>> t = ndt.type("{foo: int32, bar: float32}")
>>> t.as_numpy()
TypeError: cannot convert dynd type {foo: int32, bar: float32} into a Numpy dtype

Extract the shape from a Datashape array

>>> t = ndt.type("10 * 20 * int64")
>>> t.shape
(10, 20)