Skip to content

Commit

Permalink
Merge pull request #25791 from lysnikolaou/string-ufuncs-is-case
Browse files Browse the repository at this point in the history
ENH: Add rest of unary ufuncs for unicode/bytes dtypes
  • Loading branch information
ngoldbaum committed Feb 17, 2024
2 parents cc48de9 + 2214aee commit 9a73ab2
Show file tree
Hide file tree
Showing 10 changed files with 608 additions and 385 deletions.
20 changes: 20 additions & 0 deletions numpy/_core/code_generators/generate_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,26 @@ def english_upper(s):
docstrings.get('numpy._core.umath.isspace'),
None,
),
'isalnum':
Ufunc(1, 1, False_,
docstrings.get('numpy._core.umath.isalnum'),
None,
),
'islower':
Ufunc(1, 1, False_,
docstrings.get('numpy._core.umath.islower'),
None,
),
'isupper':
Ufunc(1, 1, False_,
docstrings.get('numpy._core.umath.isupper'),
None,
),
'istitle':
Ufunc(1, 1, False_,
docstrings.get('numpy._core.umath.istitle'),
None,
),
'isdecimal':
Ufunc(1, 1, False_,
docstrings.get('numpy._core.umath.isdecimal'),
Expand Down
119 changes: 119 additions & 0 deletions numpy/_core/code_generators/ufunc_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4441,6 +4441,125 @@ def add_newdoc(place, name, doc):
""")

add_newdoc('numpy._core.umath', 'isalnum',
"""
Returns true for each element if all characters in the string are
alphanumeric and there is at least one character, false otherwise.
Parameters
----------
x : array_like, with `np.bytes_` or `np.str_` dtype
$PARAMS
Returns
-------
out : ndarray
Output array of bool
$OUT_SCALAR_1
See Also
--------
str.isalnum
Examples
--------
>>> a = np.array(['a', '1', 'a1', '(', ''])
>>> np.strings.isalnum(a)
array([ True, True, True, False, False])
""")

add_newdoc('numpy._core.umath', 'islower',
"""
Returns true for each element if all cased characters in the
string are lowercase and there is at least one cased character,
false otherwise.
Parameters
----------
x : array_like, with `np.bytes_` or `np.str_` dtype
$PARAMS
Returns
-------
out : ndarray
Output array of bools
$OUT_SCALAR_1
See Also
--------
str.islower
Examples
--------
>>> np.strings.islower("GHC")
array(False)
>>> np.strings.islower("ghc")
array(True)
""")

add_newdoc('numpy._core.umath', 'isupper',
"""
Return true for each element if all cased characters in the
string are uppercase and there is at least one character, false
otherwise.
Parameters
----------
x : array_like, with `np.bytes_` or `np.str_` dtype
$PARAMS
Returns
-------
out : ndarray
Output array of bools
$OUT_SCALAR_1
See Also
--------
str.isupper
Examples
--------
>>> np.strings.isupper("GHC")
array(True)
>>> a = np.array(["hello", "HELLO", "Hello"])
>>> np.strings.isupper(a)
array([False, True, False])
""")

add_newdoc('numpy._core.umath', 'istitle',
"""
Returns true for each element if the element is a titlecased
string and there is at least one character, false otherwise.
Parameters
----------
x : array_like, with `np.bytes_` or `np.str_` dtype
$PARAMS
Returns
-------
out : ndarray
Output array of bools
$OUT_SCALAR_1
See Also
--------
str.istitle
Examples
--------
>>> np.strings.istitle("Numpy Is Great")
array(True)
>>> np.strings.istitle("Numpy is great")
array(False)
""")

add_newdoc('numpy._core.umath', 'isdecimal',
"""
For each element, return True if there are only decimal
Expand Down
24 changes: 23 additions & 1 deletion numpy/_core/src/common/numpyos.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,34 @@ NumPyOS_ascii_isdigit(char c)
*
* Same as isalnum under C locale
*/
static int
NPY_NO_EXPORT int
NumPyOS_ascii_isalnum(char c)
{
return NumPyOS_ascii_isdigit(c) || NumPyOS_ascii_isalpha(c);
}

/*
* NumPyOS_ascii_islower:
*
* Same as islower under C locale
*/
NPY_NO_EXPORT int
NumPyOS_ascii_islower(char c)
{
return c >= 'a' && c <= 'z';
}

/*
* NumPyOS_ascii_isupper:
*
* Same as isupper under C locale
*/
NPY_NO_EXPORT int
NumPyOS_ascii_isupper(char c)
{
return c >= 'A' && c <= 'Z';
}


/*
* NumPyOS_ascii_tolower:
Expand Down
9 changes: 9 additions & 0 deletions numpy/_core/src/common/numpyos.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ NumPyOS_ascii_isalpha(char c);
NPY_NO_EXPORT int
NumPyOS_ascii_isdigit(char c);

NPY_NO_EXPORT int
NumPyOS_ascii_isalnum(char c);

NPY_NO_EXPORT int
NumPyOS_ascii_islower(char c);

NPY_NO_EXPORT int
NumPyOS_ascii_isupper(char c);

/* Convert a string to an int in an arbitrary base */
NPY_NO_EXPORT npy_longlong
NumPyOS_strtoll(const char *str, char **endptr, int base);
Expand Down

0 comments on commit 9a73ab2

Please sign in to comment.