Since NumPy version 19.0, one must specify dtype=object
when creating an array from "ragged" sequences. I'm faced with a large number of array calls from my own code and Pandas using threading, line-by-line debugging led me nowhere.
I'd like to figure out which call resulted in VisibleDeprecationWarning in my own code or a call from Pandas. How would I be able to debug this? I've been looking through the source and I cannot see this warning getting called in Python (only in numpy.core._multiarray_umath.cp38-win_amd64.pyd).
I understand that numpy doesn't support ragged arrays, but it is fine for me if the resulting array simply pads each row with a specified value to make all rows the same length. To make this concrete, suppose I have set A with 3 items, B with 2 items, and C with four items.
When users create arrays with sequences-of-sequences, they sometimes err in matching the lengths of the nested sequences, commonly called “ragged arrays”. Here we will refer to them as ragged nested sequences.
This error occurs when you attempt to append one or more values to the end of a NumPy array by using the append() function in regular Python. Since NumPy doesn't have an append attribute, an error is thrown. To fix this, you must use np. append() instead.
numpy. array is just a convenience function to create an ndarray ; it is not a class itself. You can also create an array using numpy. ndarray , but it is not the recommended way.
With a function that creates a ragged array:
In [60]: def foo(): ...: print('one') ...: x = np.array([[1],[1,2]]) ...: return x ...: In [61]: foo() one /usr/local/bin/ipython3:3: 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 # -*- coding: utf-8 -*- Out[61]: array([list([1]), list([1, 2])], dtype=object)
I get the warning, but also the expected result.
I can control the warnings.
For example to turn if off:
In [68]: np.warnings.filterwarnings('ignore', category=np.VisibleDeprecationWarning) In [69]: foo() one Out[69]: array([list([1]), list([1, 2])], dtype=object)
Or to raise an error:
In [70]: np.warnings.filterwarnings('error', category=np.VisibleDeprecationWarning) In [71]: foo() one --------------------------------------------------------------------------- VisibleDeprecationWarning Traceback (most recent call last) <ipython-input-71-c19b6d9633cf> in <module> ----> 1 foo() <ipython-input-60-6ad21d9e07b4> in foo() 1 def foo(): 2 print('one') ----> 3 x = np.array([[1],[1,2]]) 4 return x 5 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
The error gives a traceback telling me where the warning was raised.
There may be ways of refining the warning filter to catch just this one, and not others of the same category. I haven't used this mechanism much.
Read np.warnings.filterwarnings
docs for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With