Creating ndarray from ragged nested sequences is deprecated

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
5 min

banner

# Table of Contents

  1. Creating ndarray from ragged nested sequences is deprecated
  2. Set the dtype keyword argument to object
  3. Disabling the VisibleDeprecationWarning NumPy warning
  4. Setting the action argument to error

# Creating ndarray from ragged nested sequences is deprecated

The NumPy "VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated" occurs when you create an array of arrays where the subarrays are of different lengths.

To resolve the issue, set the dtype keyword argument to object or disable the warning.

Here is the complete stack trace.

shell
/home/borislav/Desktop/bobbyhadz_python/main.py:13: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.

creating an ndarray from ragger nested sequences is deprecated

Here is an example of when the warning is shown.

main.py
import numpy as np def get_arr(): arr = np.array([[1], [1, 2], [1, 2, 3]]) return arr # /home/borislav/Desktop/bobbyhadz_python/main.py:13: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. get_arr()

Notice that the subarrays that we passed to the numpy.array method are of different lengths.

A ragged (or jagged) array is an array of arrays (or a list of lists) where the subarrays have different lengths.

This produces rows of jagged edges when visualized as output.

# Set the dtype keyword argument to object

Creating an ndarray from ragged nested sequences is deprecated, however, you can get around this by setting the dtype keyword argument to object.

main.py
import numpy as np def get_arr(): arr = np.array([[1], [1, 2], [1, 2, 3]], dtype=object) return arr result = get_arr() # ๐Ÿ‘‡๏ธ [list([1]) list([1, 2]) list([1, 2, 3])] print(result)
The code for this article is available on GitHub

The dtype (data type) keyword argument is optional and sets the data type for the array.

If the argument is not supplied, then the type is determined as the minimum type required to hold the objects in the sequence.

If you have a ragged nested sequence that you need to convert to an ndarray, use the asarray() method and set the dtype keyword argument to object.

main.py
import numpy as np list_of_lists = [[1], [1, 2], [1, 2, 3]] arr = np.asarray(list_of_lists, dtype=object) # ๐Ÿ‘‡๏ธ [list([1]) list([1, 2]) list([1, 2, 3])] print(arr)

The numpy.asarray() method converts the supplied array-like value to an array.

The dtype keyword argument is optional. If not set, the data type is inferred from the input data.

# Disabling the VisibleDeprecationWarning NumPy warning

You can also disable the VisibleDeprecationWarning NumPy warning as an alternative.

main.py
import numpy as np np.warnings.filterwarnings( 'ignore', category=np.VisibleDeprecationWarning ) def get_arr(): arr = np.array([[1], [1, 2], [1, 2, 3]]) return arr result = get_arr() # ๐Ÿ‘‡๏ธ [list([1]) list([1, 2]) list([1, 2, 3])] print(result)
The code for this article is available on GitHub

If I run the code sample above, no warnings are raised, even though the dtype keyword argument is not set to object.

disable visible deprecation warning

When the first argument of the filterwarnings() method is set to ignore, matching warnings are never printed.

The category keyword argument must be set to a class (a subclass of Warning) and specifies which warning category we want to match.

The approach only silences warnings of type VisibleDeprecationWarning, so all other NumPy warnings will still be printed.

# Setting the action argument to error

If you aren't sure where the warning is raised and you're trying to debug your code, try to set the action argument to error.

main.py
import numpy as np np.warnings.filterwarnings( 'error', category=np.VisibleDeprecationWarning ) def get_arr(): arr = np.array([[1], [1, 2], [1, 2, 3]]) return arr result = get_arr()
The code for this article is available on GitHub

Notice that the action argument is set to error this time.

Running the code above produces the following output.

shell
Traceback (most recent call last): File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 16, in <module> result = get_arr() File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 11, in get_arr arr = np.array([[1], [1, 2], [1, 2, 3]]) numpy.VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.

As shown in the code sample, the error occurred on line 11.

main.py
arr = np.array([[1], [1, 2], [1, 2, 3]])

And the function call that triggered the error was on line 16.

main.py
result = get_arr()

This can help you to track down where the warning is raised.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev