自NumPy版本19.0以来,从"不规则"序列创建数组时必须指定dtype=object
.我面临着大量来self 自己代码的数组调用,而Pandas 使用线程,逐行调试让我一事无成.
我想知道是哪个调用在我自己的代码中导致了VisibleDeprecationWarning,还是一个来自Pandas 的调用.我怎样才能调试这个?我一直在查看源代码,但我看不到在Python中调用此警告(仅在numpy.core._multiarray_umath.cp38-win_amd64.pyd中).
自NumPy版本19.0以来,从"不规则"序列创建数组时必须指定dtype=object
.我面临着大量来self 自己代码的数组调用,而Pandas 使用线程,逐行调试让我一事无成.
我想知道是哪个调用在我自己的代码中导致了VisibleDeprecationWarning,还是一个来自Pandas 的调用.我怎样才能调试这个?我一直在查看源代码,但我看不到在Python中调用此警告(仅在numpy.core._multiarray_umath.cp38-win_amd64.pyd中).
使用创建不规则数组的函数:
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)
我得到了警告,但也得到了预期的结果.
我可以控制警告.
例如,要在关闭时关闭:
In [68]: np.warnings.filterwarnings('ignore', category=np.VisibleDeprecationWarning)
In [69]: foo()
one
Out[69]: array([list([1]), list([1, 2])], dtype=object)
或者提出一个错误:
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
这个错误提供了一个回溯,告诉我警告是在哪里发出的.
可能有一些方法可以改进警告过滤器,只捕获这一个,而不捕获其他同类.我很少使用这种机制.
阅读np.warnings.filterwarnings
篇文档了解更多细节.