xarray.DataArray.str.rsplit#

DataArray.str.rsplit(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 end, 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 end. If -1 (the default), return all splits. The final number of split values may be less than this if there are no DataArray elements with that many values.

Returns:

rsplitted (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.rsplit(dim="splitted", maxsplit=1)
<xarray.DataArray (X: 2, Y: 3, splitted: 2)> Size: 816B
array([[['abc', 'def'],
 ['spam\t\teggs', 'swallow'],
 ['', 'red_blue']],
 [['test0\ntest1\ntest2', 'test3'],
 ['', ''],
 ['abra ka\nda', 'bra']]], dtype='<U17')
Dimensions without coordinates: X, Y, splitted

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

>>> values.str.rsplit(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.rsplit(dim=None, maxsplit=1)
<xarray.DataArray (X: 2, Y: 3)> Size: 48B
array([[list(['abc', 'def']), list(['spam\t\teggs', 'swallow']),
 list(['red_blue'])],
 [list(['test0\ntest1\ntest2', 'test3']), list([]),
 list(['abra ka\nda', 'bra'])]], dtype=object)
Dimensions without coordinates: X, Y

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

>>> values.str.rsplit(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.rsplit(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
On this page