【问题标题】:Check whether a PyTables node in a pandas HDFStore is tabular检查 pandas HDFStore 中的 PyTables 节点是否为表格
【发布时间】:2013-11-04 23:48:24
【问题描述】:

是否有首选方法来检查 pandas HDFStore 中的 PyTables 节点是否为表格?这可行,但 NoSuchNodeError 似乎不是 API 的一部分,所以也许我不应该依赖它。

In [34]: from tables.table import NoSuchNodeError

In [35]: def is_tabular(store, key):
    try:
        store.get_node(key).table
    except NoSuchNodeError:
        return False
    return True
   ....: 

In [36]: is_tabular(store, 'first_600')
Out[36]: False

In [37]: is_tabular(store, 'features')
Out[37]: True

【问题讨论】:

    标签: pandas pytables hdfstore


    【解决方案1】:

    你可以做这样的事情。 pandas_type、table_type 元数据将出现在节点顶层的 pytables 属性_v_attrs 中。

    In [28]: store = pd.HDFStore('test.h5',mode='w')
    
    In [29]: store.append('df',DataFrame(np.random.randn(10,2),columns=list('AB')))
    
    In [30]: store
    Out[30]: 
    <class 'pandas.io.pytables.HDFStore'>
    File path: test.h5
    /df            frame_table  (typ->appendable,nrows->10,ncols->2,indexers->[index])
    
    In [31]: store._handle.root.df._v_attrs
    Out[31]: 
    /df._v_attrs (AttributeSet), 14 attributes:
       [CLASS := 'GROUP',
        TITLE := u'',
        VERSION := '1.0',
        data_columns := [],
        encoding := None,
        index_cols := [(0, 'index')],
        info := {1: {'type': 'Index', 'names': [None]}, 'index': {}},
        levels := 1,
        nan_rep := 'nan',
        non_index_axes := [(1, ['A', 'B'])],
        pandas_type := 'frame_table',
        pandas_version := '0.10.1',
        table_type := 'appendable_frame',
        values_cols := ['values_block_0']]
    
    In [33]: getattr(getattr(getattr(store._handle.root,'df',None),'_v_attrs',None),'pandas_type',None)
    Out[33]: 'frame_table'
    
    In [34]: store.close()
    
    In [35]: 
    

    【讨论】:

      猜你喜欢
      • 2013-06-21
      • 1970-01-01
      • 2019-06-20
      • 2013-09-14
      • 2017-08-12
      • 2017-07-28
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      相关资源
      最近更新 更多