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

The PyTable columns seem to be sorted alphabetically when using a dictionary or class to define the schema for calling createTable (). I need to set a specific order and then use numpy.genfromtxt () to read and store my data from text. My text file does not have variable names included in alphabetical order, as for PyTable.

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

time (line1) bVar (line1) dVar (line1) aVar (line1) cVar (line1)

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

time (line N) bVar (line N) dVar (line N) aVar (line N) cVar (line N)

So, desire to create a table 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 desirable because it is simple code and very fast. But PyTable columns are always sorted alphabetically, and the attached data is sorted in the order you want. Am I losing something here? Is there a way for the column order of the table to follow the order in which the class is defined, rather than alphabetically?

+3
source share
1 answer

Yes, you can determine the order in tables in several ways. The easiest way is to use a parameter posfor each column. See Documents for the class Col:

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

In your example, it will look like this:

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

+8
source

Source: https://habr.com/ru/post/1777457/


All Articles