blosc2.schunk.SChunk.__setitem__#

SChunk.__setitem__(key: int | slice, value: object) None#

Set slice to value.

Parameters:
  • key (int or slice) – The index of the slice to update. Note that step parameter is not honored.

  • value (bytes-like object) – An object supporting the Buffer Protocol used to fill the slice.

Returns:

out

Return type:

None

Raises:
  • ValueError – If the object cannot be modified. If the size to get is negative. If there is not enough space in value to update the slice. If start is greater than the number of items in the SChunk.

  • RunTimeError – If a problem is detected.

  • IndexError – If step is not 1.

Notes

This method can also be used to append new data if key.stop is greater than the number of items in the SChunk.

Examples

>>> import blosc2
>>> import numpy as np
>>> nchunks = 4
>>> chunk_size = 200 * 1000 * 4
>>> data = np.arange(nchunks * chunk_size // 4, dtype=np.int32)
>>> cparams = blosc2.CParams(typesize=4)
>>> schunk = blosc2.SChunk(data=data, cparams=cparams)
>>> # Create a new array of values to update the slice (values from 1000 to 1999 multiplied by 2)
>>> start_ = 1000
>>> stop = 2000
>>> new_values = np.arange(start_, stop, dtype=np.int32) * 2
>>> schunk[start_:stop] = new_values
>>> # Retrieve the updated slice using the slicing syntax
>>> retrieved_slice = np.frombuffer(schunk[start_:stop], dtype=np.int32)
>>> f"First 10 values of the updated slice: {retrieved_slice[:10]}"
>>> f"Last 10 values of the updated slice: {retrieved_slice[-10:]}"
First 10 values of the updated slice: [2000 2002 2004 2006 2008 2010 2012 2014 2016 2018]
Last 10 values of the updated slice: [3980 3982 3984 3986 3988 3990 3992 3994 3996 3998]