How to preserve milliseconds when converting a date and time string to timestamp using PySpark?












2















I am trying to convert a column containing date and time as strings to timestamp, however I am losing the milliseconds part during the conversion.



Data



I have a Spark dataframe df that has a date and a time column containing strings. The time string contains milliseconds, as shown below:



+---------+------------+
|date |time |
+---------+------------+
|2018/1/2 |09:53:25.864|
|2018/1/3 |11:32:21.689|
|2018/1/4 |09:34:51.045|
+---------+------------+


What I tried



I concatenated date and time columns to get date_and_time column (string):



import spark.sql.functions as F

df = df.withColumn('date_and_time', F.concat_ws(' ', df.date, df.time))

df.show(3, False)


Output:



+--------+------------+---------------------+
|date |time |date_and_time |
+--------+------------+---------------------+
|2018/1/2|09:53:25.864|2018/1/2 09:53:25.864|
|2018/1/3|11:32:21.689|2018/1/3 11:32:21.689|
|2018/1/4|09:34:51.045|2018/1/4 09:34:51.045|
+--------+------------+---------------------+


Then, I specified the timestamp format using Simple Date Format Date and Time patterns:



timestamp_format = 'yyyy/M/d HH:mm:ss.SSS'


Then, I tried to convert this string to timestamp using a couple of different ways:



df.select(
df.date_and_time,
F.to_timestamp(df.date_and_time, timestamp_format).alias('method_1'),
F.unix_timestamp(df.date_and_time, format=timestamp_format).cast('timestamp').alias('method_2')
).show(3, False)


As you can see below, the timestamp is missing the milliseconds part:



+---------------------+-------------------+-------------------+
|date_and_time |method_1 |method_2 |
+---------------------+-------------------+-------------------+
|2018/1/2 09:53:25.864|2018-01-02 09:53:25|2018-01-02 09:53:25|
|2018/1/3 11:32:21.689|2018-01-03 11:32:21|2018-01-03 11:32:21|
|2018/1/4 09:34:51.045|2018-01-04 09:34:51|2018-01-04 09:34:51|
+---------------------+-------------------+-------------------+


How can I preserve the milliseconds when converting the string to timestamp?



I am using PySpark (Spark: 2.3.1, Python: 3.6.5).



I have looked at previously answered questions on SO and have not found a suitable solution.










share|improve this question



























    2















    I am trying to convert a column containing date and time as strings to timestamp, however I am losing the milliseconds part during the conversion.



    Data



    I have a Spark dataframe df that has a date and a time column containing strings. The time string contains milliseconds, as shown below:



    +---------+------------+
    |date |time |
    +---------+------------+
    |2018/1/2 |09:53:25.864|
    |2018/1/3 |11:32:21.689|
    |2018/1/4 |09:34:51.045|
    +---------+------------+


    What I tried



    I concatenated date and time columns to get date_and_time column (string):



    import spark.sql.functions as F

    df = df.withColumn('date_and_time', F.concat_ws(' ', df.date, df.time))

    df.show(3, False)


    Output:



    +--------+------------+---------------------+
    |date |time |date_and_time |
    +--------+------------+---------------------+
    |2018/1/2|09:53:25.864|2018/1/2 09:53:25.864|
    |2018/1/3|11:32:21.689|2018/1/3 11:32:21.689|
    |2018/1/4|09:34:51.045|2018/1/4 09:34:51.045|
    +--------+------------+---------------------+


    Then, I specified the timestamp format using Simple Date Format Date and Time patterns:



    timestamp_format = 'yyyy/M/d HH:mm:ss.SSS'


    Then, I tried to convert this string to timestamp using a couple of different ways:



    df.select(
    df.date_and_time,
    F.to_timestamp(df.date_and_time, timestamp_format).alias('method_1'),
    F.unix_timestamp(df.date_and_time, format=timestamp_format).cast('timestamp').alias('method_2')
    ).show(3, False)


    As you can see below, the timestamp is missing the milliseconds part:



    +---------------------+-------------------+-------------------+
    |date_and_time |method_1 |method_2 |
    +---------------------+-------------------+-------------------+
    |2018/1/2 09:53:25.864|2018-01-02 09:53:25|2018-01-02 09:53:25|
    |2018/1/3 11:32:21.689|2018-01-03 11:32:21|2018-01-03 11:32:21|
    |2018/1/4 09:34:51.045|2018-01-04 09:34:51|2018-01-04 09:34:51|
    +---------------------+-------------------+-------------------+


    How can I preserve the milliseconds when converting the string to timestamp?



    I am using PySpark (Spark: 2.3.1, Python: 3.6.5).



    I have looked at previously answered questions on SO and have not found a suitable solution.










    share|improve this question

























      2












      2








      2








      I am trying to convert a column containing date and time as strings to timestamp, however I am losing the milliseconds part during the conversion.



      Data



      I have a Spark dataframe df that has a date and a time column containing strings. The time string contains milliseconds, as shown below:



      +---------+------------+
      |date |time |
      +---------+------------+
      |2018/1/2 |09:53:25.864|
      |2018/1/3 |11:32:21.689|
      |2018/1/4 |09:34:51.045|
      +---------+------------+


      What I tried



      I concatenated date and time columns to get date_and_time column (string):



      import spark.sql.functions as F

      df = df.withColumn('date_and_time', F.concat_ws(' ', df.date, df.time))

      df.show(3, False)


      Output:



      +--------+------------+---------------------+
      |date |time |date_and_time |
      +--------+------------+---------------------+
      |2018/1/2|09:53:25.864|2018/1/2 09:53:25.864|
      |2018/1/3|11:32:21.689|2018/1/3 11:32:21.689|
      |2018/1/4|09:34:51.045|2018/1/4 09:34:51.045|
      +--------+------------+---------------------+


      Then, I specified the timestamp format using Simple Date Format Date and Time patterns:



      timestamp_format = 'yyyy/M/d HH:mm:ss.SSS'


      Then, I tried to convert this string to timestamp using a couple of different ways:



      df.select(
      df.date_and_time,
      F.to_timestamp(df.date_and_time, timestamp_format).alias('method_1'),
      F.unix_timestamp(df.date_and_time, format=timestamp_format).cast('timestamp').alias('method_2')
      ).show(3, False)


      As you can see below, the timestamp is missing the milliseconds part:



      +---------------------+-------------------+-------------------+
      |date_and_time |method_1 |method_2 |
      +---------------------+-------------------+-------------------+
      |2018/1/2 09:53:25.864|2018-01-02 09:53:25|2018-01-02 09:53:25|
      |2018/1/3 11:32:21.689|2018-01-03 11:32:21|2018-01-03 11:32:21|
      |2018/1/4 09:34:51.045|2018-01-04 09:34:51|2018-01-04 09:34:51|
      +---------------------+-------------------+-------------------+


      How can I preserve the milliseconds when converting the string to timestamp?



      I am using PySpark (Spark: 2.3.1, Python: 3.6.5).



      I have looked at previously answered questions on SO and have not found a suitable solution.










      share|improve this question














      I am trying to convert a column containing date and time as strings to timestamp, however I am losing the milliseconds part during the conversion.



      Data



      I have a Spark dataframe df that has a date and a time column containing strings. The time string contains milliseconds, as shown below:



      +---------+------------+
      |date |time |
      +---------+------------+
      |2018/1/2 |09:53:25.864|
      |2018/1/3 |11:32:21.689|
      |2018/1/4 |09:34:51.045|
      +---------+------------+


      What I tried



      I concatenated date and time columns to get date_and_time column (string):



      import spark.sql.functions as F

      df = df.withColumn('date_and_time', F.concat_ws(' ', df.date, df.time))

      df.show(3, False)


      Output:



      +--------+------------+---------------------+
      |date |time |date_and_time |
      +--------+------------+---------------------+
      |2018/1/2|09:53:25.864|2018/1/2 09:53:25.864|
      |2018/1/3|11:32:21.689|2018/1/3 11:32:21.689|
      |2018/1/4|09:34:51.045|2018/1/4 09:34:51.045|
      +--------+------------+---------------------+


      Then, I specified the timestamp format using Simple Date Format Date and Time patterns:



      timestamp_format = 'yyyy/M/d HH:mm:ss.SSS'


      Then, I tried to convert this string to timestamp using a couple of different ways:



      df.select(
      df.date_and_time,
      F.to_timestamp(df.date_and_time, timestamp_format).alias('method_1'),
      F.unix_timestamp(df.date_and_time, format=timestamp_format).cast('timestamp').alias('method_2')
      ).show(3, False)


      As you can see below, the timestamp is missing the milliseconds part:



      +---------------------+-------------------+-------------------+
      |date_and_time |method_1 |method_2 |
      +---------------------+-------------------+-------------------+
      |2018/1/2 09:53:25.864|2018-01-02 09:53:25|2018-01-02 09:53:25|
      |2018/1/3 11:32:21.689|2018-01-03 11:32:21|2018-01-03 11:32:21|
      |2018/1/4 09:34:51.045|2018-01-04 09:34:51|2018-01-04 09:34:51|
      +---------------------+-------------------+-------------------+


      How can I preserve the milliseconds when converting the string to timestamp?



      I am using PySpark (Spark: 2.3.1, Python: 3.6.5).



      I have looked at previously answered questions on SO and have not found a suitable solution.







      python python-3.x apache-spark pyspark timestamp






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 16:22









      RahulRahul

      1469




      1469
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304643%2fhow-to-preserve-milliseconds-when-converting-a-date-and-time-string-to-timestamp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304643%2fhow-to-preserve-milliseconds-when-converting-a-date-and-time-string-to-timestamp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues