2013-05-08 59 views
0

假設我有以下PyTable列描述符:訂貨在PyTable表嵌套結構的

import numpy as np 
import tables as pt 

class Date_t(pt.IsDescription): 
    year = pt.Int32Col(shape=(), dflt=2013, pos=0) 
    month = pt.Int32Col(shape=(), dflt=1, pos=1) 
    day  = pt.Int32Col(shape=(), dflt=1, pos=2) 

class Info(pt.IsDescription): 
    col1  = pt.Int32Col(shape=(), dflt=0, pos=0) 
    startdate = Date_t() 
    birthdate = Date_t() 
    col2  = pt.Int32Col(shape=(), dflt=0, pos=3) 
    enddate = Date_t() 
    col3  = pt.Int32Col(shape=(), dflt=0, pos=5) 
    col4  = pt.Int32Col(shape=(), dflt=0, pos=6) 

如何指定「開始日期」,「出生日期」和「結束日期」的位置?
我想我可以做這樣的事情:

startdate = Date_t(pos=1) 
birthdate = Date_t(pos=2) 

,並重新定義Date_t類爲:

class Date_t(pt.IsDescription): 
    def __init__(self, pos): 
     self._v_pos = pos 
    year = pt.Int32Col(shape=(), dflt=2013, pos=0) 
    month = pt.Int32Col(shape=(), dflt=1, pos=1) 
    day  = pt.Int32Col(shape=(), dflt=1, pos=2) 

但是這給我的錯誤:
類型錯誤:對象。 ()不參數

任何想法?

回答

1

以一種有點冒失的方式,您可以在定義之後更改Info的類屬性。

class Info(pt.IsDescription): 
    col1  = pt.Int32Col(shape=(), dflt=0, pos=0) 
    startdate = Date_t() 
    birthdate = Date_t() 
    col2  = pt.Int32Col(shape=(), dflt=0, pos=3) 
    enddate = Date_t() 
    col3  = pt.Int32Col(shape=(), dflt=0, pos=5) 
    col4  = pt.Int32Col(shape=(), dflt=0, pos=6) 

Info.columns['startdate']._v_pos = 1 
Info.columns['birthdate']._v_pos = 2 
Info.columns['enddate']._v_pos = 3 

有一個稱爲descr_from_dtype()在pytables一個函數,創建了一個從(可能是嵌套)numpy的D型細胞的表描述。當你運行一個from tables import *時它不會自動導入,所以你必須明確地導入它。當您在嵌套表格的語法方面遇到困難時,此函數可能會派上用場。