这个网站上有许多答案,详细说明了如何忽略python中的特定警告(by category或providing 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中的内容.有什么主意吗?