NumPy isnumeric() function
In this tutorial, we will cover isnumeric()
function of the char module in the Numpy library.
The isnumeric()
function of the NumPy library returns True if there are only numeric characters in the string, otherwise, this function will return False.
-
This function calls unicode.isnumeric
in an element-wise manner.
-
It is important to note that Numeric characters generally include digit characters and all characters that have the Unicode numeric value property(means characters have a numeric value that can be either decimal, including zero and negatives, or a vulgar fraction)
Syntax for isnumeric()
:
The syntax required to use this function is as follows:
numpy.char.isnumeric(arr)
The above syntax indicates that isnumeric()
function takes a single parameter.
In the above syntax, the argument arr
is mainly used to indicate the input array of the strings or a single string, on which this function will be applied.
Returned Values:
This function will return an output array of boolean values, with True and False values corresponding to every string element, based on whether the string is numeric or not.
Example 1: With a string
In the below example, we will use the isnumeric()
function with a simple string:
import numpy as np
string1 = "12Apple90"
print("The Input string is:")
print(string1)
x = np.char.isnumeric(string1)
print("The Output is:")
print(x)
The Input string is:
12Apple90
The Output is:
False
Example 2: With an array
The code snippet is as follows where we will use isnumeric()
function:
import numpy as np
inp_ar = np.array([ '1', '2000','90','3.5','0'] )
print ("The Input array : ")
print(inp_ar)
outp_arr = np.char.isnumeric(inp_ar)
print ("The Output array: ")
print(outp_arr)
The Input array :
['1' '2000' '90' '3.5' '0']
The Output array:
[ True True True False True]
As you can see in the output for the code example above, the isnumeric()
function returns False for a string with a numeric value with a decimal.
Summary
In this tutorial we learned isnumeric()
function of the Numpy library. We covered how it is used with its syntax and values returned by this function. There are two examples in order to gain an understanding of the working of this function.