如何摆脱 NaturalNameWarning?

How to get rid of NaturalNameWarning?

我的脚本正在做以下事情:

  1. 从二进制 trc 文件中读取时间序列(UHF 测量)
  2. 裁剪某些区域(脉冲)并将它们中的每一个保存到 pd.DataFrame
  3. 将所有 DataFrames 保存到一个 hdf5 文件中

这工作正常,但 tables 模块似乎为每个 DataFrame.

抛出一个 NaturalNameWarning

这是 DataFrames 保存到 hdf5 的位置:

num = 0
for idx, row in df_oszi.iloc[peaks].iterrows():
    start_peak = idx - 1*1e-3
    end_peak = idx + 10*1e-3  #tges=11us
    df_pos = df_oszi[start_peak:end_peak]
    df_pos.to_hdf('pos.h5', key=str(num))
    num += 1

输出:

Warning (from warnings module):
  File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\tables\path.py", line 157
    check_attribute_name(name)
NaturalNameWarning: object name is not a valid Python identifier: '185'; 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

这是一个警告。这意味着您不能使用 PyTables 自然命名约定来访问名为 185 的数据集。如果您不打算使用 PyTables,这不是问题。如果要使用 PyTables,则必须使用 File.get_node(where) 访问此组名。
两种方法的比较(其中 h5f 是我的 HDF5 文件对象):
h5f.get_node('/185') # works
tb1nn = h5f.root.185 # gives Python invalid syntax error

将群组名称更改为t185,您可以使用自然命名。 请参阅下面的示例 PyTables 代码以显示差异:

import tables as tb
import numpy as np

arr = np.arange(10.)
ds_dt = ds_dt= ( [ ('f1', float) ] ) 
rec_arr = np.rec.array(arr,dtype=ds_dt)

with tb.File('natname.h5','w') as h5f:
    tb1 = h5f.create_table('/','t185',obj=rec_arr)
    tb1nn = h5f.root.t185
    print (tb1nn.nrows)

    tb2 = h5f.create_table('/','185',obj=rec_arr)
#    tb2nn = h5f.root.185 # will give Python syntax error
    tb2un = h5f.get_node('/185')
    print (tb2un.nrows)

只要您真的不打算使用 table 访问权限,您随时都可以这样做。

import warnings
from tables import NaturalNameWarning
warnings.filterwarnings('ignore', category=NaturalNameWarning)