Skip to content

Commit 893215a

Browse files
authored
Merge pull request numpy#27058 from agriyakhetarpal/docs/add-missing-np-strings-examples
DOC: Add some missing examples for `np.strings` methods
2 parents e23826e + b2b0338 commit 893215a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

numpy/_core/code_generators/ufunc_docstrings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4482,6 +4482,17 @@ def add_newdoc(place, name, doc):
44824482
--------
44834483
str.isalpha
44844484
4485+
Examples
4486+
--------
4487+
>>> import numpy as np
4488+
>>> a = np.array(['a', 'b', '0'])
4489+
>>> np.strings.isalpha(a)
4490+
array([ True, True, False])
4491+
4492+
>>> a = np.array([['a', 'b', '0'], ['c', '1', '2']])
4493+
>>> np.strings.isalpha(a)
4494+
array([[ True, True, False], [ True, False, False]])
4495+
44854496
""")
44864497

44874498
add_newdoc('numpy._core.umath', 'isdigit',

numpy/_core/strings.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,19 @@ def mod(a, values):
197197
out : ndarray
198198
Output array of ``StringDType``, ``bytes_`` or ``str_`` dtype,
199199
depending on input types
200-
200+
201+
Examples
202+
--------
203+
>>> import numpy as np
204+
>>> a = np.array(["NumPy is a %s library"])
205+
>>> np.strings.mod(a, values=["Python"])
206+
array(['NumPy is a Python library'], dtype='<U25')
207+
208+
>>> a = np.array([b'%d bytes', b'%d bits'])
209+
>>> values = np.array([8, 64])
210+
>>> np.strings.mod(a, values)
211+
array([b'8 bytes', b'64 bits'], dtype='|S7')
212+
201213
"""
202214
return _to_bytes_or_str_array(
203215
_vec_string(a, np.object_, '__mod__', (values,)), a)
@@ -265,6 +277,18 @@ def rfind(a, sub, start=0, end=None):
265277
--------
266278
str.rfind
267279
280+
Examples
281+
--------
282+
>>> import numpy as np
283+
>>> a = np.array(["Computer Science"])
284+
>>> np.strings.rfind(a, "Science", start=0, end=None)
285+
array([9])
286+
>>> np.strings.rfind(a, "Science", start=0, end=8)
287+
array([-1])
288+
>>> b = np.array(["Computer Science", "Science"])
289+
>>> np.strings.rfind(b, "Science", start=0, end=None)
290+
array([9, 0])
291+
268292
"""
269293
end = end if end is not None else MAX
270294
return _rfind_ufunc(a, sub, start, end)
@@ -404,6 +428,17 @@ def startswith(a, prefix, start=0, end=None):
404428
--------
405429
str.startswith
406430
431+
Examples
432+
--------
433+
>>> import numpy as np
434+
>>> s = np.array(['foo', 'bar'])
435+
>>> s
436+
array(['foo', 'bar'], dtype='<U3')
437+
>>> np.strings.startswith(s, 'fo')
438+
array([True, False])
439+
>>> np.strings.startswith(s, 'o', start=1, end=2)
440+
array([True, False])
441+
407442
"""
408443
end = end if end is not None else MAX
409444
return _startswith_ufunc(a, prefix, start, end)

0 commit comments

Comments
 (0)