Skip to content

Commit c4883cb

Browse files
committed
Add support for dtypes that are ndimensional
1 parent 7045763 commit c4883cb

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/blosc2/blosc2_ext.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,7 @@ cdef class NDArray:
23912391
str_dtype = bytes_dtype.decode("utf-8")
23922392
try:
23932393
dtype = np.dtype(str_dtype)
2394-
except TypeError:
2394+
except (ValueError, TypeError):
23952395
dtype = np.dtype(ast.literal_eval(str_dtype))
23962396
self._dtype = dtype
23972397
return dtype

tests/ndarray/test_zeros.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,25 @@ def test_large_typesize(shape, typesize, asarray):
135135
else:
136136
b = blosc2.zeros(shape, dtype=dtype)
137137
assert np.array_equal(b[0], a[0])
138+
139+
140+
@pytest.mark.parametrize(
141+
"dtype",
142+
[
143+
np.dtype(("<f8", (10,))),
144+
np.dtype(("<i8", (10,))),
145+
np.dtype(("<i4,>f4", (10,))),
146+
],
147+
)
148+
def test_nd_dtype(dtype):
149+
# Test that the dtype is correctly set for a 1D array with a nested dtype
150+
a = blosc2.zeros((1,), dtype=dtype)
151+
assert a.dtype == dtype
152+
b = np.zeros((1,), dtype=dtype)
153+
if dtype.base.fields: # ("<i8,>f4", (10,))
154+
# Check values by converting to a dtype without a structure
155+
a2 = a[:].view(dtype=np.int8)
156+
b2 = b[:].view(dtype=np.int8)
157+
np.testing.assert_equal(a2, b2)
158+
else:
159+
np.testing.assert_equal(a[:], b)

0 commit comments

Comments
 (0)