Date formats how to cast string to date

0 votes

The following date formats not working for me

>>> df = spark.createDataFrame([("1997/02/28",)], ["t"])
>>> df.select(F.date_format(df.t,"yyyy/MM/dd")).show()
+--------------------------+
|date_format(t, yyyy/MM/dd)|
+--------------------------+
| null|
+--------------------------+

>>> df = spark.createDataFrame([["28-02-1997"],["01-02-2000"],["01-02-2022"]], ["t"])
>>> df.select(df.t.cast("date").alias("n_d")).orderBy("n_d").show()
+----+
| n_d|
+----+
|null|
|null|
|null|
+----+
Jul 29, 2019 in Apache Spark by Rahul
6,009 views

1 answer to this question.

0 votes

Try this, it should work:

> from pyspark.sql.functions import unix_timestamp

> df = spark.createDataFrame([("11/25/1991",), ("11/24/1991",), ("11/30/1991",)], ['date_str'])

> df2 = df.select('date_str', from_unixtime(unix_timestamp('date_str', 'MM/dd/yyy')).alias('date'))

DataFrame[date_str: string, date: timestamp]

> df2.show()


+----------+--------------------+

| date_str| date|

+----------+--------------------+

|11/25/1991|1991-11-25 00:00:...|

|11/24/1991|1991-11-24 00:00:...|

|11/30/1991|1991-11-30 00:00:...|

+----------+--------------------+
answered Jul 29, 2019 by Niall

Related Questions In Apache Spark

0 votes
0 answers

How to create RDD as string file?

Can anyone suggest how to create RDD ...READ MORE

Jul 5, 2019 in Apache Spark by anand
926 views
0 votes
1 answer

How to format a string in Scala?

Hey, To format a string, use the .format ...READ MORE

answered Jul 31, 2019 in Apache Spark by Gitika
• 65,770 points
861 views
0 votes
1 answer

How to access variables in s string interpolation in Scala?

Hey, You can use below code to access variables ...READ MORE

answered Jul 31, 2019 in Apache Spark by Gitika
• 65,770 points
1,186 views
0 votes
1 answer

How to stop messages from being displayed on spark console?

In your log4j.properties file you need to ...READ MORE

answered Apr 24, 2018 in Apache Spark by kurt_cobain
• 9,350 points
5,587 views
+1 vote
2 answers
+1 vote
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
11,072 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
2,571 views
+2 votes
11 answers

hadoop fs -put command?

Hi, You can create one directory in HDFS ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
109,060 views
0 votes
1 answer

How to print string text in scala?

Hi, You can see this example to see ...READ MORE

answered Jul 5, 2019 in Apache Spark by Gitika
• 65,770 points
2,710 views
0 votes
5 answers

How to change the spark Session configuration in Pyspark?

You aren't actually overwriting anything with this ...READ MORE

answered Dec 14, 2020 in Apache Spark by Gitika
• 65,770 points
125,769 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP