xarray.DataArray.str.findall

xarray.DataArray.str.findall#

DataArray.str.findall(pat, case=None, flags=0)[source]#

Find all occurrences of pattern or regular expression in the DataArray.

Equivalent to applying re.findall() to all the elements in the DataArray. Results in an object array of lists. If there is only one capture group, the lists will be a sequence of matches. If there are multiple capture groups, the lists will be a sequence of lists, each of which contains a sequence of matches.

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

Parameters
  • pat (str or re.Pattern) – A string containing a regular expression or a compiled regular expression object. If array-like, it is broadcast.

  • case (bool, default: True) – If True, case sensitive. Cannot be set if pat is a compiled regex. Equivalent to setting the re.IGNORECASE flag.

  • flags (int, default: 0) – Flags to pass through to the re module, e.g. re.IGNORECASE. see compilation-flags. 0 means no flags. Flags can be combined with the bitwise or operator |. Cannot be set if pat is a compiled regex.

Returns

extracted (object array)

Raises
  • ValueErrorpat has no capture groups.

  • ValueErrorcase is set when pat is a compiled regular expression.

Examples

Create a string array

>>> value = xr.DataArray(
...     [
...         [
...             "a_Xy_0",
...             "ab_xY_10-bab_Xy_110-baab_Xy_1100",
...             "abc_Xy_01-cbc_Xy_2210",
...         ],
...         [
...             "abcd_Xy_-dcd_Xy_33210-dccd_Xy_332210",
...             "",
...             "abcdef_Xy_101-fef_Xy_5543210",
...         ],
...     ],
...     dims=["X", "Y"],
... )

Extract matches

>>> value.str.findall(r"(\w+)_Xy_(\d*)")
<xarray.DataArray (X: 2, Y: 3)> Size: 48B
array([[list([('a', '0')]), list([('bab', '110'), ('baab', '1100')]),
        list([('abc', '01'), ('cbc', '2210')])],
       [list([('abcd', ''), ('dcd', '33210'), ('dccd', '332210')]),
        list([]), list([('abcdef', '101'), ('fef', '5543210')])]],
      dtype=object)
Dimensions without coordinates: X, Y