Skip to main content
Version: Python

Create a table with new_table

This guide will show you how to create a new in-memory table with new_table.

Deephaven stores data in tables, which are composed of rows and columns of data. Columns are placed into a table through one or more column methods such as int_col. Each column contains one data type. For example, int_col creates a column of Java primitive int values.

new_table

The following query creates a new in-memory table with a string column and an int column.

from deephaven import new_table

from deephaven.column import string_col, int_col

result = new_table(
[
string_col(
"Name_Of_String_Col", ["Data String 1", "Data String 2", "Data String 3"]
),
int_col("Name_Of_Int_Col", [4, 5, 6]),
]
)

This produces a table with a String column, an integer column, and three rows.

We will walk you through the query step-by-step.

  1. We are using the new_table method. Its arguments will define our column names, types, and contents.
  2. We define a string column using the method string_col:
    • The first argument is the column name, Name_Of_String_Col. Column names are typically capitalized.
    • The next arguments are the column contents written as a comma-separated list. Since these are String values, they are enclosed in quotation marks.
  3. We define a second column using the method int_col:
    • The first argument is the column name, Name_Of_Int_Col
    • The next arguments are the column contents, written as a comma-separated list of integers.

new_table can also be used to create array columns. This cannot be done with methods like int_col and string_col. In these cases, you must use the InputColumn class directly along with the dtypes package.

The following example creates a new in-memory table with a single integer array column.

from deephaven.column import InputColumn
from deephaven import new_table
from deephaven import dtypes
import numpy as np

int_array = dtypes.array(dtypes.int32, np.array([1, 2, 3], dtype=np.int32))
int_array_col = InputColumn("IntArrayCol", dtypes.int32_array, input_data=[int_array])

source = new_table([int_array_col])