DataArray.str.get_dummies(dim, sep='|')[source]

Return DataArray of dummy/indicator variables.

Each string in the DataArray is split at sep. A new dimension is created with coordinates for each unique result, and the corresponding element of that dimension is True if that result is present and False if not.

If sep is array-like, it is broadcast against the array and applied elementwise.

  • dim (hashable) – Name for the dimension to place the results in.

  • sep (str, default: "|") – String to split on. If array-like, it is broadcast.


dummies (array of bool)


Create a string array

>>> values = xr.DataArray(
...     [
...         ["a|ab~abc|abc", "ab", "a||abc|abcd"],
...         ["abcd|ab|a", "abc|ab~abc", "|a"],
...     ],
...     dims=["X", "Y"],
... )

Extract dummy values

>>> values.str.get_dummies(dim="dummies")
<xarray.DataArray (X: 2, Y: 3, dummies: 5)>
array([[[ True, False,  True, False,  True],
        [False,  True, False, False, False],
        [ True, False,  True,  True, False]],

       [[ True,  True, False,  True, False],
        [False, False,  True, False,  True],
        [ True, False, False, False, False]]])
  * dummies  (dummies) <U6 'a' 'ab' 'abc' 'abcd' 'ab~abc'
Dimensions without coordinates: X, Y