o 6ag@sdZddlZddlZddlmZmZmZmZmZm Z m Z ddl m Z m Z ddl mZddlmZddlmZdd lmZdd lmZddlZgd Zdaejejd d ZddZddZddZddZ ddZ!ee!ddZ"ee!ddZ#ee!ddZ$ee!ddZ%ee!d d!Z&ee!d"d#Z'd$d%Z(ee(d&d'Z)ee!d(d)Z*d*d+Z+ee+d,d-Z,d.d/Z-ee-d0d1Z.ee(d2d3Z/dd4d5Z0ee0dd7d8Z1dd9d:Z2ee2dd;d<Z3dd=d>Z4ee4dd?d@Z5ee4ddAdBZ6ddCdDZ7ee7ddEdFZ8ddGdHZ9ee9ddJdKZ:ee2ddLdMZ;ee2ddNdOZee(dTdUZ?ee(dVdWZ@ee(dXdYZAee(dZd[ZBee(d\d]ZCd^d_ZDeeDd`daZEddbdcZFeeFddddeZGee(dfdgZHddhdiZIeeIddjdkZJdldmZKeeKdndoZLddpdqZMeeMddrdsZNee2ddtduZOee2ddvdwZPeeFddxdyZQeeKdzd{ZRdd|d}ZSeeSdd~dZTdddiZIeeIdddZUeeSdddZVdddZWeeWdddZXdddZYeeYdddZZeeIdddZ[ee(ddZ\ee(ddZ]dddZ^ee^dddZ_ee(ddZ`ddZaeeaddZbee(ddZcee(ddZdedGddde ZedddZdddZfdS)as This module contains a set of functions for vectorized string operations and methods. .. note:: The `chararray` class exists for backwards compatibility with Numarray, it is not recommended for new development. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of `dtype` `object_`, `string_` or `unicode_`, and use the free functions in the `numpy.char` module for fast vectorized string operations. Some methods will only be available if the corresponding string method is available in your version of Python. The preferred alias for `defchararray` is `numpy.char`. N)string_unicode_integerint_object_bool_ character)ndarraycompare_chararraysarray) _vec_string) set_module) overrides)asbytes)3equal not_equal greater_equal less_equalgreaterlessstr_lenaddmultiplymod capitalizecentercountdecodeencodeendswith expandtabsfindindexisalnumisalphaisdigitislowerisspaceistitleisupperjoinljustlowerlstrip partitionreplacerfindrindexrjust rpartitionrsplitrstripsplit splitlines startswithstripswapcasetitle translateupperzfill isnumeric isdecimalr asarrayz numpy.char)modulecGs4|D]}t|tstt|jjtrtSqtS)z Helper function for determining the output type of some string operations. For an operation on two ndarrays, if at least one is unicode, the result should be unicode. ) isinstancestr issubclassnumpyrCdtypetyperr)argsxrM9/usr/lib/python3/dist-packages/numpy/core/defchararray.py _use_unicode1s rOcCst|S)z Helper function to cast a result back into a string or unicode array if an object array must be used as an intermediary. )rHrCtolist)resultrMrMrN_to_string_or_unicode_array?srRcGs*g}|D]}|dur |S||q|S)a! Helper function for delegating arguments to Python string functions. Many of the Python string operations that have optional arguments do not use 'None' to indicate a default value. In these cases, we need to remove all None arguments, and those following them. N)append)rKnewargschkrMrMrN _clean_argsFs  rVcCst|jjtr |jdS|jS)z Helper function that returns the number of characters per field in a string or unicode array. This is to abstract out the fact that for a unicode array this is itemsize / 4. )rGrIrJritemsizearMrMrN_get_num_charsVs r[cC||fSNrMx1x2rMrMrN_binary_op_dispatcheraracCt||ddS)a Return (x1 == x2) element-wise. Unlike `numpy.equal`, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarray. Parameters ---------- x1, x2 : array_like of str or unicode Input arrays of the same shape. Returns ------- out : ndarray Output array of bools. See Also -------- not_equal, greater_equal, less_equal, greater, less z==Tr r^rMrMrNrercCrc)a Return (x1 != x2) element-wise. Unlike `numpy.not_equal`, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarray. Parameters ---------- x1, x2 : array_like of str or unicode Input arrays of the same shape. Returns ------- out : ndarray Output array of bools. See Also -------- equal, greater_equal, less_equal, greater, less z!=Trdr^rMrMrNrrercCrc)a Return (x1 >= x2) element-wise. Unlike `numpy.greater_equal`, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarray. Parameters ---------- x1, x2 : array_like of str or unicode Input arrays of the same shape. Returns ------- out : ndarray Output array of bools. See Also -------- equal, not_equal, less_equal, greater, less z>=Trdr^rMrMrNrsrcCrc)a Return (x1 <= x2) element-wise. Unlike `numpy.less_equal`, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarray. Parameters ---------- x1, x2 : array_like of str or unicode Input arrays of the same shape. Returns ------- out : ndarray Output array of bools. See Also -------- equal, not_equal, greater_equal, greater, less z<=Trdr^rMrMrNrrercCrc)a Return (x1 > x2) element-wise. Unlike `numpy.greater`, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarray. Parameters ---------- x1, x2 : array_like of str or unicode Input arrays of the same shape. Returns ------- out : ndarray Output array of bools. See Also -------- equal, not_equal, greater_equal, less_equal, less >Trdr^rMrMrNrrercCrc)a Return (x1 < x2) element-wise. Unlike `numpy.greater`, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarray. Parameters ---------- x1, x2 : array_like of str or unicode Input arrays of the same shape. Returns ------- out : ndarray Output array of bools. See Also -------- equal, not_equal, greater_equal, less_equal, greater s  rcCr\r]rMrZvaluesrMrMrN_mod_dispatcher\rbr{cCtt|td|fS)a Return (a % i), that is pre-Python 2.6 string formatting (interpolation), element-wise for a pair of array_likes of str or unicode. Parameters ---------- a : array_like of str or unicode values : array_like of values These values will be element-wise interpolated into the string. Returns ------- out : ndarray Output array of str or unicode, depending on input types See Also -------- str.__mod__ __mod__rRrrryrMrMrNr`srcCt|}t||jdS)a Return a copy of `a` with only the first character of each element capitalized. Calls `str.capitalize` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like of str or unicode Input array of strings to capitalize. Returns ------- out : ndarray Output array of str or unicode, depending on input types See Also -------- str.capitalize Examples -------- >>> c = np.array(['a1b2','1b2a','b2a1','2a1b'],'S4'); c array(['a1b2', '1b2a', 'b2a1', '2a1b'], dtype='|S4') >>> np.char.capitalize(c) array(['A1b2', '1b2a', 'B2a1', '2a1b'], dtype='|S4') rrHrCrrIrZrwrMrMrNr|s #rcCrhr]rMrZwidthfillcharrMrMrN_center_dispatcherrjr cCVt|}t|}tt|j}t|jtjrt|}t ||jj |fd||fS)a Return a copy of `a` with its elements centered in a string of length `width`. Calls `str.center` element-wise. Parameters ---------- a : array_like of str or unicode width : int The length of the resulting strings fillchar : str or unicode, optional The padding character to use (default is space). Returns ------- out : ndarray Output array of str or unicode, depending on input types See Also -------- str.center r rHrCrvruflat issubdtyperIrrrrJrZrrrw width_arrsizerMrMrNrs  rcCrhr]rMrZsubstartendrMrMrN_count_dispatcherrjrcCt|td||gt|S)a Returns an array with the number of non-overlapping occurrences of substring `sub` in the range [`start`, `end`]. Calls `str.count` element-wise. Parameters ---------- a : array_like of str or unicode sub : str or unicode The substring to search for. start, end : int, optional Optional arguments `start` and `end` are interpreted as slice notation to specify the range in which to count. Returns ------- out : ndarray Output array of ints. See Also -------- str.count Examples -------- >>> c = np.array(['aAaAaA', ' aA ', 'abBABba']) >>> c array(['aAaAaA', ' aA ', 'abBABba'], dtype='>> np.char.count(c, 'A') array([3, 1, 1]) >>> np.char.count(c, 'aA') array([3, 1, 0]) >>> np.char.count(c, 'A', start=1, end=4) array([2, 1, 1]) >>> np.char.count(c, 'A', start=1, end=3) array([1, 0, 0]) rrrrVrrMrMrNrs+rcCrhr]rMrZencodingerrorsrMrMrN_code_dispatcherrjrcCtt|tdt||S)a} Calls `str.decode` element-wise. The set of available codecs comes from the Python standard library, and may be extended at runtime. For more information, see the :mod:`codecs` module. Parameters ---------- a : array_like of str or unicode encoding : str, optional The name of an encoding errors : str, optional Specifies how to handle encoding errors Returns ------- out : ndarray See Also -------- str.decode Notes ----- The type of the result will depend on the encoding specified. Examples -------- >>> c = np.array(['aAaAaA', ' aA ', 'abBABba']) >>> c array(['aAaAaA', ' aA ', 'abBABba'], dtype='>> np.char.encode(c, encoding='cp037') array(['\x81\xc1\x81\xc1\x81\xc1', '@@\x81\xc1@@', '\x81\x82\xc2\xc1\xc2\x82\x81'], dtype='|S7') rrRrrrVrrMrMrNrs*rcCr)aA Calls `str.encode` element-wise. The set of available codecs comes from the Python standard library, and may be extended at runtime. For more information, see the codecs module. Parameters ---------- a : array_like of str or unicode encoding : str, optional The name of an encoding errors : str, optional Specifies how to handle encoding errors Returns ------- out : ndarray See Also -------- str.encode Notes ----- The type of the result will depend on the encoding specified. r rrrMrMrNr 0s r cCrhr]rMrZsuffixrrrMrMrN_endswith_dispatcherTrjrcCr)a5 Returns a boolean array which is `True` where the string element in `a` ends with `suffix`, otherwise `False`. Calls `str.endswith` element-wise. Parameters ---------- a : array_like of str or unicode suffix : str start, end : int, optional With optional `start`, test beginning at that position. With optional `end`, stop comparing at that position. Returns ------- out : ndarray Outputs an array of bools. See Also -------- str.endswith Examples -------- >>> s = np.array(['foo', 'bar']) >>> s[0] = 'foo' >>> s[1] = 'bar' >>> s array(['foo', 'bar'], dtype='>> np.char.endswith(s, 'ar') array([False, True]) >>> np.char.endswith(s, 'a', start=1, end=2) array([False, True]) r!rrrVrrMrMrNr!Xs(r!cCrhr]rMrZtabsizerMrMrN_expandtabs_dispatcherrjrcCr|)aS Return a copy of each string element where all tab characters are replaced by one or more spaces. Calls `str.expandtabs` element-wise. Return a copy of each string element where all tab characters are replaced by one or more spaces, depending on the current column and the given `tabsize`. The column number is reset to zero after each newline occurring in the string. This doesn't understand other non-printing characters or escape sequences. Parameters ---------- a : array_like of str or unicode Input array tabsize : int, optional Replace tabs with `tabsize` number of spaces. If not given defaults to 8 spaces. Returns ------- out : ndarray Output array of str or unicode, depending on input type See Also -------- str.expandtabs r"r~rrMrMrNr" r"cCr)a For each element, return the lowest index in the string where substring `sub` is found. Calls `str.find` element-wise. For each element, return the lowest index in the string where substring `sub` is found, such that `sub` is contained in the range [`start`, `end`]. Parameters ---------- a : array_like of str or unicode sub : str or unicode start, end : int, optional Optional arguments `start` and `end` are interpreted as in slice notation. Returns ------- out : ndarray or int Output array of ints. Returns -1 if `sub` is not found. See Also -------- str.find r#rrrMrMrNr#s r#cCr)a Like `find`, but raises `ValueError` when the substring is not found. Calls `str.index` element-wise. Parameters ---------- a : array_like of str or unicode sub : str or unicode start, end : int, optional Returns ------- out : ndarray Output array of ints. Returns -1 if `sub` is not found. See Also -------- find, str.find r$rrrMrMrNr$sr$cCrk)a Returns true for each element if all characters in the string are alphanumeric and there is at least one character, false otherwise. Calls `str.isalnum` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like of str or unicode Returns ------- out : ndarray Output array of str or unicode, depending on input type See Also -------- str.isalnum r%rrrYrMrMrNr% r%cCrk)a Returns true for each element if all characters in the string are alphabetic and there is at least one character, false otherwise. Calls `str.isalpha` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like of str or unicode Returns ------- out : ndarray Output array of bools See Also -------- str.isalpha r&rrYrMrMrNr&rr&cCrk)a Returns true for each element if all characters in the string are digits and there is at least one character, false otherwise. Calls `str.isdigit` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like of str or unicode Returns ------- out : ndarray Output array of bools See Also -------- str.isdigit r'rrYrMrMrNr'!rr'cCrk)a Returns true for each element if all cased characters in the string are lowercase and there is at least one cased character, false otherwise. Calls `str.islower` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like of str or unicode Returns ------- out : ndarray Output array of bools See Also -------- str.islower r(rrYrMrMrNr(; r(cCrk)a Returns true for each element if there are only whitespace characters in the string and there is at least one character, false otherwise. Calls `str.isspace` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like of str or unicode Returns ------- out : ndarray Output array of bools See Also -------- str.isspace r)rrYrMrMrNr)Vrr)cCrk)a Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise. Call `str.istitle` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like of str or unicode Returns ------- out : ndarray Output array of bools See Also -------- str.istitle r*rrYrMrMrNr*qrr*cCrk)a Returns true for each element if all cased characters in the string are uppercase and there is at least one character, false otherwise. Call `str.isupper` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like of str or unicode Returns ------- out : ndarray Output array of bools See Also -------- str.isupper r+rrYrMrMrNr+rr+cCr\r]rMsepseqrMrMrN_join_dispatcherrbrcCr|)a Return a string which is the concatenation of the strings in the sequence `seq`. Calls `str.join` element-wise. Parameters ---------- sep : array_like of str or unicode seq : array_like of str or unicode Returns ------- out : ndarray Output array of str or unicode, depending on input types See Also -------- str.join r,r~rrMrMrNr,sr,cCrhr]rMrrMrMrN_just_dispatcherrjrcCr)a Return an array with the elements of `a` left-justified in a string of length `width`. Calls `str.ljust` element-wise. Parameters ---------- a : array_like of str or unicode width : int The length of the resulting strings fillchar : str or unicode, optional The character to use for padding Returns ------- out : ndarray Output array of str or unicode, depending on input type See Also -------- str.ljust r-rrrMrMrNr-  r-cCr)ag Return an array with the elements converted to lowercase. Call `str.lower` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like, {str, unicode} Input array. Returns ------- out : ndarray, {str, unicode} Output array of str or unicode, depending on input type See Also -------- str.lower Examples -------- >>> c = np.array(['A1B C', '1BCA', 'BCA1']); c array(['A1B C', '1BCA', 'BCA1'], dtype='>> np.char.lower(c) array(['a1b c', '1bca', 'bca1'], dtype='>> c = np.array(['aAaAaA', ' aA ', 'abBABba']) >>> c array(['aAaAaA', ' aA ', 'abBABba'], dtype='>> np.char.lstrip(c, 'a') array(['AaAaA', ' aA ', 'bBABba'], dtype='>> np.char.lstrip(c, 'A') # leaves c unchanged array(['aAaAaA', ' aA ', 'abBABba'], dtype='>> (np.char.lstrip(c, ' ') == np.char.lstrip(c, '')).all() ... # XXX: is this a regression? This used to return True ... # np.char.lstrip(c,'') does not modify c at all. False >>> (np.char.lstrip(c, ' ') == np.char.lstrip(c, None)).all() True r/rrZrrwrMrMrNr/s 3r/cCrhr]rMrZrrMrMrN_partition_dispatcherKrjrcCr|)aU Partition each element in `a` around `sep`. Calls `str.partition` element-wise. For each element in `a`, split the element as the first occurrence of `sep`, and return 3 strings containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return 3 strings containing the string itself, followed by two empty strings. Parameters ---------- a : array_like, {str, unicode} Input array sep : {str, unicode} Separator to split each string element in `a`. Returns ------- out : ndarray, {str, unicode} Output array of str or unicode, depending on input type. The output array will have an extra dimension with 3 elements per input element. See Also -------- str.partition r0r~rrMrMrNr0Orr0cCrhr]rMrZoldnewrrMrMrN_replace_dispatchersrjrcCstt|td||gt|S)a% For each element in `a`, return a copy of the string with all occurrences of substring `old` replaced by `new`. Calls `str.replace` element-wise. Parameters ---------- a : array-like of str or unicode old, new : str or unicode count : int, optional If the optional argument `count` is given, only the first `count` occurrences are replaced. Returns ------- out : ndarray Output array of str or unicode, depending on input type See Also -------- str.replace r1rrrMrMrNr1ws r1cCr)a. For each element in `a`, return the highest index in the string where substring `sub` is found, such that `sub` is contained within [`start`, `end`]. Calls `str.rfind` element-wise. Parameters ---------- a : array-like of str or unicode sub : str or unicode start, end : int, optional Optional arguments `start` and `end` are interpreted as in slice notation. Returns ------- out : ndarray Output array of ints. Return -1 on failure. See Also -------- str.rfind r2rrrMrMrNr2sr2cCr)ax Like `rfind`, but raises `ValueError` when the substring `sub` is not found. Calls `str.rindex` element-wise. Parameters ---------- a : array-like of str or unicode sub : str or unicode start, end : int, optional Returns ------- out : ndarray Output array of ints. See Also -------- rfind, str.rindex r3rrrMrMrNr3sr3cCr)a Return an array with the elements of `a` right-justified in a string of length `width`. Calls `str.rjust` element-wise. Parameters ---------- a : array_like of str or unicode width : int The length of the resulting strings fillchar : str or unicode, optional The character to use for padding Returns ------- out : ndarray Output array of str or unicode, depending on input type See Also -------- str.rjust r4rrrMrMrNr4rr4cCr|)af Partition (split) each element around the right-most separator. Calls `str.rpartition` element-wise. For each element in `a`, split the element as the last occurrence of `sep`, and return 3 strings containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return 3 strings containing the string itself, followed by two empty strings. Parameters ---------- a : array_like of str or unicode Input array sep : str or unicode Right-most separator to split each element in array. Returns ------- out : ndarray Output array of string or unicode, depending on input type. The output array will have an extra dimension with 3 elements per input element. See Also -------- str.rpartition r5r~rrMrMrNr5rr5cCrhr]rMrZrmaxsplitrMrMrN_split_dispatcherrjrcCt|td|gt|S)a For each element in `a`, return a list of the words in the string, using `sep` as the delimiter string. Calls `str.rsplit` element-wise. Except for splitting from the right, `rsplit` behaves like `split`. Parameters ---------- a : array_like of str or unicode sep : str or unicode, optional If `sep` is not specified or None, any whitespace string is a separator. maxsplit : int, optional If `maxsplit` is given, at most `maxsplit` splits are done, the rightmost ones. Returns ------- out : ndarray Array of list objects See Also -------- str.rsplit, split r6rrrVrrMrMrNr6#s"r6cCrhr]rMrrMrMrNrIrjcCr)a For each element in `a`, return a copy with the trailing characters removed. Calls `str.rstrip` element-wise. Parameters ---------- a : array-like of str or unicode chars : str or unicode, optional The `chars` argument is a string specifying the set of characters to be removed. If omitted or None, the `chars` argument defaults to removing whitespace. The `chars` argument is not a suffix; rather, all combinations of its values are stripped. Returns ------- out : ndarray Output array of str or unicode, depending on input type See Also -------- str.rstrip Examples -------- >>> c = np.array(['aAaAaA', 'abBABba'], dtype='S7'); c array(['aAaAaA', 'abBABba'], dtype='|S7') >>> np.char.rstrip(c, b'a') array(['aAaAaA', 'abBABb'], dtype='|S7') >>> np.char.rstrip(c, b'A') array(['aAaAa', 'abBABba'], dtype='|S7') r7rrrMrMrNr7Ms )r7cCr)a6 For each element in `a`, return a list of the words in the string, using `sep` as the delimiter string. Calls `str.split` element-wise. Parameters ---------- a : array_like of str or unicode sep : str or unicode, optional If `sep` is not specified or None, any whitespace string is a separator. maxsplit : int, optional If `maxsplit` is given, at most `maxsplit` splits are done. Returns ------- out : ndarray Array of list objects See Also -------- str.split, rsplit r8rrrMrMrNr8zsr8cCrhr]rMrZkeependsrMrMrN_splitlines_dispatcherrjrcCst|tdt|S)a For each element in `a`, return a list of the lines in the element, breaking at line boundaries. Calls `str.splitlines` element-wise. Parameters ---------- a : array_like of str or unicode keepends : bool, optional Line breaks are not included in the resulting list unless keepends is given and true. Returns ------- out : ndarray Array of list objects See Also -------- str.splitlines r9rrrMrMrNr9s r9cCrhr]rMrZprefixrrrMrMrN_startswith_dispatcherrjrcCr)a Returns a boolean array which is `True` where the string element in `a` starts with `prefix`, otherwise `False`. Calls `str.startswith` element-wise. Parameters ---------- a : array_like of str or unicode prefix : str start, end : int, optional With optional `start`, test beginning at that position. With optional `end`, stop comparing at that position. Returns ------- out : ndarray Array of booleans See Also -------- str.startswith r:rrrMrMrNr:sr:cCst|}t||jdt|S)a For each element in `a`, return a copy with the leading and trailing characters removed. Calls `str.strip` element-wise. Parameters ---------- a : array-like of str or unicode chars : str or unicode, optional The `chars` argument is a string specifying the set of characters to be removed. If omitted or None, the `chars` argument defaults to removing whitespace. The `chars` argument is not a prefix or suffix; rather, all combinations of its values are stripped. Returns ------- out : ndarray Output array of str or unicode, depending on input type See Also -------- str.strip Examples -------- >>> c = np.array(['aAaAaA', ' aA ', 'abBABba']) >>> c array(['aAaAaA', ' aA ', 'abBABba'], dtype='>> np.char.strip(c) array(['aAaAaA', 'aA', 'abBABba'], dtype='>> np.char.strip(c, 'a') # 'a' unstripped from c[1] because whitespace leads array(['AaAaA', ' aA ', 'bBABb'], dtype='>> np.char.strip(c, 'A') # 'A' unstripped from c[1] because (unprinted) ws trails array(['aAaAa', ' aA ', 'abBABba'], dtype='>> c=np.array(['a1B c','1b Ca','b Ca1','cA1b'],'S5'); c array(['a1B c', '1b Ca', 'b Ca1', 'cA1b'], dtype='|S5') >>> np.char.swapcase(c) array(['A1b C', '1B cA', 'B cA1', 'Ca1B'], dtype='|S5') r<rrrMrMrNr<s "r<cCr)a Return element-wise title cased version of string or unicode. Title case words start with uppercase characters, all remaining cased characters are lowercase. Calls `str.title` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like, {str, unicode} Input array. Returns ------- out : ndarray Output array of str or unicode, depending on input type See Also -------- str.title Examples -------- >>> c=np.array(['a1b c','1b ca','b ca1','ca1b'],'S5'); c array(['a1b c', '1b ca', 'b ca1', 'ca1b'], dtype='|S5') >>> np.char.title(c) array(['A1B C', '1B Ca', 'B Ca1', 'Ca1B'], dtype='|S5') r=rrrMrMrNr=6s $r=cCrhr]rM)rZtable deletecharsrMrMrN_translate_dispatcher^rjrcCsDt|}t|jjtrt||jd|fSt||jd|gt|S)a, For each element in `a`, return a copy of the string where all characters occurring in the optional argument `deletechars` are removed, and the remaining characters have been mapped through the given translation table. Calls `str.translate` element-wise. Parameters ---------- a : array-like of str or unicode table : str of length 256 deletechars : str Returns ------- out : ndarray Output array of str or unicode, depending on input type See Also -------- str.translate r>)rHrCrGrIrJrrrV)rZrrrwrMrMrNr>bs  r>cCr)ah Return an array with the elements converted to uppercase. Calls `str.upper` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like, {str, unicode} Input array. Returns ------- out : ndarray, {str, unicode} Output array of str or unicode, depending on input type See Also -------- str.upper Examples -------- >>> c = np.array(['a1b c', '1bca', 'bca1']); c array(['a1b c', '1bca', 'bca1'], dtype='>> np.char.upper(c) array(['A1B C', '1BCA', 'BCA1'], dtype='d?Zd@dAZ dBdCZ!dDdEZ"dFdGZ#dHdIZ$dJdKZ%dzdLdMZ&dNdOZ'd~dPdQZ(dRdSZ)d~dTdUZ*d{dVdWZ+d{dXdYZ,dzdZd[Z-d\d]Z.d|d^d_Z/d~d`daZ0d|dbdcZ1d~dddeZ2d{dfdgZ3d~dhdiZ4djdkZ5dldmZ6d~dndoZ7dpdqZ8drdsZ9dtduZ:dvdwZ;dS) chararraya chararray(shape, itemsize=1, unicode=False, buffer=None, offset=0, strides=None, order=None) Provides a convenient view on arrays of string and unicode values. .. note:: The `chararray` class exists for backwards compatibility with Numarray, it is not recommended for new development. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of `dtype` `object_`, `string_` or `unicode_`, and use the free functions in the `numpy.char` module for fast vectorized string operations. Versus a regular NumPy array of type `str` or `unicode`, this class adds the following functionality: 1) values automatically have whitespace removed from the end when indexed 2) comparison operators automatically remove whitespace from the end when comparing values 3) vectorized string operations are provided as methods (e.g. `.endswith`) and infix operators (e.g. ``"+", "*", "%"``) chararrays should be created using `numpy.char.array` or `numpy.char.asarray`, rather than this constructor directly. This constructor creates the array, using `buffer` (with `offset` and `strides`) if it is not ``None``. If `buffer` is ``None``, then constructs a new array with `strides` in "C order", unless both ``len(shape) >= 2`` and ``order='F'``, in which case `strides` is in "Fortran order". Methods ------- astype argsort copy count decode dump dumps encode endswith expandtabs fill find flatten getfield index isalnum isalpha isdecimal isdigit islower isnumeric isspace istitle isupper item join ljust lower lstrip nonzero put ravel repeat replace reshape resize rfind rindex rjust rsplit rstrip searchsorted setfield setflags sort split splitlines squeeze startswith strip swapaxes swapcase take title tofile tolist tostring translate transpose upper view zfill Parameters ---------- shape : tuple Shape of the array. itemsize : int, optional Length of each array element, in number of characters. Default is 1. unicode : bool, optional Are the array elements of type unicode (True) or string (False). Default is False. buffer : object exposing the buffer interface or str, optional Memory address of the start of the array data. Default is None, in which case a new array is created. offset : int, optional Fixed stride displacement from the beginning of an axis? Default is 0. Needs to be >=0. strides : array_like of ints, optional Strides for the array (see `ndarray.strides` for full description). Default is None. order : {'C', 'F'}, optional The order in which the array data is stored in memory: 'C' -> "row major" order (the default), 'F' -> "column major" (Fortran) order. Examples -------- >>> charar = np.chararray((3, 3)) >>> charar[:] = 'a' >>> charar chararray([[b'a', b'a', b'a'], [b'a', b'a', b'a'], [b'a', b'a', b'a']], dtype='|S1') >>> charar = np.chararray(charar.shape, itemsize=5) >>> charar[:] = 'abc' >>> charar chararray([[b'abc', b'abc', b'abc'], [b'abc', b'abc', b'abc'], [b'abc', b'abc', b'abc']], dtype='|S5') rFNrCc Cs|rt}nt}t|}t|tr|} d}nd} da|dur)tj||||f|d} ntj||||f||||d} | dur?| | d<da| S)Nrorder)bufferoffsetstridesr.r)rrrvrErF _globalvarr __new__) subtypeshaperXunicoderrrrrIfillerselfrMrMrNrs, zchararray.__new__cCs ts |jjdvrtddSdS)NSUbcz-Can only create a chararray from string data.)rrIcharrt)robjrMrMrN__array_finalize__szchararray.__array_finalize__cCs:t||}t|tr|}t|dkrd}|S|}|S)Nr)r __getitem__rEr r7len)rrvaltemprMrMrNrs   zchararray.__getitem__cC t||S)zg Return (self == other) element-wise. See Also -------- equal )rrotherrMrMrN__eq__ zchararray.__eq__cCr)zk Return (self != other) element-wise. See Also -------- not_equal )rrrMrMrN__ne__rzchararray.__ne__cCr)zo Return (self >= other) element-wise. See Also -------- greater_equal )rrrMrMrN__ge__rzchararray.__ge__cCr)zl Return (self <= other) element-wise. See Also -------- less_equal )rrrMrMrN__le__rzchararray.__le__cCr)zh Return (self > other) element-wise. See Also -------- greater )rrrMrMrN__gt__rzchararray.__gt__cCr)ze Return (self < other) element-wise. See Also -------- less )rrrMrMrN__lt__rzchararray.__lt__cCtt||S)z Return (self + other), that is string concatenation, element-wise for a pair of array_likes of str or unicode. See Also -------- add )rCrrrMrMrNrm zchararray.__add__cCsttt||S)z Return (other + self), that is string concatenation, element-wise for a pair of array_likes of `string_` or `unicode_`. See Also -------- add )rCrrHrrMrMrN__radd__s zchararray.__radd__cCrz Return (self * i), that is string multiple concatenation, element-wise. See Also -------- multiply rCrrrqrMrMrNrs%rzchararray.__mul__cCrrrrrMrMrN__rmul__0rzchararray.__rmul__cCr)z Return (self % i), that is pre-Python 2.6 string formatting (interpolation), element-wise for a pair of array_likes of `string_` or `unicode_`. See Also -------- mod )rCrrrMrMrNr}; zchararray.__mod__cCstSr])NotImplementedrrMrMrN__rmod__Gszchararray.__rmod__cCs||||S)a Return the indices that sort the array lexicographically. For full documentation see `numpy.argsort`, for which this method is in fact merely a "thin wrapper." Examples -------- >>> c = np.array(['a1b c', '1b ca', 'b ca1', 'Ca1b'], 'S5') >>> c = c.view(np.chararray); c chararray(['a1b c', '1b ca', 'b ca1', 'Ca1b'], dtype='|S5') >>> c[c.argsort()] chararray(['1b ca', 'Ca1b', 'a1b c', 'b ca1'], dtype='|S5') ) __array__argsort)raxiskindrrMrMrNrJszchararray.argsortcC tt|S)z Return a copy of `self` with only the first character of each element capitalized. See Also -------- char.capitalize )rCrrrMrMrNr_ zchararray.capitalizercCtt|||S)z Return a copy of `self` with its elements centered in a string of length `width`. See Also -------- center )rCrrrrrMrMrNrks zchararray.centercCt||||S)z Returns an array with the number of non-overlapping occurrences of substring `sub` in the range [`start`, `end`]. See Also -------- char.count )rrrrrrMrMrNrvrzchararray.countcC t|||S)zj Calls `str.decode` element-wise. See Also -------- char.decode )rrrrrMrMrNr zchararray.decodecCr)zj Calls `str.encode` element-wise. See Also -------- char.encode )r rrMrMrNr rzchararray.encodecCr)z Returns a boolean array which is `True` where the string element in `self` ends with `suffix`, otherwise `False`. See Also -------- char.endswith )r!)rrrrrMrMrNr!rzchararray.endswithrcCr)z Return a copy of each string element where all tab characters are replaced by one or more spaces. See Also -------- char.expandtabs )rCr")rrrMrMrNr"rzchararray.expandtabscCr)z For each element, return the lowest index in the string where substring `sub` is found. See Also -------- char.find )r#rrMrMrNr#rzchararray.findcCr)z Like `find`, but raises `ValueError` when the substring is not found. See Also -------- char.index )r$rrMrMrNr$rzchararray.indexcCt|S)z Returns true for each element if all characters in the string are alphanumeric and there is at least one character, false otherwise. See Also -------- char.isalnum )r%rrMrMrNr% zchararray.isalnumcCr)z Returns true for each element if all characters in the string are alphabetic and there is at least one character, false otherwise. See Also -------- char.isalpha )r&rrMrMrNr&rzchararray.isalphacCr)z Returns true for each element if all characters in the string are digits and there is at least one character, false otherwise. See Also -------- char.isdigit )r'rrMrMrNr' zchararray.isdigitcCr)z Returns true for each element if all cased characters in the string are lowercase and there is at least one cased character, false otherwise. See Also -------- char.islower )r(rrMrMrNr(rzchararray.islowercCr)z Returns true for each element if there are only whitespace characters in the string and there is at least one character, false otherwise. See Also -------- char.isspace )r)rrMrMrNr)rzchararray.isspacecCr)z Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise. See Also -------- char.istitle )r*rrMrMrNr* r zchararray.istitlecCr)z Returns true for each element if all cased characters in the string are uppercase and there is at least one character, false otherwise. See Also -------- char.isupper )r+rrMrMrNr+ rzchararray.isuppercCr)z Return a string which is the concatenation of the strings in the sequence `seq`. See Also -------- char.join )r,)rrrMrMrNr,  zchararray.joincCr)z Return an array with the elements of `self` left-justified in a string of length `width`. See Also -------- char.ljust )rCr-rrMrMrNr-,  zchararray.ljustcCr)z Return an array with the elements of `self` converted to lowercase. See Also -------- char.lower )rCr.rrMrMrNr.8 rzchararray.lowercCr)z For each element in `self`, return a copy with the leading characters removed. See Also -------- char.lstrip )rCr/rrrMrMrNr/D rzchararray.lstripcCr)zu Partition each element in `self` around `sep`. See Also -------- partition )rCr0rrrMrMrNr0P zchararray.partitioncCstt||||S)z For each element in `self`, return a copy of the string with all occurrences of substring `old` replaced by `new`. See Also -------- char.replace )rCr1)rrrrrMrMrNr1Z s zchararray.replacecCr)z For each element in `self`, return the highest index in the string where substring `sub` is found, such that `sub` is contained within [`start`, `end`]. See Also -------- char.rfind )r2rrMrMrNr2f s zchararray.rfindcCr)z Like `rfind`, but raises `ValueError` when the substring `sub` is not found. See Also -------- char.rindex )r3rrMrMrNr3s rzchararray.rindexcCr)z Return an array with the elements of `self` right-justified in a string of length `width`. See Also -------- char.rjust )rCr4rrMrMrNr4 r zchararray.rjustcCr)zv Partition each element in `self` around `sep`. See Also -------- rpartition )rCr5r rMrMrNr5 rzchararray.rpartitioncCr)z For each element in `self`, return a list of the words in the string, using `sep` as the delimiter string. See Also -------- char.rsplit )r6rrrrMrMrNr6 rzchararray.rsplitcCr)z For each element in `self`, return a copy with the trailing characters removed. See Also -------- char.rstrip )rCr7r rMrMrNr7 rzchararray.rstripcCr)z For each element in `self`, return a list of the words in the string, using `sep` as the delimiter string. See Also -------- char.split )r8rrMrMrNr8 rzchararray.splitcCr)z For each element in `self`, return a list of the lines in the element, breaking at line boundaries. See Also -------- char.splitlines )r9)rrrMrMrNr9 r zchararray.splitlinescCr)z Returns a boolean array which is `True` where the string element in `self` starts with `prefix`, otherwise `False`. See Also -------- char.startswith )r:)rrrrrMrMrNr: rzchararray.startswithcCr)z For each element in `self`, return a copy with the leading and trailing characters removed. See Also -------- char.strip )rCr;r rMrMrNr; rzchararray.stripcCr)z For each element in `self`, return a copy of the string with uppercase characters converted to lowercase and vice versa. See Also -------- char.swapcase )rCr<rrMrMrNr< rzchararray.swapcasecCr)z For each element in `self`, return a titlecased version of the string: words start with uppercase characters, all remaining cased characters are lowercase. See Also -------- char.title )rCr=rrMrMrNr= s zchararray.titlecCr)aB For each element in `self`, return a copy of the string where all characters occurring in the optional argument `deletechars` are removed, and the remaining characters have been mapped through the given translation table. See Also -------- char.translate )rCr>)rrrrMrMrNr> s zchararray.translatecCr)z Return an array with the elements of `self` converted to uppercase. See Also -------- char.upper )rCr?rrMrMrNr? rzchararray.uppercCr)z Return the numeric string left-filled with zeros in a string of length `width`. See Also -------- char.zfill )rCr@)rrrMrMrNr@ rzchararray.zfillcCr)z For each element in `self`, return True if there are only numeric characters in the element. See Also -------- char.isnumeric )rArrMrMrNrA r zchararray.isnumericcCr)z For each element in `self`, return True if there are only decimal characters in the element. See Also -------- char.isdecimal )rBrrMrMrNrB( r zchararray.isdecimal)rFNrNr)rNNrrNNNrr])<__name__ __module__ __qualname____doc__rrrrrrrrrrmrrsrr}rrr rrrrr r!r"r#r$r%r&r'r(r)r*r+r,r-r.r/r0r1r2r3r4r5r6r7r8r9r:r;r<r=r>r?r@rArBrMrMrMrNr sz  #                            rTcCst|ttfr,|durt|trd}nd}|durt|}t||}t|||||dSt|ttfr8t|}t|t rt |j j t rt|tsN|t}|dur`|j}t |j j tr`|d}|durpt |j j trnd}nd}|rut}nt}|durtj||d}|s||jks|st|ts|rt|tr||t|f}|St|t rt |j j tr|dur|}|rt}nt}|durt|||dd}n t|||f|dd}|tS)a Create a `chararray`. .. note:: This class is provided for numarray backward-compatibility. New code (not concerned with numarray compatibility) should use arrays of type `string_` or `unicode_` and use the free functions in :mod:`numpy.char ` for fast vectorized string operations instead. Versus a regular NumPy array of type `str` or `unicode`, this class adds the following functionality: 1) values automatically have whitespace removed from the end when indexed 2) comparison operators automatically remove whitespace from the end when comparing values 3) vectorized string operations are provided as methods (e.g. `str.endswith`) and infix operators (e.g. ``+, *, %``) Parameters ---------- obj : array of str or unicode-like itemsize : int, optional `itemsize` is the number of characters per scalar in the resulting array. If `itemsize` is None, and `obj` is an object array or a Python list, the `itemsize` will be automatically determined. If `itemsize` is provided and `obj` is of type str or unicode, then the `obj` string will be chunked into `itemsize` pieces. copy : bool, optional If true (default), then the object is copied. Otherwise, a copy will only be made if __array__ returns a copy, if obj is a nested sequence, or if a copy is needed to satisfy any of the other requirements (`itemsize`, unicode, `order`, etc.). unicode : bool, optional When true, the resulting `chararray` can contain Unicode characters, when false only 8-bit characters. If unicode is None and `obj` is one of the following: - a `chararray`, - an ndarray of type `str` or `unicode` - a Python str or unicode object, then the unicode setting of the output array will be automatically determined. order : {'C', 'F', 'A'}, optional Specify the order of the array. If order is 'C' (default), then the array will be in C-contiguous order (last-index varies the fastest). If order is 'F', then the returned array will be in Fortran-contiguous order (first-index varies the fastest). If order is 'A', then the returned array may be in any order (either C-, Fortran-contiguous, or even discontiguous). NTF)rXrrrrWr)rIrsubok)rEbytesrFrrlisttuplerHrCr rGrIrJr viewrXrrastypervobjectrPnarray)rrXcopyrrrrIrrMrMrNr 5 sf>       r cCst||d||dS)a Convert the input to a `chararray`, copying the data only if necessary. Versus a regular NumPy array of type `str` or `unicode`, this class adds the following functionality: 1) values automatically have whitespace removed from the end when indexed 2) comparison operators automatically remove whitespace from the end when comparing values 3) vectorized string operations are provided as methods (e.g. `str.endswith`) and infix operators (e.g. ``+``, ``*``,``%``) Parameters ---------- obj : array of str or unicode-like itemsize : int, optional `itemsize` is the number of characters per scalar in the resulting array. If `itemsize` is None, and `obj` is an object array or a Python list, the `itemsize` will be automatically determined. If `itemsize` is provided and `obj` is of type str or unicode, then the `obj` string will be chunked into `itemsize` pieces. unicode : bool, optional When true, the resulting `chararray` can contain Unicode characters, when false only 8-bit characters. If unicode is None and `obj` is one of the following: - a `chararray`, - an ndarray of type `str` or 'unicode` - a Python str or unicode object, then the unicode setting of the output array will be automatically determined. order : {'C', 'F'}, optional Specify the order of the array. If order is 'C' (default), then the array will be in C-contiguous order (last-index varies the fastest). If order is 'F', then the returned array will be in Fortran-contiguous order (first-index varies the fastest). F)r rrr )rrXrrrMrMrNrC s0rCr]rrrr)NTNN)NNN)gr functoolssys numerictypesrrrrrrr numericr r r rnumpy.core.multiarrayrnumpy.core.overridesr numpy.corer numpy.compatrrH__all__rpartialarray_function_dispatchrOrRrVr[rarrrrrrrirrrrrr{rrrrrrrrr rr!rr"r#r$r%r&r'r(r)r*r+rr,rr-r.rr/rr0rr1r2r3r4r5rr6r7r8rr9rr:r;r<r=rr>r?rr@rArBrrCrMrMrMrNs*$                   &  $  - -  #  + # #           #  " 6  #    #  #  % ,  "    , %  ' $ "    -