Change Column Name in Dataset

Is there a relatively easy way to rebuild a dataset with new column names?

I will have a dataset with a dynamic set of columns (between 2 and 10 really), lets say it looks like this:

Timestamp Temperature Pressure
XXX XXX XXX
XXX XXX XXX
XXX XXX XXX

I need to change it to the following:

column0 column1 column2
XXX XXX XXX
XXX XXX XXX
XXX XXX XXX

I know its possible for me to iterate through each column and swap it out like this:

newColumns = ["column%s" % index for index in range(dataset.getColumnCount())]
for index, column in enumerate(dataset.getColumnNames()):
	dataset = system.dataset.addColumn(dataset, index, dataset.getColumnAsList(index), "column%s" % index, dataset.getColumnType(index))

dataset = system.dataset.filterColumns(dataset, newColumns)

I am just curious if there is a better way, I know they are immutable, so I am guessing not, but just curious.

BasicDataset has a constructor that accepts another dataset:

from com.inductiveautomation.ignition.common import BasicDataset
newColumns = ["column%s" % index for index in range(dataset.getColumnCount())]
dataset = BasicDataset(newColumns, dataset.columnTypes, dataset)

If the original was a 'streaming' dataset type, this will collect it into memory, but your original code was doing that too, so :man_shrugging:

2 Likes