Ato*_*tom 3 python dataframe apache-spark pyspark
我希望在将日期字符串转换为当前时区后从日期字符串中提取年、月、日和小时。我创建了以下独立代码,该代码导致空值。不确定如何处理数据中时间格式的 T 和 Z 分隔符。
from pyspark.sql.functions import unix_timestamp, from_unixtime
df = spark.createDataFrame(
[("2020-02-28T09:49Z",)],
['date_str']
)
df2 = df.select(
'date_str',
from_unixtime(unix_timestamp('date_str', 'yyyy-MM-ddThh:mmZ')).alias('date')
)
df2.show()
Run Code Online (Sandbox Code Playgroud)
从上面的结果可以看出——
+-----------------+----+
| date_str|date|
+-----------------+----+
|2020-02-28T09:49Z|null|
+-----------------+----+
Run Code Online (Sandbox Code Playgroud)
有人可以指导我如何处理这个问题并在此处打印日期吗?
Shu*_*Shu 11
to_timestamp, from_unixtime(unix_timestamp())对于这种情况,我们可以使用任一函数。
"yyyy-MM-dd'T'hh:mm'Z'"将T,Z用单引号引起来!Example:
df.select('date_str', to_timestamp('date_str',"yyyy-MM-dd'T'hh:mm'Z'").alias('date')).show()
df.select('date_str', from_unixtime(unix_timestamp('date_str', "yyyy-MM-dd'T'hh:mm'Z'")).alias('date')).show()
#+-----------------+-------------------+
#| date_str| date|
#+-----------------+-------------------+
#|2020-02-28T09:49Z|2020-02-28 09:49:00|
#+-----------------+-------------------+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7001 次 |
| 最近记录: |