# system.dataset.toDataSet pyfloat cannot be cast

**URL:** <https://forum.inductiveautomation.com/t/system-dataset-todataset-pyfloat-cannot-be-cast/42416>\
**Category:** Ignition\
**Created:** [January 19, 2021, 9:27pm UTC](https://forum.inductiveautomation.com/t/system-dataset-todataset-pyfloat-cannot-be-cast/42416 "2021-01-19T21:27:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![speters](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@speters](https://forum.inductiveautomation.com/u/speters)\
**Post date:** [January 19, 2021, 9:27pm UTC](https://forum.inductiveautomation.com/t/system-dataset-todataset-pyfloat-cannot-be-cast/42416/1 "2021-01-19T21:27:12Z")

</div>

I’m trying to make a dynamic table, based on the script below, this is just for one product, but depending on what location I pick, only want to run queries for available products. I keep getting this:  
java.lang.ClassCastException: java.lang.ClassCastException: class org.python.core.PyFloat cannot be cast to class org.python.core.PySequence (org.python.core.PyFloat and org.python.core.PySequence are in unnamed module of loader java.net.URLClassLoader @37827677)

Here is the script I’m using. Right now it’s only for one location, but I’ll loop thru it as I add locations, or do I need to scrap this and try a different approach.

```auto
if (event.propertyName == 'yearly' or 'monthly' or 'daily') and (event.newValue == 1):
	start=event.source.parent.getComponent('LOADS').start
	end=event.source.parent.getComponent('LOADS').end
	loc=event.source.parent.Loc
	prods=system.db.runPrepQuery("select * from Location where location="+"'"+loc+"'")
	
	qry="SELECT coalesce(sum(weight/2000.0),0.0) as "+'"'+"Total Tons" + '"' + " from truck_counts where location="+ "'" + loc + "'" +" and t_stamp between " + "'" + start + "'" + "and " + "'" + end + "'"

	master=system.db.runPrepQuery(qry)
	total=master[0][0]
	toth='"'+"<html><br>&nbsp;<html>&nbsp;<br><Center>Total&nbsp"+'"'
	data=[]
	data.append(total)
	header=[]
	header.append(toth)
	if prods[0][4]==1:
			prod1='SELECT coalesce(sum(weight/2000.0),0.0) as '+'"'+"Product I/II"+'"'+" from truck_counts where location="+"'"+loc+"'"+" and product_type='Product_I_II' and t_stamp between "+"'"+start+"'"+ "and "+"'"+end+"'"
			p1=system.db.runPrepQuery(prod1)
			p1d=p1[0][0]
			p1h='"'+"<html><br><Center>Product&nbsp;<br><Center>I/II&nbsp"+'"'
			data.extend([p1d])
			header.extend(p1h)
		
	d1=system.dataset.toDataSet(header ,data)
	
	event.source.data=d1

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 19, 2021, 10:23pm UTC](https://forum.inductiveautomation.com/t/system-dataset-todataset-pyfloat-cannot-be-cast/42416/2 "2021-01-19T22:23:18Z")

</div>

Haven't looked at anything else yet, but `if (event.propertyName == 'yearly' or 'monthly' or 'daily')` isn't doing what you think it is - it's checking:

> does event.propertyName equal 'yearly'  
> or is "monthly" true  
> or is "daily" true

In Python, any non-zero length string is true, so your check isn't doing what it's supposed to. It should be  
`if event.propertyName in {"yearly", "monthly", "daily"}` (the curly braces initialize a [set](https://docs.python.org/2.7/library/stdtypes.html#set-types-set-frozenset); you could also use square brackets to initialize a list, or parentheses for a tuple)

That aside, is there a line number on the exception you're getting?

---

<div class="post-metadata">

**Author:** ![speters](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@speters](https://forum.inductiveautomation.com/u/speters)\
**Post date:** [January 19, 2021, 10:34pm UTC](https://forum.inductiveautomation.com/t/system-dataset-todataset-pyfloat-cannot-be-cast/42416/3 "2021-01-19T22:34:20Z")

</div>

The top part is working, just let’s me know when to run the script. I’m getting the error when I go to create the dataset, so line 27.  
This is what each list returns:  
[20.0, 0.0] # data  
[’  
&nbsp;&nbsp;  
Total&nbsp’, ‘  
Product&nbsp;  
I/II&nbsp’] #header I have this header static in another script for something else and works great for increasing the column header height on the table so I can multi-line it.

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [January 19, 2021, 10:38pm UTC](https://forum.inductiveautomation.com/t/system-dataset-todataset-pyfloat-cannot-be-cast/42416/4 "2021-01-19T22:38:29Z")

</div>

Another aside, this is not the proper use of prepared SQL statements and leaves you open for an injection attack if `loc`, `start`, or `end` are human inputs

```auto
query="""
    SELECT COALESCE(SUM(weight/2000.0),0.0) as 'Product I/II'
    FROM truck_counts
    WHERE 
        location = ? 
        AND product_type = 'Product_I_II' 
        AND t_stamp BETWEEN ? AND ?
"""
args = [location, start_time, end_time]
p1=system.db.runPrepQuery(query, args)

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 19, 2021, 10:42pm UTC](https://forum.inductiveautomation.com/t/system-dataset-todataset-pyfloat-cannot-be-cast/42416/5 "2021-01-19T22:42:55Z")

</div>

There’s no line 27 in what you posted, but I’m pretty sure the issue is this:  
`header.extend(p1h)`  
If you only have a single item, you should be using `append` - `extend` is used to add all the elements from one sequence to the end of another.

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [January 19, 2021, 10:51pm UTC](https://forum.inductiveautomation.com/t/system-dataset-todataset-pyfloat-cannot-be-cast/42416/6 "2021-01-19T22:51:57Z")

</div>

> The top part is working, just let’s me know when to run the script

As long as we acknowledge that 'working' and 'doing what you expect' are two separate things 😉

---

<div class="post-metadata">

**Author:** ![speters](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@speters](https://forum.inductiveautomation.com/u/speters)\
**Post date:** [February 15, 2021, 9:24pm UTC](https://forum.inductiveautomation.com/t/system-dataset-todataset-pyfloat-cannot-be-cast/42416/7 "2021-02-15T21:24:54Z")

</div>

Figured out what the problem was…gotta thank Nick Mudge in another post for jogging a few brain cells. I forgot the data part of that fuction is a list of lists. Just created another list, and after appending everything in my original list, appended that list to the new one list. Works great now.

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [August 4, 2023, 3:18pm UTC](https://forum.inductiveautomation.com/t/system-dataset-todataset-pyfloat-cannot-be-cast/42416/8 "2023-08-04T15:18:07Z")

</div>

I had something very similar.

```auto

testsHeaders = ['Sample '+str(i) for i,j in valuesIn] 
testsData = [[float(j) for i,j in valuesIn]]
print testsHeaders
print testsData
print len(testsHeaders),len(testsData)
datasetTests = system.dataset.toDataSet(testsHeaders, testsData)

```

When I added an additional set of brackets for testsData it worked.  
Without them, I got the error about the floats.  
There are 240 i, and 240 j.  
i is an integer. j is a float.  
Why did I need the second brackets to make this work?

Snip of results. I don't understand why it works with 240, 1 for lengths instead of 240, 240.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/4/b4d0f2367d7f17431a4a957f3dd765e0150f23fc.png)

These are just he rules of the function?

---

<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:** [August 4, 2023, 3:24pm UTC](https://forum.inductiveautomation.com/t/system-dataset-todataset-pyfloat-cannot-be-cast/42416/9 "2023-08-04T15:24:24Z")

</div>

Because, `system.dataset.toDataSet()` expects a List of Lists as the second argument.

Each list is a row, each item in each list is a value in that column.

You get the error stating that a `float can not be converted to a PySequence` because the function treats it like a list of lists, but then the first "list" is actually a float.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 4, 2023, 3:25pm UTC](https://forum.inductiveautomation.com/t/system-dataset-todataset-pyfloat-cannot-be-cast/42416/10 "2023-08-04T15:25:22Z")

</div>

`headers` need to be a list, which is what you have.  
`data` needs to be a list of lists, where each inner list is a row.

What you have here is a dataset with one row.
