Is there a way to convert a dataset column type?
I am trying to import a CSV file into a dataset tag but the numbers come in as strings. What would be the easiest way to accomplish this? Thanks for any guidance.
witman
August 16, 2021, 6:01pm
2
Used DataSetBuilder to create a dataset with the right column types.
Use Ignition’s DatasetBuilder . Something like this:
from com.inductiveautomation.ignition.common.util import DatasetBuilder
from java.lang import Integer, String, Object
from java.awt import Color
def forceTypes(sqlDS):
builder = DatasetBuilder.newBuilder()
builder.colNames(sqlDS.columnNames)
originalTypes = list(sqlDS.columnTypes)
i = #some column index for a color
originalTypes[i] = Color
builder.colTypes(originalTypes)
for r in range(sqlDS.rowCount):
bui…
Then copy the rows from your dataset with the wrong types into the dataset with the correct column types.
Or it might be as easy to manipulate the CSV to specify the correct types before turning it into a dataset:
Use system.dataset.fromCSV to specify data types and column headers for your blank dataset like this:
blankDataset = """#NAMES
Header1, Header2,
#TYPES
d,I
#ROWS,0"""
See Ignition Manual for data types, etc.
@pturmel @witman
The man, the myth, the legend strikes again! Thanks Phil and witman.
Updated code attached.
Updated the function to take a dictionary of integer keys and datatype values in other words
ds_dict = {0: Integer, 2: Integer}