# Converting PyDataSet to DataSet

**URL:** <https://forum.inductiveautomation.com/t/converting-pydataset-to-dataset/40233>\
**Category:** Ignition\
**Created:** [November 3, 2020, 2:46pm UTC](https://forum.inductiveautomation.com/t/converting-pydataset-to-dataset/40233 "2020-11-03T14:46:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![akolovos](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@akolovos](https://forum.inductiveautomation.com/u/akolovos)\
**Post date:** [November 3, 2020, 2:46pm UTC](https://forum.inductiveautomation.com/t/converting-pydataset-to-dataset/40233/1 "2020-11-03T14:46:02Z")

</div>

Hello, i am trying to implement some filters on a table component, and the way I am trying to do it is by having the data(to be filtered) on a view property and then binding this to the table data and using a script transform to actually do the filtering. However my script cannot even be compiled, and i cant seem to be able to figure out why:

```auto
def transform(self, value, quality, timestamp):
	"""
	Transform the incoming value and return a result.

	Arguments:
		self: A reference to the component this binding is configured on.
		value: The incoming value from the binding or the previous transform.
		quality: The quality code of the incoming value.
		timestamp: The timestamp of the incoming value as a java.util.Date
	"""
	
	data=system.dataset.toPyDataSet(value)

	header=[]
	for i in range(data.getColumnCount()):
		header.append(data.getColumnName(i))

	newData=[]
	for row in data:
		if (self.checkFilters(row)): #Custom method to apply the filters
			newData.append(row)
	
	ret=system.dataset.toDataSet(header,newData)
	return ret

```

I am getting a scriptEval error at ret=system.dataset.toDataSet(header,newData)  
(I cant seem to be able to copy-paste the error.)  
newData is a PyList object (389x325)  
header is also a PyList object(325)  
Any help would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [November 3, 2020, 3:03pm UTC](https://forum.inductiveautomation.com/t/converting-pydataset-to-dataset/40233/2 "2020-11-03T15:03:21Z")

</div>

Without a little more information on the error, I’m not sure how much we can help.

If getColumnNames() returns a list of the column names, then why build your own list with a for loop? This seems like unneeded code.

---

<div class="post-metadata">

**Author:** ![akolovos](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@akolovos](https://forum.inductiveautomation.com/u/akolovos)\
**Post date:** [November 3, 2020, 3:21pm UTC](https://forum.inductiveautomation.com/t/converting-pydataset-to-dataset/40233/3 "2020-11-03T15:21:09Z")

</div>

Sorry about that, that was uncessary code, getColumnNames() returns an UnmodifiableRandomAccessList which causes an error in the conversion again, as the 1st Argument cannot be coerced into a PySequence.

I took a screenshot of the error message:

 ![error](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/e/7e92f054bb06a9d87d1a61ee3083864a212b31af.png)

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [November 3, 2020, 4:33pm UTC](https://forum.inductiveautomation.com/t/converting-pydataset-to-dataset/40233/4 "2020-11-03T16:33:46Z")

</div>

Try changing line 13 to this:

```auto
newData.append(list(row))

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [November 3, 2020, 6:13pm UTC](https://forum.inductiveautomation.com/t/converting-pydataset-to-dataset/40233/5 "2020-11-03T18:13:24Z")

</div>

The type cast to list as @code_skin suggests may do the trick. Barring that, I would shy away from the conversion to a PyDataSet. I just feel like it adds complexity without much reward in most cases. Here is how I would refactor your script:

```auto
def transform(self, value, quality, timestamp):
   """
   Transform the incoming value and return a result.

   Arguments:
   	self: A reference to the component this binding is configured on.
   	value: The incoming value from the binding or the previous transform.
   	quality: The quality code of the incoming value.
   	timestamp: The timestamp of the incoming value as a java.util.Date
   """
   
   headers = system.dataset.getColumnHeaders(value)

   newData=[]
   for rowIndex in range(value.rowCount):
       row = [value.getValueAt(rowIndex,col) for col in range(value.columnCount)]
       if self.checkFilters(row): #Custom method to apply the filters
           newData.append(row)

   return system.dataset.toDataSet(headers,newData)

```

---

<div class="post-metadata">

**Author:** ![akolovos](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@akolovos](https://forum.inductiveautomation.com/u/akolovos)\
**Post date:** [November 4, 2020, 12:50pm UTC](https://forum.inductiveautomation.com/t/converting-pydataset-to-dataset/40233/6 "2020-11-04T12:50:03Z")

</div>

The type casting to list, worked! Thank you very much all for your help and suggestions.
