Datasets
Overview​
Datasets are used to manage tabular data and can be accessed like native Python objects. They support features such as indexing, iteration, and dataset-specific methods, making them simple to use in scripting. Datasets in Ignition can now be accessed directly for both reading and writing operations, eliminating the need for manual conversions.
Creating Datasets​
Datasets can be created programmatically in Python using the system.dataset.toDataSet function. This function requires two inputs: a list of headers and a list of data rows. Each data row must have the same number of elements as there are headers.
# First, create a list that contains the headers.
headers = ["City", "Population", "Timezone", "GMTOffset"]
# Create a list to house the data rows.
data = [
["New York", 8363710, "EST", -5],
["Los Angeles", 3833995, "PST", -8],
["Chicago", 2853114, "CST", -6],
["Houston", 2242193, "CST", -6],
["Phoenix", 1567924, "MST", -7]
]
# Use the headers and data to create a Dataset.
cities = system.dataset.toDataSet(headers, data)
All code snippets on this page will reference the cities dataset we created above, so place that code at the beginning of every code snippet.
Accessing Data in a Dataset​
Datasets can be accessed using either dataset-specific methods or Python-style indexing and iteration. Below are examples of common ways to interact with dataset data.
The table below lists common methods available for interacting with datasets.
Function | Description | Example | Output |
---|---|---|---|
getColumnAsList(colIndex) | Returns a column as a list. | cities.getColumnAsList(0) | [New York, Los Angeles, Chicago, Houston, Phoenix] |
getColumnCount() | Returns the number of columns. | cities.getColumnCount() | 4 |
getColumnIndex(colName) | Returns the index of a column by name. | cities.getColumnIndex("Timezone") | 2 |
getColumnName(colIndex) | Returns the name of a column by its index. | cities.getColumnName(1) | Population |
getRowCount() | Returns the number of rows. | cities.getRowCount() | 5 |
getValueAt(rowIndex, colIndex) | Gets the value at a specific row and column. | cities.getValueAt(1, 2) | PST |
With Python-style access, datasets can be iterated over or indexed like lists, allowing for intuitive interaction with rows and columns.
# Access a specific value directly using row and column indices.
print(cities[1][2]) # Outputs "PST"
# You can also access a column by name if you iterate through rows.
for row in cities:
print(row["City"], row["Population"])
# Loop through all rows and columns in the dataset.
for row in cities:
for value in row:
print(value)
Altering a Dataset​
Datasets in Ignition are immutable, meaning you cannot modify them directly. Instead, you create a new dataset by applying changes to the original dataset using functions like system.dataset.addRow, system.dataset.deleteRow, or system.dataset.setValue. The original dataset remains unchanged.
# Create a new dataset with the updated value.
newData = system.dataset.setValue(cities, 1, "Population", 5000000)
# The original dataset remains unchanged. Iterate over both datasets to compare.
print("Original Dataset:")
for row in cities:
print(row)
print("\nUpdated Dataset:")
for row in newData:
print(row)
Backward Compatibility​
Scripts using the legacy system.dataset.toPyDataSet function will continue to function as expected. However, as of 8.3, datasets no longer require explicit conversions. You can directly access datasets using indexing, iteration, and dataset methods.
For example, the following is no longer necessary:
pyData = system.dataset.toPyDataSet(cities)
Instead, you can work directly with the dataset:
for row in cities:
print(row["City"], row["Population"])