NumPy strings.isdecimal()
The numpy.strings.isdecimal()
function checks if each element in an input array consists only of decimal characters.
It returns a boolean array where each element is True
if the corresponding input element contains only decimal characters and False
otherwise.
Syntax
numpy.strings.isdecimal(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like (StringDType or str_ dtype) | Input array of strings to be checked for decimal characters. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array to store the result. If None, a new array is created. |
where | array_like, optional | Boolean mask that specifies which elements to check. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when performing the operation. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Specifies the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array of boolean values indicating whether each input string contains only decimal characters. If the input is a scalar, a single boolean value is returned.
Examples
1. Checking Decimal Characters in a Single String
Here, we check whether a single string consists entirely of decimal characters.
import numpy as np
# Define a string
string_value = np.str_("12345")
# Check if the string contains only decimal characters
result = np.strings.isdecimal(string_value)
# Print the result
print("Is '12345' decimal?:", result)
Output:
Is '12345' decimal?: True

2. Checking Decimal Characters in an Array of Strings
We apply isdecimal()
to an array of strings to check which elements contain only decimal characters.
import numpy as np
# Define an array of strings
string_array = np.array(["123", "45.6", "789", "abcd", "١٢٣"], dtype="str")
# Check which strings contain only decimal characters
decimal_check = np.strings.isdecimal(string_array)
# Print the results
print("Input Strings:", string_array)
print("Is Decimal:", decimal_check)
Output:
Input Strings: ['123' '45.6' '789' 'abcd' '١٢٣']
Is Decimal: [ True False True False True]

3. Using the out
Parameter
Storing results in an output array instead of creating a new one.
import numpy as np
# Define an array of strings
string_array = np.array(["123", "text", "456", "7.89"], dtype="str")
# Create an output array
output_array = np.empty_like(string_array, dtype=bool)
# Check decimal characters and store result in output_array
np.strings.isdecimal(string_array, out=output_array)
# Print the results
print("Output array:", output_array)
Output:
Output array: [ True False True False]

4. Using the where
Parameter
Using a condition to check decimal characters only for selected elements.
import numpy as np
# Define an array of strings
string_array = np.array(["123", "abc", "987", "45.6"], dtype="str")
# Define a mask (check decimal only where mask is True)
mask = np.array([True, False, True, False])
# Apply isdecimal() where mask is True
result = np.strings.isdecimal(string_array, where=mask)
# Print the results
print("Result with mask:", result)
Output:
Result with mask: [ True False True False]

The function checks only elements where mask=True
. The other values remain unchanged.