Skip to content

setitem with repeated indices is not guaranteed in practice #2855

Description

@dcherian

Zarr version

v2.18.3, 3.0.2 and 3.0.3

Numcodecs version

?

Python Version

?

Operating System

linux & windows

Installation

either

Description

behaviour of setitem with repeated indices depends on platform. The example below succeeds on Mac and fails on ubuntu, windows with numpy 2.2. It succeeds in all CI environments with numpy 1.25.

I can reproduce on zarr v2.18.4, v3.0.2 on linux.

I say we disallow this

Steps to reproduce

import zarr
import numpy as np

array = zarr.array(data=np.zeros((4,)), chunks=(1,))
indexer = np.array([-1, -1, 0, 0])
array.oindex[(indexer,)] = [0, 1, 2, 3]
np.testing.assert_array_equal(array[:], np.array([3, 0, 0, 1]))

fails on windows & linux, numpy=2.2 in CI.

cc @LDeakin @ilan-gold

Activity

  1. added
    bugPotential issues with the zarr-python library
    on Feb 21, 2025
  2. d-v-b commented on Feb 24, 2025

    @d-v-b
    Contributor

    is numpy's behavior also platform-dependent? If not, then I think we should copy numpy here. On my OSX machine, numpy uses the last value assigned:

    >>> x = np.arange(4)
    >>> x[np.ones(10, dtype='int')] = np.arange(10)
    >>> x
    array([0, 9, 2, 3]) # value at index 1 is set to the last value of np.arange(10)

    We probably need to transform the input such that it defines an indexing operation that only applies 1 change to each output index.

  3. LDeakin commented on Feb 24, 2025

    @LDeakin
    Member

    We probably need to transform the input such that it defines an indexing operation that only applies 1 change to each output index.

    That'd be perfect

  4. dcherian commented on Feb 24, 2025

    @dcherian
    ContributorAuthor

    numpy behaviour is always consistent and pretty well-defined since it's serial.

    I don't understand what's going on here. transforming the input does seem sensible.

  5. d-v-b commented on Nov 18, 2026

    @d-v-b
    Contributor

    following up on this because we have a few open PRs about it:

    • numpy documents its write order as an implementation detail that could change: https://numpy.org/devdocs/user/basics.indexing.html#detailed-notes. I think we want to make the same claim for zarr, because that gives us flexibility. Users who want stable behavior should rewrite their indexing expressions to be unambiguous. This needs to be documented.
    • the platform dependence is due to varying implementations of argsort, which are unstable sorts on some platforms. a stable sort keeps things consistent across platforms
    • the de-duplication I proposed upthread is more expensive perf-wise than keeping user input as-is and following a heuristic like last-write wins, so I don't think rewriting the indexing expression is worth it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPotential issues with the zarr-python library

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions