Concatenate strings in a DataArray along a particular dimension. An optional separator `sep` can also be specified. If `sep` is array-like, it is broadcast against the array and applied elementwise. Parameters ---------- dim : hashable, optional
(
self,
dim: Hashable = None,
sep: str | bytes | Any = "",
)
| 513 | ) |
| 514 | |
| 515 | def join( |
| 516 | self, |
| 517 | dim: Hashable = None, |
| 518 | sep: str | bytes | Any = "", |
| 519 | ) -> T_DataArray: |
| 520 | """ |
| 521 | Concatenate strings in a DataArray along a particular dimension. |
| 522 | |
| 523 | An optional separator `sep` can also be specified. If `sep` is |
| 524 | array-like, it is broadcast against the array and applied elementwise. |
| 525 | |
| 526 | Parameters |
| 527 | ---------- |
| 528 | dim : hashable, optional |
| 529 | Dimension along which the strings should be concatenated. |
| 530 | Only one dimension is allowed at a time. |
| 531 | Optional for 0D or 1D DataArrays, required for multidimensional DataArrays. |
| 532 | sep : str or array-like, default: "". |
| 533 | Separator to use between strings. |
| 534 | It is broadcast in the same way as the other input strings. |
| 535 | If array-like, its dimensions will be placed at the end of the output array dimensions. |
| 536 | |
| 537 | Returns |
| 538 | ------- |
| 539 | joined : same type as values |
| 540 | |
| 541 | Examples |
| 542 | -------- |
| 543 | Create an array |
| 544 | |
| 545 | >>> values = xr.DataArray( |
| 546 | ... [["a", "bab", "abc"], ["abcd", "", "abcdef"]], |
| 547 | ... dims=["X", "Y"], |
| 548 | ... ) |
| 549 | |
| 550 | Determine the separator |
| 551 | |
| 552 | >>> seps = xr.DataArray( |
| 553 | ... ["-", "_"], |
| 554 | ... dims=["ZZ"], |
| 555 | ... ) |
| 556 | |
| 557 | Join the strings along a given dimension |
| 558 | |
| 559 | >>> values.str.join(dim="Y", sep=seps) |
| 560 | <xarray.DataArray (X: 2, ZZ: 2)> Size: 192B |
| 561 | array([['a-bab-abc', 'a_bab_abc'], |
| 562 | ['abcd--abcdef', 'abcd__abcdef']], dtype='<U12') |
| 563 | Dimensions without coordinates: X, ZZ |
| 564 | |
| 565 | See Also |
| 566 | -------- |
| 567 | pandas.Series.str.join |
| 568 | str.join |
| 569 | """ |
| 570 | if self._obj.ndim > 1 and dim is None: |
| 571 | raise ValueError("Dimension must be specified for multidimensional arrays.") |
| 572 |