自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篇文档了解更多细节.

Python-3.x相关问答推荐

网站抓取:当我使用Chrome DevTools中的网络选项卡时,找不到正确的URL来提供我想要的数据

丢弃重复的索引,并在多索引数据帧中保留一个

Pandas 插入的速度太慢了.对于跟踪代码,什么是更快的替代方案?

Python根据条件从多行读取值

如何将函数映射到所有命名元组的元素?

从 https://www.niftytrader.in/stock-options-chart/sbin 提取 SBIN 股票最大痛苦值的 Python 代码不起作用 - 我错过了什么?

转换Pandas 数据框 - 添加行

Python 3 - 给定未知数量的类别动态地将字典嵌套到列表中

Pandas 按值和索引对 DF 进行排序

Python.在循环中填充字典的问题

XPATH:使用 .find_elements_by_xpath 为未知数量的 xpath 输入值

使用一周的特定第一天将每日日期转换为每周

python 3:如何判断一个对象是否是一个函数?

pandas 中 df.reindex() 和 df.set_index() 方法的区别

通过多个键对字典列表进行分组和聚合

在 WSL (Ubuntu) 中为 python3 安装 venv

无 Python 错误/错误?

Python 3.4 多处理队列比 Pipe 快,出乎意料

带有 Emacs 的 Python 3

如何更改 tkinter 文本小部件中某些单词的 colored颜色 ?