2

I have a spark dataframe.

root
 |-- col1: string (nullable = true)
 |-- Id: string (nullable = true)
 |-- col2: string (nullable = true)
 |-- col3: string (nullable = true)
 |-- col4: string (nullable = true)
 |-- date1: string (nullable = true)
 |-- col5: string (nullable = true)
 |-- date2: string (nullable = true)

I just want to convert the date2 column to date. I used the below code to do that

to_date(myDF$date2)

But the dataframe remains the same. No change in date2 datatype.

How can I change the column date2 to date dataType?

2
  • 2
    The convention in R is that you need to make an assignment in order to make a transformation "stick". This question does not actually have sufficient information to test any code. Commented Apr 5, 2016 at 6:24
  • That does solve my problem myDF$date2 <- to_date(myDF$date2) Commented Apr 5, 2016 at 6:29

1 Answer 1

4
myDF <- withColumn(myDF, "date2", cast(myDF$date2, "date"))

See these pages in the SparkR documentation for the latest version (2.0.1 at the time of writing this):

The cast will not change the existing dataframe, so you need to create a new dataframe or replace the existing dataframe with the newly casted column replacing the old column of the same name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.