【发布时间】:2021-09-28 18:35:11
【问题描述】:
我有一个带有字符串日期时间列的数据框。 我将它转换为时间戳,但值正在改变。 以下是我的代码,谁能帮我在不改变值的情况下进行转换。
df=spark.createDataFrame(
data = [ ("1","2020-04-06 15:06:16 +00:00")],
schema=["id","input_timestamp"])
df.printSchema()
#Timestamp String to DateType
df = df.withColumn("timestamp",to_timestamp("input_timestamp"))
# Using Cast to convert TimestampType to DateType
df.withColumn('timestamp_string', \
to_timestamp('timestamp').cast('string')) \
.show(truncate=False)
这是输出:
+---+--------------------------+-------------------+-------------------+
|id |input_timestamp |timestamp |timestamp_string |
+---+--------------------------+-------------------+-------------------+
|1 |2020-04-06 15:06:16 +00:00|2020-04-06 08:06:16|2020-04-06 08:06:16|
+---+--------------------------+-------------------+-------------------+
我想知道为什么时间会从 15 点变为 8 点,我该如何防止呢?
【问题讨论】: