Skip to content

Commit b32ebb3

Browse files
committed
Handle structured fill_value parsing
1 parent 0a17e88 commit b32ebb3

File tree

1 file changed

+20
-1
lines changed
  • src/zarr/core/metadata

1 file changed

+20
-1
lines changed

src/zarr/core/metadata/v2.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,24 @@ def parse_metadata(data: ArrayV2Metadata) -> ArrayV2Metadata:
315315
return data
316316

317317

318+
def parse_structured_fill_value(fill_value: Any, dtype: np.dtype[Any]) -> Any:
319+
"""Handle structured dtype/fill value pairs"""
320+
try:
321+
if isinstance(fill_value, (tuple, list)):
322+
fill_value = np.array([fill_value], dtype=dtype)[0]
323+
elif isinstance(fill_value, bytes):
324+
fill_value = np.frombuffer(fill_value, dtype=dtype)[0]
325+
elif isinstance(fill_value, str):
326+
decoded = base64.standard_b64decode(fill_value)
327+
fill_value = np.frombuffer(decoded, dtype=dtype)[0]
328+
else:
329+
fill_value = np.array(fill_value, dtype=dtype)[()]
330+
except Exception as e:
331+
msg = f"Fill_value {fill_value} is not valid for dtype {dtype}."
332+
raise ValueError(msg) from e
333+
return fill_value
334+
335+
318336
def parse_fill_value(fill_value: Any, dtype: np.dtype[Any]) -> Any:
319337
"""
320338
Parse a potential fill value into a value that is compatible with the provided dtype.
@@ -332,8 +350,9 @@ def parse_fill_value(fill_value: Any, dtype: np.dtype[Any]) -> Any:
332350
"""
333351

334352
if fill_value is None or dtype.hasobject:
335-
# no fill value
336353
pass
354+
elif dtype.fields is not None:
355+
fill_value = parse_structured_fill_value(fill_value, dtype)
337356
elif not isinstance(fill_value, np.void) and fill_value == 0:
338357
# this should be compatible across numpy versions for any array type, including
339358
# structured arrays

0 commit comments

Comments
 (0)