Spotfire Change Datatype of Column in Datatable with Ironpython

This is my code for changing the datatype of a column with a list of datatypes, columns, and datatables:

def change_datatypes_alt():
    for x in range(len(dataTables)):
        table = Document.Data.Tables[dataTables[x]]
        #updateCalc(table, columnNames[x], dataTypes[x]+'(['+columnNames[x]+'])')
        print("Changing DataType of Column "+columnNames[x]+" to "+dataTypes[x]+" in DataTable "+dataTables[x])
        t = ExpressionTransformation()
        t.ColumnReplacements.Add(
            columnNames[x],
            dataTypes[x]+'(['+columnNames[x]+'])',
            ColumnSelection([DataColumnSignature(columnNames[x], datatypes.get(dataTypes[x]) )])
        )
        #t.ColumnAdditions.Add(dataTypes[x]+'(['+columnNames[x]+'])')
        table.AddTransformation(t)
    print("Done Changing Datatypes....")

It appears to work. When I look at my DataTable properties source information I see:

6. Select Insert > Transformations...
    Added transformations
        Transformation name: Expression transformation
        Added columns: (None)
        Replaced columns
            Column name expression: DATE_COMPLETION
            Replaced columns: DATE_COMPLETION
            Expression: DateTime([DATE_COMPLETION])

        Excluded columns: (None)
        Excluded columns matching these expressions: (None)

However,  it is clear to me that the column datatype never changed. When I look at the column properties I see:

"DATE_COMPLETION  String"

So the datatype never changed. If I manually attempt to change it to a different datatype it works. I am trying to do this for 300+ columns, so it needs to be automated.

I am using datatables that are linked, with an information link.

Any help appreciated, thanks.

EDIT: FIXED IT --. updated code:

def change_datatypes_alt():
    for x in range(len(dataTables)):
        table = Document.Data.Tables[dataTables[x]]
        #updateCalc(table, columnNames[x], dataTypes[x]+'(['+columnNames[x]+'])')
        print("Changing DataType of Column "+columnNames[x]+" to "+dataTypes[x]+" in DataTable "+dataTables[x])
        t = ExpressionTransformation()
        t.ColumnReplacements.Add(
            columnNames[x],
            dataTypes[x]+'(['+columnNames[x]+'])',
            ColumnSelection(columnNames[x])
        )
        #t.ColumnAdditions.Add(dataTypes[x]+'(['+columnNames[x]+'])')
        table.AddTransformation(t)
    print("Done Changing Datatypes....")

 

(1) Answer

Login