Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to store PyTable columns in a specific order?

Tags:

pytables

It seems that the PyTable columns are alphabetically ordered when using both dictionary or class for schema definition for the call to createTable(). My need is to establish a specific order and then use numpy.genfromtxt() to read and store my data from text. My text file does not have the variable names included alphabetically as they are for the PyTable.

For example, assuming text file is named mydata.txt and is organized as follows:

time(row1) bVar(row1) dVar(row1) aVar(row1) cVar(row1)

time(row2) bVar(row2) dVar(row2) aVar(row2) cVar(row2) ...

time(rowN) bVar(rowN) dVar(rowN) aVar(rowN) cVar(rowN)

So, the desire is to create a table that is ordered with these columns and then use the numpy.genfromtxt command to populate the table.

# Column and Table definition with desired order
class parmDev(tables.IsDescription):
    time = tables.Float64Col()
    bVar = tables.Float64Col()
    dVar = tables.Float64Col()
    aVar = tables.Float64Col()
    cVar = tables.Float64Col()

#...

mytab = tables.createTable( group, tabName, paramDev )

data = numpy.genfromtxt(mydata.txt)
mytab.append(data)

This is desired because it is straightforward code and is very fast. But, the PyTable columns are always ordered alphabetically and the appended data is ordered according to the desired order. Am I missing something basic here? Is there a way to have the order of the table columns follow the class definition order instead of being alphabetical?

like image 900
tnt Avatar asked Nov 29 '10 13:11

tnt


People also ask

How do I change the order of columns in a SQL table?

In Object Explorer, right-click the table with columns you want to reorder and select Design. Select the box to the left of the column name that you want to reorder. Drag the column to another location within the table.

Does the order of columns in a table matter?

No it shouldn't matter. A normalized database should not have constraints on the column order, as well. Save this answer.

Can you query columns in any order?

The order doesn't matter, actually, so you are free to order them however you'd like.


1 Answers

Yes, you can define an order in tables in several different ways. The easiest one is to use the pos parameter for each column. See the docs for the Col class:

http://pytables.github.io/usersguide/libref/declarative_classes.html#the-col-class-and-its-descendants

For your example, it will look like:

class parmDev(tables.IsDescription):
    time = tables.Float64Col(pos=0)
    bVar = tables.Float64Col(pos=1)
    dVar = tables.Float64Col(pos=2)
    aVar = tables.Float64Col(pos=3)
    cVar = tables.Float64Col(pos=4)

Hope this helps

like image 128
FrancescAlted Avatar answered Sep 19 '22 23:09

FrancescAlted