Search code examples
pythonpandashdf5

How to get rid of NaturalNameWarning?


My script is doing following things:

  1. read time series from binary trc file (UHF Measurement)
  2. crop certain regions (Impulses) and save each of them into a pd.DataFrame
  3. save all DataFrames into one hdf5 file

This works fine but the tables module seems to throw a NaturalNameWarning for every single DataFrame.

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

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

Solution

  • This is a warning. It means you can't use PyTables natural naming convention to access a dataset named 185. It's not a problem if you don't plan to use PyTables. If you want to use PyTables, you have to use File.get_node(where) to access this group name.
    Comparison of the 2 methods (where h5f is my HDF5 file object):
    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. See example PyTables code below to show the difference:

    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)