Using NumPy char.isnumeric() function (5 examples)

Updated: February 29, 2024 By: Guest Contributor Post a comment

Introduction

NumPy, a cornerstone library for numerical computing in Python, contains a comprehensive set of functions for array manipulation. Among these, the char module offers a host of string operations that can be applied element-wise in arrays. Particularly, char.isnumeric() is a valuable method for data pre-processing, as it checks whether each element in an array of strings consists only of numeric characters. This tutorial aims to demonstrate the effectiveness and versatility of char.isnumeric() in NumPy through five distinct examples.

Prerequisites

To follow along with this tutorial, ensure you have the following:

  • Python 3.6 or newer installed on your machine.
  • NumPy installed. If you haven’t yet, you can install it using pip: pip install numpy.

Example 1: Basic Usage

Firstly, let’s start with the basics of using char.isnumeric(). This example showcases how to determine if the elements in an array are composed solely of numeric characters.

import numpy as np

# Example array
arr = np.array(['123', 'abc', '456abc', '789'])

# Use char.isnumeric()
result = np.char.isnumeric(arr)

print(result)

Output:

[ True  False  False  True]

The output is a boolean array indicating whether each element is numeric.

Example 2: Handling Multidimensional Arrays

NumPy’s char.isnumeric() also gracefully handles multidimensional arrays. Let’s examine how it operates with a 2D array.

import numpy as np

# Creating a 2D array
arr_2d = np.array([["123", "45a"], ["678", "90"]])

# Applying char.isnumeric()
result = np.char.isnumeric(arr_2d)

print(result)

Output:

[[ True False]
 [ True  True]]

This showcases that each element in the 2D array is individually assessed.

Example 3: Integrating with Pandas

Python’s Pandas library is frequently used alongside NumPy for data analysis. We can apply NumPy’s char.isnumeric() function to Pandas DataFrame columns consisting of strings. This example demonstrates this integration.

import numpy as np
import pandas as pd

# Example DataFrame
df = pd.DataFrame({'data': ['123', '456a', '789']})

# Apply char.isnumeric() to a DataFrame column
result = np.char.isnumeric(df['data'].to_numpy())

df['is_numeric'] = result

print(df)

Output:

    data  is_numeric
0    123        True
1   456a       False
2    789        True

This approach is particularly useful in data cleaning and preparation stages.

Example 4: Working with Unicode Strings

NumPy’s char.isnumeric function also supports Unicode numeric characters, such as Roman numerals or currency symbols. This example illustrates its effectiveness with such characters.

import numpy as np

# Unicode string array
uni_arr = np.array(['â…§', 'IV', '", "123'])

# Apply char.isnumeric()
result = np.char.isnumeric(uni_arr)

print(result)

Output:

[ True  False  True]

This reveals that char.isnumeric() is adept at handling an array of Unicode strings, broadening its applicability.

Example 5: Filtering Numeric Values

Combining what we’ve learned, a practical application could involve filtering out non-numeric values from an array. This final example ties together multiple concepts to accomplish that.

import numpy as np

# Complex array mix
mix_arr = np.array(['1', '2a', '3', 'four', '5'])

# Determine numeric values
numeric_mask = np.char.isnumeric(mix_arr)

# Filter only numeric values
filtered_arr = mix_arr[numeric_mask]

print(filtered_arr)

Output:

['1' '3' '5']

The filtered array contains only those elements that are purely numeric.

Conclusion

The char.isnumeric() function in NumPy is a powerful yet straightforward tool for verifying the numeric character of string elements in arrays. Through these examples, we’ve explored its basic usage, handling of multidimensional arrays, integration with Pandas, support for Unicode characters, and a practical application in filtering numeric values. These capabilities make char.isnumeric() an invaluable function in the preprocessing steps of data analysis projects.