繁体   English   中英

Pyspark to_date() function 在 Windows 和 WSL Z3D945423F8E96496C60429BA 上给出了不同的答案

[英]Pyspark to_date() function gives different answers on Windows and WSL Ubuntu

我有一个 function 将 int 转换为日期,然后将其输入 datediff 以查找事件发生后的天数。 One of our tests passes on PySpark Windows and in our Azure DevOps pipeline, but fails when run on PySpark in WSL Ubuntu. 我们已将其缩小到to_date() function 在 2 个平台上产生不同的结果,但不明白为什么。

import pyspark.sql.functions as F
import datetime
def from_int_to_date(int_date: int) -> datetime.datetime:
    """
    Convert an integer in YYYYMMDD format into a datetime object
    """
    return datetime.datetime.strptime(str(int_date), "%Y%m%d")

如果我计算F.to_date(F.lit(from_int_to_date(20190401))) Column<b"to_date(TIMESTAMP '2019-04-01 00:00:00')">在 Windows 和Column<b"to_date(TIMESTAMP('2019-03-31 23:00:00.0'))>在 WSL 下运行的版本上。

我住在英国,2019 年 4 月 1 日,我们更改了夏天的时钟,所以我可以理解为什么它会倒退一个小时,因为输入 int 为 20190331 时不会出现问题。我只是想理解为什么 to_date() 的行为在两个系统上是不同的,以及我们应该做些什么来缓解这个(以及任何其他差异),因为理想情况下我们的代码将与平台无关。

使用配置spark.sql.session.timeZone将时区设置为 spark 驱动程序,这样您就不会依赖系统时钟。

spark.conf.set("spark.sql.session.timeZone", "Europe/London")

即使创建了 spark session,也可以解决此选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM