Skip to content

Regression in support for indexing into structured arrays within a Zarr store (v2 -> v3) #3983

@rachitk

Description

@rachitk

Zarr version

v3.2.1

Numcodecs version

0.16.5

Python Version

3.12.7

Operating System

Linux

Installation

Pip into virtual environment

Description

In Zarr-Python 2, saving a structured array into a Zarr store enabled one to load the store and index into the structured array to load a specific field of the structured array. This same functionality does not appear to work in Zarr-Python 3, instead returning an error similar to that of:

TypeError: Cannot cast array data from dtype([('A', '<U8'), ('B', '<i4')]) to dtype('<i4') according to the rule 'unsafe'

This appears to be a regression from something that was supported in Zarr-Python 2. I know that structured array support was pending for a while in Zarr-Python 3, but I was under the impression that #2874 had fixed this (or at least achieved feature parity with Zarr-Python 2 in this regard) per the discussion in #2134.

(As a note, I'm not as involved in the Zarr ecosystem but am hoping to use it more heavily in my future projects. Apologies if any of my terminology is not exact when it comes to describing Zarr stores, groups, and arrays.)

Steps to reproduce

# /// script
# requires-python = ">=3.12"
# dependencies = [
#   "zarr@git+https://github.com/zarr-developers/zarr-python.git@main",
#   "numpy",
# ]
# ///
#
# This script automatically imports the development branch of zarr to check for issues

import zarr
import numpy as np

n_samples = 1000

# Structured array example from https://numpy.org/doc/stable/user/basics.rec.html
x = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)],
             dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])

# Save into Zarr (format v2)
zarr_f = zarr.open('test.zarr', mode='w', zarr_format=2)
zarr_struct = zarr_f.create_array('test', data=x)

# errors out with " TypeError: Cannot cast array data from dtype([('name', '<U10'), ('age', '<i4'), ('weight', '<f4')]) to dtype('<U10') according to the rule 'unsafe' "
name_array = zarr_struct['name']

# zarr.print_debug_info()

Additional output

zarr.print_debug_info() output:

platform: Linux-5.14.0-427.110.1.el9_4.x86_64-x86_64-with-glibc2.34
python: 3.12.7
zarr: 3.2.1

**Required dependencies:**
packaging: 26.2
numpy: 2.1.2
numcodecs: 0.16.5
typing_extensions: 4.15.0
donfig: 0.8.1.post1

**Optional dependencies:**

fsspec: 2026.4.0
numcodecs: 0.16.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPotential issues with the zarr-python library

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions