MCPcopy
hub / github.com/pydata/xarray / count

Method count

xarray/core/accessor_str.py:1079–1155  ·  view source on GitHub ↗

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 :class:`~xarray.DataArray`. The pattern `pat` can either be a single ``

(
        self, pat: str | bytes | Pattern | Any, flags: int = 0, case: bool | None = None
    )

Source from the content-addressed store, hash-verified

1077 return self._apply(func=lambda x: x.isupper(), dtype=bool)
1078
1079 def count(
1080 self, pat: str | bytes | Pattern | Any, flags: int = 0, case: bool | None = None
1081 ) -> T_DataArray:
1082 """
1083 Count occurrences of pattern in each string of the array.
1084
1085 This function is used to count the number of times a particular regex
1086 pattern is repeated in each of the string elements of the
1087 :class:`~xarray.DataArray`.
1088
1089 The pattern `pat` can either be a single ``str`` or `re.Pattern` or
1090 array-like of ``str`` or `re.Pattern`. If array-like, it is broadcast
1091 against the array and applied elementwise.
1092
1093 Parameters
1094 ----------
1095 pat : str or re.Pattern or array-like of str or re.Pattern
1096 A string containing a regular expression or a compiled regular
1097 expression object. If array-like, it is broadcast.
1098 flags : int, default: 0
1099 Flags to pass through to the re module, e.g. `re.IGNORECASE`.
1100 see `compilation-flags <https://docs.python.org/3/howto/regex.html#compilation-flags>`_.
1101 ``0`` means no flags. Flags can be combined with the bitwise or operator ``|``.
1102 Cannot be set if `pat` is a compiled regex.
1103 case : bool, default: True
1104 If True, case sensitive.
1105 Cannot be set if `pat` is a compiled regex.
1106 Equivalent to setting the `re.IGNORECASE` flag.
1107
1108 Returns
1109 -------
1110 counts : array of int
1111
1112 Examples
1113 --------
1114 >>> da = xr.DataArray(["jjklmn", "opjjqrs", "t-JJ99vwx"], dims="x")
1115 >>> da
1116 <xarray.DataArray (x: 3)> Size: 108B
1117 array(['jjklmn', 'opjjqrs', 't-JJ99vwx'], dtype='<U9')
1118 Dimensions without coordinates: x
1119
1120 Using a string:
1121 >>> da.str.count("jj")
1122 <xarray.DataArray (x: 3)> Size: 24B
1123 array([1, 1, 0])
1124 Dimensions without coordinates: x
1125
1126 Enable case-insensitive matching by setting case to false:
1127 >>> counts = da.str.count("jj", case=False)
1128 >>> counts
1129 <xarray.DataArray (x: 3)> Size: 24B
1130 array([1, 1, 1])
1131 Dimensions without coordinates: x
1132
1133 Using regex:
1134 >>> pat = "JJ[0-9]{2}[a-z]{3}"
1135 >>> counts = da.str.count(pat)
1136 >>> counts

Callers 2

parse_ordered_dimsFunction · 0.45
dropnaMethod · 0.45

Calls 3

_re_compileMethod · 0.95
_applyMethod · 0.95
findallMethod · 0.80

Tested by

no test coverage detected