<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: convert string dataframe column MM/dd/yyyy  hh:mm:ss AM/PM to timestamp MM-dd-yyyy  hh:mm:ss in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/convert-string-dataframe-column-mm-dd-yyyy-hh-mm-ss-am-pm-to/m-p/27347#M19224</link>
    <description>&lt;P&gt;You would use a combination of the functions: &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;pyspark.sql.functions.from_unixtime(timestamp, format='yyyy-MM-dd HH:mm:ss')&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt; (documentation) and &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;pyspark.sql.functions.unix_timestamp(timestamp=None, format='yyyy-MM-dd HH:mm:ss')&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt; (documentation)&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from pyspark.sql.types import *
from pyspark.sql.functions import unix_timestamp, from_unixtime
df = spark.createDataFrame(["6/3/2019 5:06:00 AM"], StringType()).toDF("ts_string")
# convert to timestamp type
df1 = df.select(from_unixtime(unix_timestamp('ts_string', 'MM/dd/yyyy hh:mm:ss a')).cast(TimestampType()).alias("timestamp"))
# change timestamp format&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;df2 = df1.select(from_unixtime(unix_timestamp('timestamp', 'MM-dd-yyyy hh:mm:ss')).alias("timestamp2"))
# all together
df3 = df.select(
  'ts_string',
  from_unixtime(unix_timestamp('ts_string', 'MM/dd/yyyy hh:mm:ss a')).cast(TimestampType()).alias("timestamp"),
  from_unixtime(unix_timestamp(from_unixtime(unix_timestamp('ts_string', 'MM/dd/yyyy hh:mm:ss a')).cast(TimestampType()), 'MM-dd-yyyy hh:mm:ss')).alias("timestamp2")
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 14 Jan 2020 01:17:35 GMT</pubDate>
    <dc:creator>lee</dc:creator>
    <dc:date>2020-01-14T01:17:35Z</dc:date>
    <item>
      <title>convert string dataframe column MM/dd/yyyy  hh:mm:ss AM/PM to timestamp MM-dd-yyyy  hh:mm:ss</title>
      <link>https://community.databricks.com/t5/data-engineering/convert-string-dataframe-column-mm-dd-yyyy-hh-mm-ss-am-pm-to/m-p/27346#M19223</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;How to convert string 6/3/2019 5:06:00 AM to timestamp in 24 hour format MM-dd-yyyy hh:mm:ss in python spark.&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 19:04:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/convert-string-dataframe-column-mm-dd-yyyy-hh-mm-ss-am-pm-to/m-p/27346#M19223</guid>
      <dc:creator>Venkata_Krishna</dc:creator>
      <dc:date>2020-01-13T19:04:43Z</dc:date>
    </item>
    <item>
      <title>Re: convert string dataframe column MM/dd/yyyy  hh:mm:ss AM/PM to timestamp MM-dd-yyyy  hh:mm:ss</title>
      <link>https://community.databricks.com/t5/data-engineering/convert-string-dataframe-column-mm-dd-yyyy-hh-mm-ss-am-pm-to/m-p/27347#M19224</link>
      <description>&lt;P&gt;You would use a combination of the functions: &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;pyspark.sql.functions.from_unixtime(timestamp, format='yyyy-MM-dd HH:mm:ss')&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt; (documentation) and &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;pyspark.sql.functions.unix_timestamp(timestamp=None, format='yyyy-MM-dd HH:mm:ss')&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt; (documentation)&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from pyspark.sql.types import *
from pyspark.sql.functions import unix_timestamp, from_unixtime
df = spark.createDataFrame(["6/3/2019 5:06:00 AM"], StringType()).toDF("ts_string")
# convert to timestamp type
df1 = df.select(from_unixtime(unix_timestamp('ts_string', 'MM/dd/yyyy hh:mm:ss a')).cast(TimestampType()).alias("timestamp"))
# change timestamp format&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;df2 = df1.select(from_unixtime(unix_timestamp('timestamp', 'MM-dd-yyyy hh:mm:ss')).alias("timestamp2"))
# all together
df3 = df.select(
  'ts_string',
  from_unixtime(unix_timestamp('ts_string', 'MM/dd/yyyy hh:mm:ss a')).cast(TimestampType()).alias("timestamp"),
  from_unixtime(unix_timestamp(from_unixtime(unix_timestamp('ts_string', 'MM/dd/yyyy hh:mm:ss a')).cast(TimestampType()), 'MM-dd-yyyy hh:mm:ss')).alias("timestamp2")
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 01:17:35 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/convert-string-dataframe-column-mm-dd-yyyy-hh-mm-ss-am-pm-to/m-p/27347#M19224</guid>
      <dc:creator>lee</dc:creator>
      <dc:date>2020-01-14T01:17:35Z</dc:date>
    </item>
  </channel>
</rss>

