这个网站上有许多答案,详细说明了如何忽略python中的特定警告(by categoryproviding a regex to match a warning message).

然而,当我试图 suppress 来自PyTables的PerformanceWarning个词时,这些似乎都不起作用.

以下是一份MWE:

import pandas as pd 
import warnings 
from tables import NaturalNameWarning, PerformanceWarning

data = {
    'a' : 1,
    'b' : 'two'
} 
df = pd.DataFrame.from_dict(data, orient = 'index') # mixed types will trigger PerformanceWarning

dest = pd.HDFStore('warnings.h5', 'w') 

#dest.put('data', df) # mixed type will produce a PerformanceWarning
#dest.put('data 1', df) # space in 'data 1' will trigger NaturalNameWarning in addition to the PerformanceWarning

warnings.filterwarnings('ignore', category = NaturalNameWarning) # NaturalNameWarnings ignored 
warnings.filterwarnings('ignore', category = PerformanceWarning) # no effect
warnings.filterwarnings('ignore', message='.*PyTables will pickle') # no effect
#warnings.filterwarnings('ignore') # kills all warnings, not what I want

dest.put('data 2', df) # PerformanceWarning

dest.close()

使用上下文管理器也无济于事:

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=PerformanceWarning) # no effect
    warnings.filterwarnings('ignore', message='.*PyTables') # no effect
    dest.put('data 6', df)

warnings.simplefilter()代替warnings.filterwarnings()也不起作用.

或许与此相关,以下是PerformanceWarning:

test.py:21: PerformanceWarning: 
your performance may suffer as PyTables will pickle object types that it cannot
map directly to c-types [inferred_type->mixed-integer,key->block0_values] [items->Int64Index([0], dtype='int64')]

  dest.put('data 2', df) # PerformanceWarning

与此形成对比的是NaturalNameWarning,它不是来自test.py的违规线路,而是来自tables/path.py:

/home/user/.local/lib/python3.8/site-packages/tables/path.py:137: NaturalNameWarning: object name is not a valid Python identifier: 'data 2'; it does not match the pattern ``^[a-zA-Z_][a-zA-Z0-9_]*$``; you will not be able to use natural naming to access this object; using ``getattr()`` will still work, though
  check_attribute_name(name)

这是表3.7.0/python3.8.10中的内容.有什么主意吗?

推荐答案

这可能会令人困惑,但PerformanceWarning不是由tables封装发出的,而是由pandas发出的:

try :

from pandas.errors import PerformanceWarning

示例:

import pandas as pd 
import warnings 
from tables import NaturalNameWarning
from pandas.errors import PerformanceWarning

data = {
    'a' : 1,
    'b' : 'two'
} 
df = pd.DataFrame.from_dict(data, orient = 'index')

dest = pd.HDFStore('warnings.h5', 'w') 

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=PerformanceWarning)
    dest.put('data', df) # mixed type will produce a PerformanceWarning
    dest.put('data 1', df) # space in 'data 1' will trigger NaturalNameWarning

dest.close()

在上面的示例中,应该只保留NaturalNameWarning.

Python相关问答推荐

Pydantic 2.7.0模型接受字符串日期时间或无

Pystata:从Python并行运行stata实例

Pytest两个具有无限循环和await命令的Deliverc函数

可变参数数量的重载类型(args或kwargs)

如何从具有不同len的列表字典中创建摘要表?

log 1 p numpy的意外行为

数据抓取失败:寻求帮助

Python虚拟环境的轻量级使用

递归访问嵌套字典中的元素值

形状弃用警告与组合多边形和多边形如何解决

如何在Python中找到线性依赖mod 2

mypy无法推断类型参数.List和Iterable的区别

我对这个简单的异步者的例子有什么错误的理解吗?

如何将一组组合框重置回无 Select tkinter?

获取PANDA GROUP BY转换中的组的名称

当HTTP 201响应包含 Big Data 的POST请求时,应该是什么?  

Scipy差分进化:如何传递矩阵作为参数进行优化?

如何在Python中从html页面中提取html链接?

为什么我只用exec()函数运行了一次文件,而Python却运行了两次?

某些值的数值幂和**之间的差异