PySpark StructField StringType 或 TimestampType

问题描述 投票:0回答:1

我有一个 pyspark 数据框的模式

(StructField, StructType)
,我们有一个日期列(值例如:2023-10-05)。该日期应该使用 StringType 还是 TimestampType 格式化数据吗?我相信 StructField 只有 StringType 或 TimestampType,但没有像 DateType 这样的东西。

new_schema = [StructField("item_id", StringType(), True),
                     StructField("date", TimestampType(), True),
                     StructField("description", StringType(), True)]

由于以下原因,我更喜欢使用字符串作为日期而不是时间戳。

1、TimestampType更多用于像人们关心实时性的秒、毫秒数据的流式数据。在我们的例子中,我们只需要日期。字符串就足够了。

2、从一致性的角度来看,字符串传输比时间戳更稳定。

3,从强制转换的角度来看,字符串到日期更像是向下强制转换,而时间戳到日期更像是向上强制转换,将字符串转换为日期比将时间戳转换为日期更安全。

不确定我的观点是否有效,感谢您的意见。

好奇是否有人知道为什么 pyspark StructField 只有 StringType 或 TimestampType 而没有 dateType?

pyspark data-structures datetime-format
1个回答
0
投票

弄清楚它可以使用DateType。

from pyspark.sql.types import StructType, StructField, StringType, DateType

所以你可以使用DateType

© www.soinside.com 2019 - 2024. All rights reserved.