0

Suppose I have the following PyTable column descriptor:

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) 

How can I specify the position of 'startdate', 'birthdate', and 'enddate'?
I thought I could do something like:

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

and redefine the Date_t class as:

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)

but this gives me the error:
TypeError: object.new() takes no parameters

Any ideas?

Joel Vroom
  • 1,611
  • 1
  • 16
  • 30

1 Answers1

1

In a somewhat hackish way, you can change the class attributes of Info after it is defined.

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

There's a function in pytables called descr_from_dtype() that creates a table description from a (possibly nested) numpy dtype. It does not import automatically when one runs a from tables import *, so you have to import it explicitly. This function may come in handy when you are having trouble with the syntax for nested tables.

streeto
  • 123
  • 1
  • 4