Replace each null element in values with a corresponding element from fill_value. If fill_value is scalar-like, then every null element in values will be replaced with fill_value. If fill_value is array-like, then the i-th element in values will be replaced with the i-th element
(values, fill_value)
| 510 | |
| 511 | |
| 512 | def fill_null(values, fill_value): |
| 513 | """Replace each null element in values with a corresponding |
| 514 | element from fill_value. |
| 515 | |
| 516 | If fill_value is scalar-like, then every null element in values |
| 517 | will be replaced with fill_value. If fill_value is array-like, |
| 518 | then the i-th element in values will be replaced with the i-th |
| 519 | element in fill_value. |
| 520 | |
| 521 | The fill_value's type must be the same as that of values, or it |
| 522 | must be able to be implicitly casted to the array's type. |
| 523 | |
| 524 | This is an alias for :func:`coalesce`. |
| 525 | |
| 526 | Parameters |
| 527 | ---------- |
| 528 | values : Array, ChunkedArray, or Scalar-like object |
| 529 | Each null element is replaced with the corresponding value |
| 530 | from fill_value. |
| 531 | fill_value : Array, ChunkedArray, or Scalar-like object |
| 532 | If not same type as values, will attempt to cast. |
| 533 | |
| 534 | Returns |
| 535 | ------- |
| 536 | result : depends on inputs |
| 537 | Values with all null elements replaced |
| 538 | |
| 539 | Examples |
| 540 | -------- |
| 541 | >>> import pyarrow as pa |
| 542 | >>> arr = pa.array([1, 2, None, 3], type=pa.int8()) |
| 543 | >>> fill_value = pa.scalar(5, type=pa.int8()) |
| 544 | >>> arr.fill_null(fill_value) |
| 545 | <pyarrow.lib.Int8Array object at ...> |
| 546 | [ |
| 547 | 1, |
| 548 | 2, |
| 549 | 5, |
| 550 | 3 |
| 551 | ] |
| 552 | >>> arr = pa.array([1, 2, None, 4, None]) |
| 553 | >>> arr.fill_null(pa.array([10, 20, 30, 40, 50])) |
| 554 | <pyarrow.lib.Int64Array object at ...> |
| 555 | [ |
| 556 | 1, |
| 557 | 2, |
| 558 | 30, |
| 559 | 4, |
| 560 | 50 |
| 561 | ] |
| 562 | """ |
| 563 | if not isinstance(fill_value, (pa.Array, pa.ChunkedArray, pa.Scalar)): |
| 564 | fill_value = pa.scalar(fill_value, type=values.type) |
| 565 | elif values.type != fill_value.type: |
| 566 | fill_value = pa.scalar(fill_value.as_py(), type=values.type) |
| 567 | |
| 568 | return call_function("coalesce", [values, fill_value]) |
| 569 |