🍾 Xarray is now 10 years old! 🎉

xarray.DataArray.str.join

xarray.DataArray.str.join#

DataArray.str.join(dim=None, sep='')[source]#

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) – Dimension along which the strings should be concatenated. Only one dimension is allowed at a time. Optional for 0D or 1D DataArrays, required for multidimensional DataArrays.

  • sep (str or array-like, default: "") – Separator to use between strings. It is broadcast in the same way as the other input strings. If array-like, its dimensions will be placed at the end of the output array dimensions.

Returns:

joined (same type as values)

Examples

Create an array

>>> values = xr.DataArray(
...     [["a", "bab", "abc"], ["abcd", "", "abcdef"]],
...     dims=["X", "Y"],
... )

Determine the separator

>>> seps = xr.DataArray(
...     ["-", "_"],
...     dims=["ZZ"],
... )

Join the strings along a given dimension

>>> values.str.join(dim="Y", sep=seps)
<xarray.DataArray (X: 2, ZZ: 2)> Size: 192B
array([['a-bab-abc', 'a_bab_abc'],
       ['abcd--abcdef', 'abcd__abcdef']], dtype='<U12')
Dimensions without coordinates: X, ZZ