MCPcopy
hub / github.com/numpy/numpy / __setitem__

Method __setitem__

numpy/ma/core.py:3406–3473  ·  view source on GitHub ↗

x.__setitem__(i, y) <==> x[i]=y Set item described by index. If value is masked, masks those locations.

(self, indx, value)

Source from the content-addressed store, hash-verified

3404 # correct warnings (as is typical also in masked calculations).
3405 @np.errstate(over='ignore', invalid='ignore')
3406 def __setitem__(self, indx, value):
3407 """
3408 x.__setitem__(i, y) <==> x[i]=y
3409
3410 Set item described by index. If value is masked, masks those
3411 locations.
3412
3413 """
3414 if self is masked:
3415 raise MaskError('Cannot alter the masked element.')
3416 _data = self._data
3417 _mask = self._mask
3418 if isinstance(indx, str):
3419 _data[indx] = value
3420 if _mask is nomask:
3421 self._mask = _mask = make_mask_none(self.shape, self.dtype)
3422 _mask[indx] = getmask(value)
3423 return
3424
3425 _dtype = _data.dtype
3426
3427 if value is masked:
3428 # The mask wasn't set: create a full version.
3429 if _mask is nomask:
3430 _mask = self._mask = make_mask_none(self.shape, _dtype)
3431 # Now, set the mask to its value.
3432 if _dtype.names is not None:
3433 _mask[indx] = tuple([True] * len(_dtype.names))
3434 else:
3435 _mask[indx] = True
3436 return
3437
3438 # Get the _data part of the new value
3439 dval = getattr(value, '_data', value)
3440 # Get the _mask part of the new value
3441 mval = getmask(value)
3442 if _dtype.names is not None and mval is nomask:
3443 mval = tuple([False] * len(_dtype.names))
3444 if _mask is nomask:
3445 # Set the data, then the mask
3446 _data[indx] = dval
3447 if mval is not nomask:
3448 _mask = self._mask = make_mask_none(self.shape, _dtype)
3449 _mask[indx] = mval
3450 elif not self._hardmask:
3451 # Set the data, then the mask
3452 if (isinstance(indx, masked_array) and
3453 not isinstance(value, masked_array)):
3454 _data[indx.data] = dval
3455 else:
3456 _data[indx] = dval
3457 _mask[indx] = mval
3458 elif hasattr(indx, 'dtype') and (indx.dtype == MaskType):
3459 indx = indx * umath.logical_not(_mask)
3460 _data[indx] = dval
3461 else:
3462 if _dtype.names is not None:
3463 err_msg = "Flexible 'hard' masks are not yet supported."

Callers

nothing calls this directly

Calls 4

MaskErrorClass · 0.85
make_mask_noneFunction · 0.85
getmaskFunction · 0.85
mask_orFunction · 0.85

Tested by

no test coverage detected