Using char.endswith() in NumPy (4 examples)

Updated: March 2, 2024 By: Guest Contributor Post a comment

Introduction

Understanding the specifics of char.endswith() in NumPy can greatly enhance your data processing capabilities, especially when handling text data arrays. This tutorial aims to delve into practical examples that cater to different skill levels – from beginners in Python programming to those with more advanced knowledge. By exploring these examples, you’ll grasp how to apply char.endswith() in a variety of contexts, sharpen your data manipulation skills, and expedite your analytical tasks.

What does char.endswith() Do?

The numpy.char.endswith() function in NumPy is used to check if each element of an array of strings ends with a specified suffix. It returns an array of booleans.

Syntax:

numpy.char.endswith(a, suffix, start=0, end=None)

Parameters:

  • a: array_like of str or unicode. Input array of strings.
  • suffix: str or unicode. The suffix to be searched at the end of the strings in a.
  • start: int, optional. The starting index of the substring within each element of a to be considered for the operation.
  • end: int, optional. The ending index of the substring within each element of a to be considered for the operation. If None, the search considers characters to the end of each string.

Returns:

  • out: ndarray. An array of booleans indicating whether each element of a ends with the specified suffix.

Basic Usage Example

Let’s start with the basics. In this example, we demonstrate how to check if the country names in an array end with the letter 'a'.

import numpy as np

# Sample array of country names
country_names = np.array(['India', 'Canada', 'Australia', 'Japan', 'USA'])

# Checking which names end with 'a'
results = np.char.endswith(country_names, 'a')

print(results)

The output for this would be:

[ True  True  True False False]

This boolean array indicates which elements comply with the condition set by endswith().

Case Sensitivity Handling

Moving on to handling case sensitivity – a common issue while dealing with text data. This example shows how to use endswith() without being affected by character case.

import numpy as np

# Sample data array
file_extensions = np.array(['README.TXT', 'assignment.doc', 'report.PDF', 'presentation.PPTX'])

# Convert array elements to lowercase then check ending
results_case_insensitive = np.char.endswith(np.char.lower(file_extensions), 'txt')

print(results_case_insensitive)

The output demonstrates the function’s flexibility:

[ True False False False]

This approach allows for conducting case-insensitive searches efficiently.

Working With Multiple Suffixes

It’s often useful to check against multiple suffixes, especially when categorizing or filtering data. The following example illustrates how to accomplish this task.

import numpy as np

# Creating an array with various types of files
files = np.array(['photo.jpg', 'document.pdf', 'song.mp3', 'archive.zip', 'presentation.pptx'])

# Define a list of suffixes to check
suffixes = ['pdf', 'zip']

# Checking which files end with either 'pdf' or 'zip'
results_multiple_suffixes = np.array([np.any([np.char.endswith(f, suffix) for suffix in suffixes]) for f in files])

print(results_multiple_suffixes)

The outcome:

[False  True False  True False]

This method requires a little more code but is versatile, allowing checks against a list of given suffixes.

Advanced Example: Using With Structured Arrays

For a more complex scenario, let’s use endswith() with structured arrays. This example demonstrates filtering records based on the suffix of one of the fields in a structured array.

import numpy as np

# Defining a structured array with names and file types
records = np.array([('Alice', 'document.doc'), ('Bob', 'photo.jpg'),
                   ('Charlie', 'data.csv'), ('Diane', 'report.pdf')],
                  dtype=[('Name', 'U10'), ('FileType', 'U15')])

# Filtering records where the FileType ends with 'doc' or 'pdf'
filtered_records = np.array([record for record in records if np.char.endswith(record['FileType'], ('doc', 'pdf'))])

print(filtered_records)

This snippet filters out records where the FileType ends with either ‘doc’ or ‘pdf’, showcasing how endswith() can be creatively applied in data filtering contexts beyond simple string arrays.

Conclusion

Through these four examples, we’ve explored the versatility and efficiency of using char.endswith() in NumPy for text data manipulation. Whether handling simple string checks or complex data filtering, mastering this function can significantly streamline your data processing workflow. Happy coding!