xarray.DataArray.str.count#
- DataArray.str.count(pat, flags=0, case=None)[source]#
Count occurrences of pattern in each string of the array.
This function is used to count the number of times a particular regex pattern is repeated in each of the string elements of the
DataArray
.The pattern pat can either be a single
str
or re.Pattern or array-like ofstr
or re.Pattern. If array-like, it is broadcast against the array and applied elementwise.- Parameters
pat (
str
orre.Pattern
or array-like ofstr
orre.Pattern
) – A string containing a regular expression or a compiled regular expression object. If array-like, it is broadcast.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.case (
bool
, default:True
) – If True, case sensitive. Cannot be set if pat is a compiled regex. Equivalent to setting the re.IGNORECASE flag.
- Returns
Examples
>>> da = xr.DataArray(["jjklmn", "opjjqrs", "t-JJ99vwx"], dims="x") >>> da <xarray.DataArray (x: 3)> Size: 108B array(['jjklmn', 'opjjqrs', 't-JJ99vwx'], dtype='<U9') Dimensions without coordinates: x
Using a string: >>> da.str.count(“jj”) <xarray.DataArray (x: 3)> Size: 24B array([1, 1, 0]) Dimensions without coordinates: x
Enable case-insensitive matching by setting case to false: >>> counts = da.str.count(“jj”, case=False) >>> counts <xarray.DataArray (x: 3)> Size: 24B array([1, 1, 1]) Dimensions without coordinates: x
Using regex: >>> pat = “JJ[0-9]{2}[a-z]{3}” >>> counts = da.str.count(pat) >>> counts <xarray.DataArray (x: 3)> Size: 24B array([0, 0, 1]) Dimensions without coordinates: x
Using an array of strings (the pattern will be broadcast against the array):
>>> pat = xr.DataArray(["jj", "JJ"], dims="y") >>> counts = da.str.count(pat) >>> counts <xarray.DataArray (x: 3, y: 2)> Size: 48B array([[1, 0], [1, 0], [0, 1]]) Dimensions without coordinates: x, y