将pyspark数据框列转换为特定的时间戳格式

时间:2020-03-29 00:31:55

标签: timestamp strptime pyspark-dataframes

我的pyspark数据框中有两种不同的日期格式: 格式1 = 2019-10-03 08.19.42.274014 格式2 = 2020-03-20 11:05:48.899000 我想摆脱两种格式中的微秒: new_format1 = 2019-10-03 08.19.42 new_format2 = 2020-03-20 11:05:48

我能够按照我的 df 所示的1种格式进行操作:

 ts_col1                           ts_col2                             ts_col3 
 2019-10-03 08.19.42.274014        2019-10-03 08.19.42.274014          2019-10-03 08.19.42.274014 
 2020-03-20 11:05:48.899000        2020-03-20 11:05:48.899000          2020-03-20 11:05:48.899000

我的代码是:

format = "yyyy-MM-dd HH:mm:ss" df = df.withColumn('ts_col1', f.unix_timestamp('ts_col1', format).cast('timestamp')) df = df.withColumn('ts_col2', f.unix_timestamp('ts_col2', format).cast('timestamp')) df = df.withColumn('ts_col3', f.unix_timestamp('ts_col3', format).cast('timestamp')) df.show(2, False)

我的df结果是:

ts_col1                           ts_col2                             ts_col3 
  null                              null                                null                              
  2020-03-20 11:05:48               2020-03-20 11:05:48                 2020-03-20 11:05:48

我想在df中为这两种格式更新没有空值的列。它们最终都以各自的格式还是以相同的格式(两者之一)都没有关系。我唯一关心的是摆脱df列中的微秒。

任何帮助将不胜感激。 谢谢

0 个答案:

没有答案