Dataset Add Column Date Type

How do I use system.dataset.addColumn() to add a date formatted column to the dataset? I’ve tried

data2 = system.dataset.addColumn(data, colCount, newColumn, 'Date', Date)
data2 = system.dataset.addColumn(data, colCount, newColumn, 'Date', java.util.Date)
data2 = system.dataset.addColumn(data, colCount, newColumn, 'Date', 'java.util.Date')
data2 = system.dataset.addColumn(data, colCount, newColumn, 'Date', 'Date')

They all throw errors

It should be this, or

from java.util import Date
data2 = system.dataset.addColumn(data, colCount, newColumn, 'Date', Date)

Or I think this would also work:

d = system.date.now()
data2 = system.dataset.addColumn(data, colCount, newColumn, 'Date', type(d))

I was able to get it to work with

data2 = system.dataset.addColumn(data, colCount, newColumn, 'Date', str)

That looks like just an empty string column being added with the name “Date”. Maybe the data you were trying to add wasn’t an actual date object?

2 Likes

The newColumn list entries were made with system.date.format(). The list is not empty, but you’re probably correct that they’re not the Date datatype, but I was still getting errors that had nothing to do with type mismatch. The issue was likely that I didn’t import the java type Date

system.date.format turns date objects into strings (a lossy operation that should only be done at the last possible moment before presentation), so it makes sense you were getting type coercion errors trying to put a string into a date-typed column.

Had I imported the type, ya I probably would have gotten those errors but I didn’t actually make it that far because I didn’t import the type :sweat_smile:

1 Like