How do you convert a string column (with 4-digit year values) to a DATE type in Foundry Code Workbooks?

428 Views Asked by At

Is there a way to convert a column with 4-digit year values to a DATE type and have it display as such in Foundry?

Right now, I am working with a column called year that contains values like: 1999, 2001, 2010 and its type is a STRING.

When I write code to convert it to a date, it converts the 4 digit year to a 10 digit year, month, and date like so 2010-01-01.

Here is some code I've tried:

  1. df = df.withColumn('year_mfr', F.trunc(df.year_mfr, 'yyyy'))
  2. df = df.withColumn('year_mfr', F.to_date(df.year_mfr, 'yyyy'))
1

There are 1 best solutions below

0
On

The date type in spark is defined as a 10 digit year month and day so unfortunately it isn't possible to truncate it...

https://spark.apache.org/docs/1.5.0/api/java/org/apache/spark/sql/types/DateType.html

A workaround for this would be to convert the year to an integer.