Convert to dataset

Hi Friends,

I’m trying to convert data into a dataset using toPyDataSet.

val1 = system.tag.readBlocking([’[default]Sample/Tag 1/tag’])[0].value
val2 = system.tag.readBlocking([’[default]Sample/Tag 2/tag’])[0].value

data =
data.append(val1)
data.append(val2)

convertData = system.dataset.toPyDataSet(data)
print(convertData)

found the error → TypeError: toPyDataSet(): 1st arg can’t be coerced to com.inductiveautomation.ignition.common.Dataset

Please can you suggest what is wrong from my side as soon as possible?

Thanks,
Priyanka

Hi, @victordcq can you suggest any solution?

val1 and val2 are just single values correct?

You first need to make a dataset before you can go to pydataset.
To turn it into a dataset, you need an array of headers and an array of values per row. check here:
https://docs.inductiveautomation.com/display/DOC81/Datasets

1 Like

Yes. val1 and val2 get single integer value from tag.

1 Like

check out the link it should explain everything^^

val1 = system.tag.readBlocking([’[default]Sample/Tag 1/tag’])[0].value
val2 = system.tag.readBlocking([’[default]Sample/Tag 2/tag’])[0].value

data =
data.append(val1)
data.append(val2)

ds = system.dataset.toDataSet(data)
convertData = system.dataset.toPyDataSet(ds)
print(convertData)

Added dataset still found same error.

needs two arguments, the first is the headers, the second the values

Yes. Added headers also but still not working.

val1 = system.tag.readBlocking([’[default]Sample/Tag 1/tag’])[0].value
val2 = system.tag.readBlocking([’[default]Sample/Tag 2/tag’])[0].value

headers = [‘Sample’]
data =
data.append(val1)
data.append(val2)

ds = system.dataset.toDataSet(headers,data)
convertData = system.dataset.toPyDataSet(ds)
print(convertData)

what is the error?

Screenshot (84)

ah yes each row needs to be in its own array.
so in your case

data.append([val1])
data.append([val2])
2 Likes

Hi @Priyanka.Khandge, I think @victordcq has answered the primary question, but I was just wondering why why your data has to be a dataset or PyDataSet?

Hi, @victordcq Thank you for the neat step by step explanation.

1 Like

As a side note, if you have many tags, it will be more efficient to read the tags all at once, then use a loop to construct your dataset.

tagList = ['[default]Sample/Tag 1/tag', 
           '[default]Sample/Tag 2/tag'
		  ]

tags = system.tag.readBlocking(tagList)

headers = ['Sample']
data =[]
for tag in tags:
	data.append([tag.value])

ds = system.dataset.toDataSet(headers,data)
convertData = system.dataset.toPyDataSet(ds)
print(convertData)
1 Like

@victordcq and @JordanCClark Thank you for your help. both solutions are working.

Now I found a new error. I have a Flex Repeater that is set to use the said view as its path. I am trying to use a tag with a script transform to tell props.instances.

The problem is I get the error-> instances: array expected, object found

tagList = [’[default]Sample/Tag 1/tag’, ‘[default]Sample/Tag 2/tag’]
tags = system.tag.readBlocking(tagList)
headers = [‘Sample’]
data =
for tag in tags:
data.append([tag.value])
ds = system.dataset.toDataSet(headers,data)
convertData = system.dataset.toPyDataSet(ds)
print(convertData)

Got the output in Dataset[2 rows, 1 cols] format.

Please give me any suggestions?

a flex repeater needs an array of objects as argument.
you will need to turn your data into something like this:

(instanceStyle and instancePosition are optional)

tagList = [’[default]Sample/Tag 1/tag’, ‘[default]Sample/Tag 2/tag’]
tags = system.tag.readBlocking(tagList)
instanceStyle = {}
instancePosition={}
data = []
for tag in tags:
  data.append({'Sample':tag.value,'instanceStyle':instanceStyle,'instancePosition':instancePosition  })

return data