Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Timestamp to Date format in DataFrame?

I have a DataFrame with Timestamp column, which i need to convert as Date format.

Is there any Spark SQL functions available for this?

like image 626
Shankar Avatar asked Nov 17 '16 13:11

Shankar


People also ask

How do I convert timestamps to dates?

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

How do you convert a timestamp to a Dataframe in Python?

read_csv(), the timestamps column from the data Dataframe is given as an argument in the to_datetime() for it to be converted into DateTime. unit='s' is used to convert the values of the timestamp column to epoch time after converting the values to DateTime it is stored in a column called 'Datetime' in the Dataframe.

How do I change the date format in a Dataframe in Python?

Function usedstrftime() can change the date format in python.


2 Answers

You can cast the column to date:

Scala:

import org.apache.spark.sql.types.DateType  val newDF = df.withColumn("dateColumn", df("timestampColumn").cast(DateType)) 

Pyspark:

df = df.withColumn('dateColumn', df['timestampColumn'].cast('date')) 
like image 98
Daniel de Paula Avatar answered Sep 24 '22 05:09

Daniel de Paula


In SparkSQL:

SELECT   CAST(the_ts AS DATE) AS the_date FROM the_table 
like image 35
dslack Avatar answered Sep 23 '22 05:09

dslack