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

IGN 8.1
here is the Source code i used.
Vision , Regular Table component.
with Tag history .


table = event.source.parent.getComponent("Table")
data = table.data
from com.inductiveautomation.ignition.common import BasicDataset
newColumns = ["Time","U1 Volts","U2 Volts","U3 Volts","I1 Amps","I2 Amps","I3 Amps","P1 Power KW","P2 Power KW,""P3 Power KW","Total Power KW","PF1","PF2","PF3","Total PF"]
dataset = BasicDataset(newColumns, data.columnTypes, data)
csv = system.dataset.toCSV(dataset)
file = system.file.saveFile("export.csv", "csv", "Comma Separated Values")
if file:
system.file.writeFile(file, csv)


note new columns string names must match how many columns you have in your table.
this was used on a image for mouse click script to export data to a excel / CSV file.