Convert historical status (string) data to integers

Can I take a table of historical string values denoting run status (eg R, Q, S, etc) and convert them to integers?

I figured it would be as easy as mapping a transform in the tag history binding eg R = 1, fallback = 0, but it seems to treat the dataset like a single data point... any help is appreciated.


Are you just displaying it in a table? If so, you could look into using a view for that column and do your convert in the view.
There may be a way to do the map like you are wanting, I just have never messed with it/seen it done. Aside from the good ole loop thru the dataset and changing...but I'd see that as least efficient most likely.

You could do it in a transform, but there is an efficiency hit:

mapDict = {'Q' : 4,
           'R' : 1,
		   'S' : 2
		  }

headers = list(value.columnNames)

# dict.get() can return a default value if the key is not found in the dictionary.
data = [[value.getValueAt(row,0), mapDict.get(value.getValueAt(row,1), 0)] for row in xrange(value.rowCount)]

return system.dataset.toDataSet(headers, data)

But, you may want to consider putting the raw data in a custom prop and use @pturmel's Integration Toolkit module to do the transform as an expression. Much more performant.

I'm not too worried about performance, this data is updated once a minute and it's just a proof of concept. The dataset is created in the tag binding - I'm not sure how to transform it via code or even how to reference it, so I would need a bit of help there.
image

Thanks for the replies so far!

Try using a script transform using the sample code I posted. You will need to adjust the dictionary to your specific needs.

'value' in a transform is whatever the result is of the step before it-- in this case, a dataset.

2 Likes

Thank you, I'm on the right track now. I appreciate the explanation.

1 Like

Add your two columns to the columns property of the table, then configure the t_stamp column's render property to datetime, so you get something readable.
Also, it seems kinda weird to convert the status to a boolean when there are more than 2 different possible values.
I'd probably keep the original value, and add a new column, or some particular row styling (maybe a background color) to indicate whether the status is R or something else.
But I don't know your use case, so don't blindly listen to me.

Converting status strings to integers so I can use them graphically in a time series output - not using the table itself, it was just helpful to understand the dataset.