Skip to content

Commit e3f9633

Browse files
Ridhwan LuthraTomAugspurger
authored andcommitted
DOC: Update the pandas.Series.str.count() docstring (Delhi) (#20154)
1 parent 92c2910 commit e3f9633

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

pandas/core/strings.py

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,65 @@ def str_count(arr, pat, flags=0):
212212
"""
213213
Count occurrences of pattern in each string of the Series/Index.
214214
215+
This function is used to count the number of times a particular regex
216+
pattern is repeated in each of the string elements of the
217+
:class:`~pandas.Series`.
218+
215219
Parameters
216220
----------
217-
pat : string, valid regular expression
218-
flags : int, default 0 (no flags)
219-
re module flags, e.g. re.IGNORECASE
221+
pat : str
222+
Valid regular expression.
223+
flags : int, default 0, meaning no flags
224+
Flags for the `re` module. For a complete list, `see here
225+
<https://docs.python.org/3/howto/regex.html#compilation-flags>`_.
226+
**kwargs
227+
For compatability with other string methods. Not used.
220228
221229
Returns
222230
-------
223-
counts : Series/Index of integer values
231+
counts : Series or Index
232+
Same type as the calling object containing the integer counts.
233+
234+
Notes
235+
-----
236+
Some characters need to be escaped when passing in `pat`.
237+
eg. ``'$'`` has a special meaning in regex and must be escaped when
238+
finding this literal character.
239+
240+
See Also
241+
--------
242+
re : Standard library module for regular expressions.
243+
str.count : Standard library version, without regular expression support.
244+
245+
Examples
246+
--------
247+
>>> s = pd.Series(['A', 'B', 'Aaba', 'Baca', np.nan, 'CABA', 'cat'])
248+
>>> s.str.count('a')
249+
0 0.0
250+
1 0.0
251+
2 2.0
252+
3 2.0
253+
4 NaN
254+
5 0.0
255+
6 1.0
256+
dtype: float64
257+
258+
Escape ``'$'`` to find the literal dollar sign.
259+
260+
>>> s = pd.Series(['$', 'B', 'Aab$', '$$ca', 'C$B$', 'cat'])
261+
>>> s.str.count('\$')
262+
0 1
263+
1 0
264+
2 1
265+
3 2
266+
4 2
267+
5 0
268+
dtype: int64
269+
270+
This is also available on Index
271+
272+
>>> pd.Index(['A', 'A', 'Aaba', 'cat']).str.count('a')
273+
Int64Index([0, 0, 2, 1], dtype='int64')
224274
"""
225275
regex = re.compile(pat, flags=flags)
226276
f = lambda x: len(regex.findall(x))

0 commit comments

Comments
 (0)