MCPcopy Create free account
hub / github.com/numpy/numpy / _check_fill_value

Function _check_fill_value

numpy/ma/core.py:433–474  ·  view source on GitHub ↗

Private function validating the given `fill_value` for the given dtype. If fill_value is None, it is set to the default corresponding to the dtype. If fill_value is not None, its value is forced to the given dtype. The result is always a 0d array.

(fill_value, ndtype)

Source from the content-addressed store, hash-verified

431
432
433def _check_fill_value(fill_value, ndtype):
434 """
435 Private function validating the given `fill_value` for the given dtype.
436
437 If fill_value is None, it is set to the default corresponding to the dtype.
438
439 If fill_value is not None, its value is forced to the given dtype.
440
441 The result is always a 0d array.
442
443 """
444 ndtype = np.dtype(ndtype)
445 if fill_value is None:
446 fill_value = default_fill_value(ndtype)
447 elif ndtype.names is not None:
448 if isinstance(fill_value, (ndarray, np.void)):
449 try:
450 fill_value = np.array(fill_value, copy=False, dtype=ndtype)
451 except ValueError as e:
452 err_msg = "Unable to transform %s to dtype %s"
453 raise ValueError(err_msg % (fill_value, ndtype)) from e
454 else:
455 fill_value = np.asarray(fill_value, dtype=object)
456 fill_value = np.array(_recursive_set_fill_value(fill_value, ndtype),
457 dtype=ndtype)
458 else:
459 if isinstance(fill_value, str) and (ndtype.char not in 'OSVU'):
460 # Note this check doesn't work if fill_value is not a scalar
461 err_msg = "Cannot set fill value of string with array of dtype %s"
462 raise TypeError(err_msg % ndtype)
463 else:
464 # In case we want to convert 1e20 to int.
465 # Also in case of converting string arrays.
466 try:
467 fill_value = np.array(fill_value, copy=False, dtype=ndtype)
468 except (OverflowError, ValueError) as e:
469 # Raise TypeError instead of OverflowError or ValueError.
470 # OverflowError is seldom used, and the real problem here is
471 # that the passed fill_value is not compatible with the ndtype.
472 err_msg = "Cannot convert fill_value %s to dtype %s"
473 raise TypeError(err_msg % (fill_value, ndtype)) from e
474 return np.array(fill_value)
475
476
477def set_fill_value(a, fill_value):

Callers 9

merge_arraysFunction · 0.85
__new__Method · 0.85
__array_finalize__Method · 0.85
fill_valueMethod · 0.85
filledMethod · 0.85
_comparisonMethod · 0.85
test_check_on_scalarMethod · 0.85
test_check_on_fieldsMethod · 0.85

Calls 3

default_fill_valueFunction · 0.85
dtypeMethod · 0.45

Tested by 3

test_check_on_scalarMethod · 0.68
test_check_on_fieldsMethod · 0.68