10-03-2019 01:41 AM
Hi All,
I am trying to convert a dataframe column which is in the format of string to date type format yyyy-MM-DD?
I have written a sql query and stored it in dataframe.
df3 = sqlContext.sql(sqlString2)
df3.withColumn(df3['CalDay'],pd.to_datetime(df3['CalDay'],format = '%Y%m%d'))
I get this error
ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.
How is this happening?
10-03-2019 02:56 AM
Hi @desai.n.3, In scala please use coalesce function to convert Date format,
df.withColumn("Date", coalesce(
to_timestamp($"Date", "yyyyMMdd"))).show<br>
10-03-2019 03:58 AM
Hi Thanks for the reply,
It says coalesce fucntion not defined, plus gives invalid syntax with $
10-03-2019 08:12 AM
If you are using the Databricks notebook the above code works and for Pandas use below code,
df['Date']= pd.to_datetime(df['Date'])
10-24-2019 10:56 PM
You gave him scala code, his code is obviously python
"Databricks notebooks" can be python, scala even SQL so my advice is check the question and answer in the language they are using at least (both if you know it for future people)10-24-2019 11:06 PM
Registered to post this so forgive the formatting nightmare
This is a python databricks script function that allows you to convert from string to datetime or date and utilising coalesce
from pyspark.sql.functions import coalesce, to_datedef to_datetime_(col, format): # Spark 2.2 or later syntax, for < 2.2 use unix_timestamp and cast return coalesce(to_date(col, format))
df3 = sqlContext.sql(sqlString2)df3.withColumn(df3['CalDay'],pd.to_datetime_(df3['CalDay'],format = '%Y%m%d'))
10-24-2019 11:08 PM
the resulting comment is looking nothing like it does in edit mode
this is the function
def to_datetime_(col, format):
# Spark 2.2 or later syntax, for < 2.2 use unix_timestamp and cast
return coalesce(to_date(col, format))
Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.
If there isn’t a group near you, start one and help create a community that brings people together.
Request a New Group