MCPcopy Index your code
hub / github.com/numpy/numpy / partition

Function partition

numpy/_core/strings.py:1538–1602  ·  view source on GitHub ↗

Partition each element in ``a`` around ``sep``. For each element in ``a``, split the element at the first occurrence of ``sep``, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found,

(a, sep)

Source from the content-addressed store, hash-verified

1536@set_module("numpy.strings")
1537@array_function_dispatch(_partition_dispatcher)
1538def partition(a, sep):
1539 """
1540 Partition each element in ``a`` around ``sep``.
1541
1542 For each element in ``a``, split the element at the first
1543 occurrence of ``sep``, and return a 3-tuple containing the part
1544 before the separator, the separator itself, and the part after
1545 the separator. If the separator is not found, the first item of
1546 the tuple will contain the whole string, and the second and third
1547 ones will be the empty string.
1548
1549 Parameters
1550 ----------
1551 a : array-like, with ``StringDType``, ``bytes_``, or ``str_`` dtype
1552 Input array
1553 sep : array-like, with ``StringDType``, ``bytes_``, or ``str_`` dtype
1554 Separator to split each string element in ``a``.
1555
1556 Returns
1557 -------
1558 out : 3-tuple:
1559 - array with ``StringDType``, ``bytes_`` or ``str_`` dtype with the
1560 part before the separator
1561 - array with ``StringDType``, ``bytes_`` or ``str_`` dtype with the
1562 separator
1563 - array with ``StringDType``, ``bytes_`` or ``str_`` dtype with the
1564 part after the separator
1565
1566 See Also
1567 --------
1568 str.partition
1569
1570 Examples
1571 --------
1572 >>> import numpy as np
1573 >>> x = np.array(["Numpy is nice!"])
1574 >>> np.strings.partition(x, " ")
1575 (array(['Numpy'], dtype='<U5'),
1576 array([' '], dtype='<U1'),
1577 array(['is nice!'], dtype='<U8'))
1578
1579 """
1580 a = np.asanyarray(a)
1581 sep = np.asanyarray(sep)
1582
1583 if np.result_type(a, sep).char == "T":
1584 return _partition(a, sep)
1585
1586 sep = sep.astype(a.dtype, copy=False)
1587 pos = _find_ufunc(a, sep, 0, MAX)
1588 a_len = str_len(a)
1589 sep_len = str_len(sep)
1590
1591 not_found = pos < 0
1592 buffersizes1 = np.where(not_found, a_len, pos)
1593 buffersizes3 = np.where(not_found, 0, a_len - pos - sep_len)
1594
1595 out_dtype = ",".join([f"{a.dtype.char}{n}" for n in (

Callers

nothing calls this directly

Calls 4

astypeMethod · 0.80
joinMethod · 0.80
maxMethod · 0.45
allMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…