8

I've created a Python dask array and I'm trying to modify a slice of the array as follows:

import numpy as np
import dask.array as da

x = np.random.random((20000, 100, 100)) # Create numpy array
dx = da.from_array(x, chunks=(x.shape[0], 10, 10)) # Create dask array from numpy array

dx[:50, :, :] = 0 # Modify a slice of the dask array

Such an attempt to modify the dask array raises the exception:

TypeError: 'Array' object does not support item assignment

Is there a way to modify a dask array slice without raising an exception?

1 Answer 1

5

Currently dask.array does not support item assignment or any other mutation operation.

In the case above I recommend concatenating with zeros

In [1]: import dask.array as da

In [2]: dx = da.random.random((20000 - 50, 100, 100), chunks=(None, 10, 10))

In [3]: z = da.zeros((50, 100, 100), chunks=(50, 10, 10))

In [4]: dx2 = da.concatenate([z, dx], axis=0)

In [5]: dx2
Out[5]: dask.array<concate..., shape=(20000, 100, 100), dtype=float64, chunksize=(50, 10, 10)>

In [6]: (dx2 == 0)[0:100, 0, 0].compute()
Out[6]: 
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False, False], dtype=bool)

The da.where(condition, iftrue, iffalse) function can also be quite useful in working around cases where mutation is often desired.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, MRocklin! This works great! Now consider a case where you have a list of values (e.g., index = [10, 20, 25, 50, 100, 120]) to index the 0th axis of dx, and all elements corresponding to these index values need to be assigned 0. Concatenation wouldn't work in this case, and as far as I can tell, da.where wouldn't work either. Do you know a workaround for this particular case?
Recently dask array mutation (or __setitem__) became supported and the original code just works. See github.com/dask/dask/issues/2000 for some history.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.