System.dataset.addColumn can't convert error

Hi,
I’m trying to combine two datasets and I want to use the addColumn method.
I create a tag history column with a dictionary and the append method then i convert to a dataset and then to a pyDataset, i pass it to the addColumn function but I get an error about the data ype of the column
Here’s the final part of the code with some prints that you can see in the console

print col_caravaggio

col_caravaggio = system.dataset.toDataSet(header_caravaggio, col_caravaggio)
print col_caravaggio

#Dataset_finale
pydataset_caravaggio=system.dataset.toPyDataSet(col_caravaggio)
for row in pydataset_caravaggio:
    for value in row:
        print value

dataset_finale=system.dataset.addColumn(dataset_titec, pydataset_caravaggio,"CARAVAGGIO",float)

print dataset_finale

Your are using addColumn wrong, it expects a list of values for the column variable. You are passing in an entire dataset.

system.dataset.addColumn - Ignition User Manual 8.1 - Ignition Documentation (inductiveautomation.com)

Try

print col_caravaggio

dataset_finale=system.dataset.addColumn(dataset_titec, col_caravaggio,"CARAVAGGIO",float)

print dataset_finale
1 Like

That seems overly complicated to merge two datasets.
How do you want them joined ?
Let's say you have

[
	[a1, a2, a3],
	[b1, b2, b3]
]
[
	[c1, c2, c3],
	[d1, d2, d3]
]

do you want this:

[
	[a1, a2, a3],
	[b1, b2, b3],
	[c1, c2, c3],
	[d1, d2, d3]
]

or this:

[
	[a1, a2, a3, c1, c2, c3],
	[b1, b2, b3, d1, d2, d3]
]

the second case, i have two datasaet with the same numbers of rows

a = [
	['a1', 'a2', 'a3'],
	['b1', 'b2', 'b3']
]
b = [
	['a4', 'a5', 'a6'],
	['b4', 'b5', 'b6']
]

[x + y for x, y in zip(a, b)]

[['a1', 'a2', 'a3', 'a4', 'a5', 'a6'], ['b1', 'b2', 'b3', 'b4', 'b5', 'b6']]

replace a and b by your pyDataSets.

Alternatively, you could do it with basic datasets and addColumn, but frankly I tend to avoid those as I think they don't integrate well with python.

1 Like

Thank you,
i ended up using the addColumn function at the end because is a bit more readeable and probably better for future maintenance.
For some reason i miread the doc and thought that the function required a pydataset instead of a python list.
Now it merges two tabels like I want to but the values all comes out with a lot of decimals ( i round them up with 2 decimals before addinf them to the list so it’s a function problems).
This is the output excel, the last column is the one I added
immagine
Any way to fix this?

more readable than [x + y for x, y in zip(a, b)] ?
I'd be curious to see the code that's more readable than a single list comprehension :X

Sometimes its not about being terse, but about being able to understand the script especially when you come back to it 6 months down the road.

Granted that is a fairly simple list comprehension, but if one is not use to it, it can look intimidating. A great time to learn it though!

1 Like