2013-05-08 60 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 *时它不会自动导入,所以你必须明确地导入它。当您在嵌套表格的语法方面遇到困难时,此函数可能会派上用场。