简体   繁体   English

如何摆脱 NaturalNameWarning?

[英]How to get rid of NaturalNameWarning?

My script is doing following things:我的脚本正在做以下事情:

  1. read time series from binary trc file (UHF Measurement)从二进制trc文件中读取时间序列(UHF 测量)
  2. crop certain regions (Impulses) and save each of them into a pd.DataFrame裁剪某些区域(脉冲)并将它们中的每一个保存到pd.DataFrame
  3. save all DataFrames into one hdf5 file将所有DataFrames保存到一个hdf5文件中

This works fine but the tables module seems to throw a NaturalNameWarning for every single DataFrame .这工作正常,但tables模块似乎为每个DataFrame NaturalNameWarning

This is where the DataFrames are saved to the hdf5 :这是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

Output: Output:

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

You can always do this as long as you are not really going to use the table accessing.只要您不真的要使用表访问,您就可以随时执行此操作。

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

This is a warning.这是一个警告。 It means you can't use PyTables natural naming convention to access a dataset named 185 .这意味着您不能使用 PyTables 自然命名约定来访问名为185的数据集。 It's not a problem if you don't plan to use PyTables.如果您不打算使用 PyTables,这不是问题。 If you want to use PyTables, you have to use File.get_node(where) to access this group name.如果你想使用 PyTables,你必须使用File.get_node(where)来访问这个组名。
Comparison of the 2 methods (where h5f is my HDF5 file object):两种方法的比较(其中 h5f 是我的 HDF5 文件对象):
h5f.get_node('/185') # works
tb1nn = h5f.root.185 # gives Python invalid syntax error

Change the group name to t185 and you can use natural naming.将组名更改为t185即可使用自然命名。 See example PyTables code below to show the difference:请参阅下面的示例 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM