@@ -4261,6 +4261,9 @@ def __setitem__(
42614261 key_ , mask = process_key (key , self .shape ) # internally handles key an integer
42624262 if hasattr (value , "shape" ) and value .shape == ():
42634263 value = value .item ()
4264+ value = (
4265+ value if np .isscalar (value ) else blosc2 .as_simpleproxy (value )
4266+ ) # convert to SimpleProxy for e.g. JAX, Tensorflow, PyTorch
42644267
42654268 if builtins .any (isinstance (k , (list , np .ndarray )) for k in key_ ): # fancy indexing
42664269 _slice = ndindex .ndindex (key_ ).expand (
@@ -4284,20 +4287,17 @@ def __setitem__(
42844287 return self ._get_set_nonunit_steps ((start , stop , step , mask ), value = value )
42854288
42864289 shape = [sp - st for sp , st in zip (stop , start , strict = False )]
4287- if isinstance (value , NDArray ):
4288- value = value [... ] # convert to numpy
4289- if np .isscalar (value ):
4290+ if isinstance (value , blosc2 . Operand ): # handles SimpleProxy, NDArray, LazyExpr etc.
4291+ value = value [() ] # convert to numpy
4292+ if np .isscalar (value ) or value . shape == () :
42904293 value = np .full (shape , value , dtype = self .dtype )
4291- elif isinstance (value , np .ndarray ): # handles decompressed NDArray too
4292- if value .dtype != self .dtype :
4293- try :
4294- value = value .astype (self .dtype )
4295- except ComplexWarning :
4296- # numexpr type inference can lead to unnecessary type promotions
4297- # when using complex functions (e.g. conj) with real arrays
4298- value = value .real .astype (self .dtype )
4299- if value .shape == ():
4300- value = np .full (shape , value , dtype = self .dtype )
4294+ if value .dtype != self .dtype : # handles decompressed NDArray too
4295+ try :
4296+ value = value .astype (self .dtype )
4297+ except ComplexWarning :
4298+ # numexpr type inference can lead to unnecessary type promotions
4299+ # when using complex functions (e.g. conj) with real arrays
4300+ value = value .real .astype (self .dtype )
43014301
43024302 return super ().set_slice ((start , stop ), value )
43034303
0 commit comments