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)
| 431 | |
| 432 | |
| 433 | def _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 | |
| 477 | def set_fill_value(a, fill_value): |