如何摆脱自然灾害?

2024-09-29 06:29:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我的剧本是这样的:

  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

Tags: 文件tonameposdataframedftablesnot
1条回答
网友
1楼 · 发布于 2024-09-29 06:29:27

这是一个警告。这意味着您不能使用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)

相关问题 更多 >