-1

Hi i'm trying to get weekday from timestamp with am pm. so I have a dataframe

dataframe = spark.createDataFrame(

        data = [ ("1","9/8/2019 10:01:28 PM")],

        schema=["id","input_timestamp"])
dataframe.show()
dataframe.printSchema()

And if I do this its giving null as output. I have to do it using to_date

dataframe.withColumn("timestamp",to_date("input_timestamp", "MM/dd/yyyy HH:mm:ss am")) \
.show(truncate=False)

+---+--------------------+---------+
|id |input_timestamp     |timestamp|
+---+--------------------+---------+
|1  |9/8/2019 10:01:28 PM|null     |
+---+--------------------+---------+
Koushik Roy
  • 6,868
  • 2
  • 12
  • 33
GLADOS
  • 33
  • 3

1 Answers1

0

Use M/d/yyyy hh:mm:ss a as format.

spark.conf.set('spark.sql.legacy.timeParserPolicy', 'LEGACY')

spark.sparkContext.parallelize([("9/8/2019 10:01:28 PM",)]).toDF(['ts_str']). \
    withColumn('ts', func.to_timestamp('ts_str', 'M/d/yyyy hh:mm:ss a')). \
    show(truncate=False)

# +--------------------+-------------------+
# |ts_str              |ts                 |
# +--------------------+-------------------+
# |9/8/2019 10:01:28 PM|2019-09-08 22:01:28|
# +--------------------+-------------------+

# root
#  |-- ts_str: string (nullable = true)
#  |-- ts: timestamp (nullable = true)
samkart
  • 6,007
  • 2
  • 14
  • 29