xarray.core.accessor_str.StringAccessor.cat#

StringAccessor.cat(*others, sep='')[source]#

Concatenate strings elementwise in the DataArray with other strings.

The other strings can either be string scalars or other array-like. Dimensions are automatically broadcast together.

An optional separator sep can also be specified. If sep is array-like, it is broadcast against the array and applied elementwise.

Parameters:
  • *others (str or array-like of str) – Strings or array-like of strings to concatenate elementwise with the current DataArray.

  • sep (str or array-like of str, 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:

concatenated (same type as values)

Examples

Create a string array

>>> myarray = xr.DataArray(
...     ["11111", "4"],
...     dims=["X"],
... )

Create some arrays to concatenate with it

>>> values_1 = xr.DataArray(
...     ["a", "bb", "cccc"],
...     dims=["Y"],
... )
>>> values_2 = np.array(3.4)
>>> values_3 = ""
>>> values_4 = np.array("test", dtype=np.str_)

Determine the separator to use

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

Concatenate the arrays using the separator

>>> myarray.str.cat(values_1, values_2, values_3, values_4, sep=seps)
<xarray.DataArray (X: 2, Y: 3, ZZ: 2)>
array([[['11111 a 3.4  test', '11111, a, 3.4, , test'],
        ['11111 bb 3.4  test', '11111, bb, 3.4, , test'],
        ['11111 cccc 3.4  test', '11111, cccc, 3.4, , test']],

       [['4 a 3.4  test', '4, a, 3.4, , test'],
        ['4 bb 3.4  test', '4, bb, 3.4, , test'],
        ['4 cccc 3.4  test', '4, cccc, 3.4, , test']]], dtype='<U24')
Dimensions without coordinates: X, Y, ZZ