xarray.core.accessor_str.StringAccessor.split

xarray.core.accessor_str.StringAccessor.split#

StringAccessor.split(dim, sep=None, maxsplit=-1)[source]#

Split strings in a DataArray around the given separator/delimiter sep.

Splits the string in the DataArray from the beginning, at the specified delimiter string.

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

Parameters
  • dim (hashable or None) – Name for the dimension to place the results in. If None, place the results as list elements in an object DataArray.

  • sep (str, default: None) – String to split on. If None (the default), split on any whitespace. If array-like, it is broadcast.

  • maxsplit (int, default: -1) – Limit number of splits in output, starting from the beginning. If -1 (the default), return all splits.

Returns

splitted (same type as values or object array)

Examples

Create a string DataArray

>>> values = xr.DataArray(
...     [
...         ["abc def", "spam\t\teggs\tswallow", "red_blue"],
...         ["test0\ntest1\ntest2\n\ntest3", "", "abra  ka\nda\tbra"],
...     ],
...     dims=["X", "Y"],
... )

Split once and put the results in a new dimension

>>> values.str.split(dim="splitted", maxsplit=1)
<xarray.DataArray (X: 2, Y: 3, splitted: 2)> Size: 864B
array([[['abc', 'def'],
        ['spam', 'eggs\tswallow'],
        ['red_blue', '']],

       [['test0', 'test1\ntest2\n\ntest3'],
        ['', ''],
        ['abra', 'ka\nda\tbra']]], dtype='<U18')
Dimensions without coordinates: X, Y, splitted

Split as many times as needed and put the results in a new dimension

>>> values.str.split(dim="splitted")
<xarray.DataArray (X: 2, Y: 3, splitted: 4)> Size: 768B
array([[['abc', 'def', '', ''],
        ['spam', 'eggs', 'swallow', ''],
        ['red_blue', '', '', '']],

       [['test0', 'test1', 'test2', 'test3'],
        ['', '', '', ''],
        ['abra', 'ka', 'da', 'bra']]], dtype='<U8')
Dimensions without coordinates: X, Y, splitted

Split once and put the results in lists

>>> values.str.split(dim=None, maxsplit=1)
<xarray.DataArray (X: 2, Y: 3)> Size: 48B
array([[list(['abc', 'def']), list(['spam', 'eggs\tswallow']),
        list(['red_blue'])],
       [list(['test0', 'test1\ntest2\n\ntest3']), list([]),
        list(['abra', 'ka\nda\tbra'])]], dtype=object)
Dimensions without coordinates: X, Y

Split as many times as needed and put the results in a list

>>> values.str.split(dim=None)
<xarray.DataArray (X: 2, Y: 3)> Size: 48B
array([[list(['abc', 'def']), list(['spam', 'eggs', 'swallow']),
        list(['red_blue'])],
       [list(['test0', 'test1', 'test2', 'test3']), list([]),
        list(['abra', 'ka', 'da', 'bra'])]], dtype=object)
Dimensions without coordinates: X, Y

Split only on spaces

>>> values.str.split(dim="splitted", sep=" ")
<xarray.DataArray (X: 2, Y: 3, splitted: 3)> Size: 2kB
array([[['abc', 'def', ''],
        ['spam\t\teggs\tswallow', '', ''],
        ['red_blue', '', '']],

       [['test0\ntest1\ntest2\n\ntest3', '', ''],
        ['', '', ''],
        ['abra', '', 'ka\nda\tbra']]], dtype='<U24')
Dimensions without coordinates: X, Y, splitted

See also

DataArray.str.rsplit, str.split, pandas.Series.str.split