PyTables User's Guide: Hierarchical datasets in Python - Release 1.3.3
Prev | Chapter 4. Library Reference | Next

# 4.9. The Column class

Each instance of this class is associated with one column of every table. These instances are mainly used to fetch and set actual data from the table columns, but there are a few other associated methods to deal with indexes.

## 4.9.1. Column instance variables

table

  The parent `Table` instance.

name

  The name of the associated column.

pathname

  The complete pathname of the associated column. This is mainly useful in nested columns; for non-nested ones this value is the same a `name`.

type

  The data type of the column.

shape

  The shape of the column.

index

  The associated `Index` object (see 4.17.3) to this column (`None` if does not exist).

dirty

  Whether the index is dirty or not (property).

## 4.9.2. Column methods

### 4.9.2.1. createIndex()

Create an `Index` (see 4.17.3) object for this column.

### 4.9.2.2. reIndex()

Recompute the index associated with this column. This can be useful when you suspect that, for any reason, the index information is no longer valid and want to rebuild it.

### 4.9.2.3. reIndexDirty()

Recompute the existing index only if it is dirty. This can be useful when you have set the `reindex` parameter to 0 in `IndexProps` constructor (see 4.17.2) for the table and want to update the column's index after a invalidating index operation (`Table.removeRows`, for example).

### 4.9.2.4. removeIndex()

Delete the associated column's index. After doing that, you will loose the indexation information on disk. However, you can always re-create it using the `createIndex()` method (see 4.9.2).

## 4.9.3. Column special methods

### 4.9.3.1. __getitem__(key)

Returns a column element or slice. It takes different actions depending on the type of the key parameter:

key is an `Integer`

  The corresponding element in the column is returned as a scalar object or as a `numarray` object, depending on its shape.

key is a `Slice`

  The row range determined by this slice is returned as a `numarray` object.

Example of use:

    print "Column handlers:"
    for name in table.colnames:
        print table.cols[name]
    print
    print "Some selections:"
    print "Select table.cols.name[1]-->", table.cols.name[1]
    print "Select table.cols.name[1:2]-->", table.cols.name[1:2]
    print "Select table.cols.lati[1:3]-->", table.cols.lati[1:3]
    print "Select table.cols.pressure[:]-->", table.cols.pressure[:]
    print "Select table.cols['temperature'][:]-->", table.cols['temperature'][:]

and the output of this for a certain arbitrary table is:

    Column handlers:
    /table.cols.name (Column(1,), CharType)
    /table.cols.lati (Column(2,), Int32)
    /table.cols.longi (Column(1,), Int32)
    /table.cols.pressure (Column(1,), Float32)
    /table.cols.temperature (Column(1,), Float64)

    Some selections:
    Select table.cols.name[1]--> Particle:     11
    Select table.cols.name[1:2]--> ['Particle:     11']
    Select table.cols.lati[1:3]--> [[11 12]
     [12 13]]
    Select table.cols.pressure[:]--> [  90.  110.  132.]
    Select table.cols['temperature'][:]--> [ 100.  121.  144.]

See the `examples/table2.py` for a more complete example.

### 4.9.3.2. __setitem__(key, value)

It takes different actions depending on the type of the `key` parameter:

key is an `Integer`

  The corresponding element in the column is set to value. value must be a scalar or `numarray`/`NumPy` object, depending on column's shape.

key is a `Slice`

  The row slice determined by key is set to value. value must be a list of elements or a `numarray`/`NumPy`.

Example of use:

    		# Modify row 1
    		table.cols.col1[1] = -1
    		# Modify rows 1 and 3
    		table.cols.col1[1::2] = [2,3]

Which is equivalent to:

    		# Modify row 1
    		table.modifyColumns(start=1, columns=[[-1]], names=["col1"])
    		# Modify rows 1 and 3
    		columns = numarray.records.fromarrays([[2,3]], formats="i4")
    		table.modifyColumns(start=1, step=2, columns=columns, names=["col1"])

Prev | Home | Next
The Description class | Up | The Array class