xarray.DataArray.str.get_dummies

xarray.DataArray.str.get_dummies#

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.

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

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

Returns

dummies (array of bool)

Examples

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)> Size: 30B
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]]])
Coordinates:
  * dummies  (dummies) <U6 120B 'a' 'ab' 'abc' 'abcd' 'ab~abc'
Dimensions without coordinates: X, Y