TypeError: toDataSet(): 1st arg can't be coerced to com.indu

What I’m trying to attempt here is to create a dropbox with the last x weeks starting Sunday

So I created a dataset with x number of string of Sunday dates in Python. Then I have to convert it into a normal DataSet so I can return it in an Expression script

When I use the normaDataSet = system.dataset.toDataSet(pyDataSet), it gives me this error. And I obviously don’t get it.

And also taking suggestion on a better way to do this :smiley:

Exception: Error executing expression binding on Window.Root Container.Dropdown.data caused by ExpressionException: Error executing script for runScript() expression:app.getWeeks.bySundays(5) caused by Traceback (most recent call last): File "<expression:runScript>", line 1, in <module> File "<module:getWeeks>", line 20, in bySundays TypeError: toDataSet(): 1st arg can't be coerced to com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities$PyDataSet

Ignition v7.5.5 (b1255)
Java: Oracle Corporation 1.7.0_15

[code]def bySundays(numWeeks):
import datetime
import system

now = datetime.date.today()
	
x = 0 - now.isoweekday()
if x == -7:
	x = 0
	
startw = now + datetime.timedelta(days=x)
pyDataSet = []
	
for i in range(numWeeks):
	input = datetime.date.isoformat(startw + datetime.timedelta(weeks=-i))
	pyDataSet.append(input)
	
print pyDataSet
	
normaDataSet = system.dataset.toDataSet(pyDataSet)
	
return normalDataSet.data

[/code]

And Plan B didn’t work neither I tried to return the Python Dataset to the expression and use the toDataset function as per

toDataSet(runScript("app.getWeeks.bySundays(5)"))

and got the following

Exception: Error executing expression binding on
Window.Root Container.Dropdown.data
	caused by ExpressionException: Unable to cast: '['2013-04-14', '2013-04-07', '2013-03-31', '2013-03-24', '2013-03-17']' to a(n) Dataset. Try using a failover parameter in your cast function.

Ignition v7.5.5 (b1255)
Java: Oracle Corporation 1.7.0_15

system.dataset.toDataSet() expects two arguments: headers and data.

headers is a list, data is a list of lists.

Right now you’ve either got no data, or no headers and data that’s simply a list.

Alternatively it can receive a PyDataSet, but just making yourself a list of stuff in Python does not mean you have a PyDataSet

1 Like

You are creating a list.
This will define the data to convert to a dataset.
I changed your PyDataset variable to list.

[code]
def bySundays(numWeeks):
import datetime
import system

now = datetime.date.today()
numWeeks = 5
rw = 0 #row pointer

header = ["Value", "Label"] #Define the header
list = [ [ "" for c in range(2) ] for r in range(numWeeks) ] #Setup the number of rows we will need, make 2 columns.
      
for i in range(numWeeks):
	input = now - datetime.timedelta(days=now.weekday()+1, weeks=i) #Find the previous Sundays (Starts on Monday)
	list[rw][0] = datetime.date.isoformat(input) #Add data to the value column previously made.
	list[rw][1] = datetime.date.isoformat(input) #Add data to the Label column previously made.
	rw += 1

normaDataSet = system.dataset.toDataSet(header,list) #Make the datatable back.
return normaDataSet[/code]

Hope this helps.

Cheers,
Chris

THANK!!! I’ll try that tomorrow at work!!! :prayer:

I’ve should have looked at the second toDataSet Example in the manual… I would have clued in… It’s been 5 years since I’ve wrote code… new job require it I guess :stuck_out_tongue: This ain’t like PHP neither :unamused: