2013-09-26 2 views
2

팬더 데이터 프레임으로 가져 오려고하는 파이 테이블로 만든 데이터 세트가 있습니다. read_hdf 단계에 where 필터를 적용 할 수 없습니다. 나는 dataframe에 잘 가져올 수 있습니다팬더 가져 오기에서 파이 테이블 테이블 가져 오기

import tables 
import pandas as pd 
import numpy as np 

class BranchFlow(tables.IsDescription): 
    branch = tables.StringCol(itemsize=25, dflt=' ') 
    flow = tables.Float32Col(dflt=0) 

filters = tables.Filters(complevel=8) 
h5 = tables.openFile('foo.h5', 'w') 
tbl = h5.createTable('/', 'BranchFlows', BranchFlow, 
      'Branch Flows', filters=filters, expectedrows=50e6) 

for i in range(25): 
    element = tbl.row 
    element['branch'] = str(i) 
    element['flow'] = np.random.randn() 
    element.append() 
tbl.flush() 
h5.close() 

가 :

store = pd.HDFStore('foo.h5') 
print store 
print pd.read_hdf('foo.h5', 'BranchFlows').head() 

보여줍니다

In [10]: print store 
<class 'pandas.io.pytables.HDFStore'> 
File path: foo.h5 
/BranchFlows   frame_table [0.0.0] (typ->generic,nrows->25,ncols->2,indexers->[index],dc->[branch,flow]) 

In [11]: print pd.read_hdf('foo.h5', 'BranchFlows').head() 
    branch  flow 
0  0 -0.928300 
1  1 -0.256454 
2  2 -0.945901 
3  3 1.090994 
4  4 0.350750 
나는 팬더 '0.12.0의'에

내 샘플 pytables 데이터를 해요

하지만 흐름 열에 작동하도록 필터를 가져올 수 없습니다.

pd.read_hdf('foo.h5', 'BranchFlows', where=['flow>0.5']) 

<snip traceback> 

TypeError: passing a filterable condition to a non-table indexer [field->flow,op->>,value->[0.5]] 

답변

3

PyTables에서 직접 읽은 테이블 만 읽으면 (전체) 테이블을 직접 읽을 수 있습니다. 팬더 선택 메커니즘을 사용하려면 팬더 도구 (테이블 형식)를 사용해야합니다 (판다에 필요한 메타 데이터가 없기 때문에 - 수행 할 수 있지만 작업이 필요합니다).

위와 같이 표를 읽은 다음 새 표를 만들고 표 형식을 지정하십시오. here for docs

In [6]: df.to_hdf('foo.h5','BranchFlowsTable',data_columns=True,table=True) 

In [24]: with pd.get_store('foo.h5') as store: 
    print(store) 
    ....:  
<class 'pandas.io.pytables.HDFStore'> 
File path: foo.h5 
/BranchFlows     frame_table [0.0.0] (typ->generic,nrows->25,ncols->2,indexers->[index],dc->[branch,flow]) 
/BranchFlowsTable   frame_table (typ->appendable,nrows->25,ncols->2,indexers->[index],dc->[branch,flow])  

In [7]: pd.read_hdf('foo.h5','BranchFlowsTable',where='flow>0.5') 
Out[7]: 

    branch  flow 
14  14 1.503739 
15  15 0.660297 
17  17 0.685152 
18  18 1.156073 
20  20 0.994792 
21  21 1.266463 
23  23 0.927678 
+0

내가 왜 팬더 데이터 프레임을 만들지 않았는지 기억이 안납니다. 지금 당장 numpy는 "ValueError : 배열이 너무 큽니다."라고 말하면서 실제 데이터 세트를 읽으려고하지만 또 다른 문제입니다. – chip

+0

이 작업을 부분적으로 수행 할 수 있습니다 (''append = True''를 패스). 그리고 부분 프레임을 전달하고 테이블을 빌드 할 수 있습니다. [here for more] (http://pandas.pydata.org/pandas-docs/ dev/cookbook.html # hdfstore) – Jeff