小编Nik*_*uer的帖子

Pyspark:两个日期之间的差异(Cast TimestampType、Datediff)

有一个包含事件和特定时间戳的表。我很难计算使用 Pyspark 2.0 API 过去的天数。当时间戳遵循另一种格式(yyyy-mm-dd)时,我设法做同样的事情

    +-------------------+------------------------+----------+--------------+
     | first_booking_date|first_booking_date_clean|  today   |customer_since|
    +-------------------+------------------------+----------+--------------+
     |02-06-2011 20:52:04|              02-06-2011|02-06-2011|          null|
     |03-06-2004 18:15:10|              03-06-2004|02-06-2011|          null|
Run Code Online (Sandbox Code Playgroud)

我尝试了以下操作(没有任何效果):-使用字符串操作提取日期并使用 datediff-转换为时间戳,然后提取 dd:MM:yy(->result null)-我更喜欢使用 pyspark 命令而不是使用 sql 进行任何其他转换

非常感谢帮助,最好,非常感谢!!!

编辑:这是一个不起作用的例子:

import datetime
today = datetime.date(2011,2,1)
today = "02-06-2011"
first_bookings = first_bookings.withColumn("today",F.lit(today))
first_bookings = first_bookings.withColumn("first_booking_date_clean",F.substring(first_bookings.first_booking_date, 0, 10))
first_bookings = first_bookings.withColumn("customer_since",F.datediff(first_bookings.today,first_bookings.first_booking_date_clean))
Run Code Online (Sandbox Code Playgroud)

timestamp datediff pyspark

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

datediff ×1

pyspark ×1

timestamp ×1