Modify Numpy array to store an arbitrary length string
Last Updated :
06 Mar, 2019
NumPy builds on (and is a successor to) the successful Numeric array object. Its goal is to create the corner-stone for a useful environment for scientific computing. NumPy provides two fundamental objects: an N-dimensional array object (ndarray) and a universal function object (ufunc).
The dtype of any numpy array containing string values is the maximum length of any string present in the array. Once set, it will only be able to store new string having length not more than the maximum length at the time of the creation. If we try to reassign some another string value having length greater than the maximum length of the existing elements, it simply discards all the values beyond the maximum length.
In this post we are going to discuss ways in which we can overcome this problem and create a numpy array of
arbitrary length.
Let's first visualize the problem with creating an arbitrary length numpy array of string type.
Python3 1==
# importing numpy as np
import numpy as np
# Create the numpy array
country = np.array(['USA', 'Japan', 'UK', '', 'India', 'China'])
# Print the array
print(country)
Output :

As we can see in the output, the maximum length of any string length element in the given array is 5. Let's try to assign a value having greater length at the place of missing value in the array.
Python3 1==
# Assign 'New Zealand' at the place of missing value
country[country == ''] = 'New Zealand'
# Print the modified array
print(country)
Output :

As we can see in the output, 'New Z' has been assigned rather than 'New Zealand' because of the limitation to the length. Now, let's see the ways in which we can overcome this problem.
Problem #1 : Create a numpy array of arbitrary length.
Solution : While creating the array assign the 'object' dtype to it. This lets you have all the behaviors of the python string.
Python3 1==
# importing the numpy library as np
import numpy as np
# Create a numpy array
# set the dtype to object
country = np.array(['USA', 'Japan', 'UK', '', 'India', 'China'], dtype = 'object')
# Print the array
print(country)
Output :

Now we will use assign a value of arbitrary length at the place of missing value in the given array.
Python3
# Assign 'New Zealand' to the missing value
country[country == ''] = 'New Zealand'
# Print the array
print(country)
Output :

As we can see in the output, we have successfully assigned an arbitrary length string to the given array object.
Problem #2 : Create a numpy array of arbitrary length.
Solution : We will use the
numpy.astype()
function to change the dtype of the given array object.
Python3 1==
# importing the numpy library as np
import numpy as np
# Create a numpy array
# Notice we have not set the dtype of the object
# this will lead to the length problem
country = np.array(['USA', 'Japan', 'UK', '', 'India', 'China'])
# Print the array
print(country)
Output :

Now we will change the dtype of the given array object using
numpy.astype()
function. Then we will assign an arbitrary length string to it.
Python3
# Change the dtype of the country
# object to 'U256'
country = country.astype('U256')
# Assign 'New Zealand' to the missing value
country[country == ''] = 'New Zealand'
# Print the array
print(country)
Output :

As we can see in the output, we have successfully assigned an arbitrary length string to the given array object.
Note : The maximum length of the string that we can assign in this case after changing the dtype is 256.
Similar Reads
Repeat all the elements of a NumPy array of strings Let us see how to repeat all elements of the given array of string 3 times. Example : Input : ['Akash', 'Rohit', 'Ayush', 'Dhruv', 'Radhika'] Output : ['AkashAkashAkash', 'RohitRohitRohit', 'AyushAyushAyush', 'DhruvDhruvDhruv', 'RadhikaRadhikaRadhika'] We will be using the numpy.char.multiply(a, i)
2 min read
How to save a NumPy array to a text file? When working with data it's important to know how to save NumPy arrays to text files for storage, sharing and further analysis. There are different ways from manual file handling to using specialized NumPy functions. In this article, we will see how to save a NumPy array to a text fileMethod 1: Usin
3 min read
How to convert an array of indices to one-hot encoded NumPy array A very popular technique used in machine learning to transform categorical data into binary values of 0 and 1 is called the one-hot encoding technique. There are various circumstances when you need to use a one-hot encoded NumPy array rather than an array of indices, thus we can convert it using the
3 min read
NumPy save() Method | Save Array to a File The NumPy save() method is used to store the input array in a binary file with the 'npy extension' (.npy). Example: Python3 import numpy as np a = np.arange(5) np.save('array_file', a) SyntaxSyntax: numpy.save(file, arr, allow_pickle=True, fix_imports=True) Parameters: file: File or filename to whic
2 min read
How to Change a Single Value in a NumPy Array NumPy arrays are a fundamental data structure in Python, widely used for scientific computing and data analysis. They offer a powerful way to perform operations on large datasets efficiently. One common task when working with NumPy arrays is changing a single value within the array. This article wil
6 min read
Find the length of each string element in the Numpy array NumPy builds on (and is a successor to) the successful Numeric array object. Its goal is to create the corner-stone for a useful environment for scientific computing. NumPy provides two fundamental objects: an N-dimensional array object (ndarray) and a universal function object (ufunc). In this post
3 min read