使用 warnings.filterwarnings() 过滤 PyTables PerformanceWarning 失败

问题描述 投票:0回答:1

此网站上有许多答案,详细说明了如何忽略 python 中的特定警告(按类别或通过提供正则表达式来匹配警告消息)。

但是,当我尝试抑制来自 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()

也许相关,这里是性能警告:

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/python 3.8.10 的情况。有什么想法吗?

python pandas pytables
1个回答
0
投票

这可能会令人困惑,但

PerformanceWarning
不是由
tables
包发出的,而是由
pandas
发出的:

尝试:

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') # mixed types will trigger PerformanceWarning

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

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=PerformanceWarning) # no effect
    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

© www.soinside.com 2019 - 2024. All rights reserved.