python - 将字符串转换为日期列
问题描述
我想将 String 列转换为 Date 列,但结果我收到了带有空值的列。
from pyspark.sql.functions import expr, from_unixtime, dayofmonth, unix_timestamp, year, to_date, col
dane3.withColumn('date', to_date(unix_timestamp(col('dateRep'),'%d.%m.%Y').cast("timestamp"))).show()
解决方案
你不需要像sql这样的%
之前。文档中的d/m/y
更多信息
df = spark.createDataFrame(
[
(1, '27.08.2020'),
(2, '27.08.2019'),
],
['id', 'txt']
)
df = df.withColumn('formatted',
to_date(unix_timestamp(col('txt'), 'dd.MM.yyyy').cast("timestamp")))
df.show()
+---+----------+----------+
| id| txt| formatted|
+---+----------+----------+
| 1|27.08.2020|2019-12-29|
| 2|27.08.2019|2018-12-30|
+---+----------+----------+
推荐阅读
- ios - 在 Xcode 界面构建器中创建动态调整大小的 UILabel
- sql - SQL 循环和执行查询 - 必须声明标量变量
- javascript - 防止点击处理程序在孩子身上触发
- sql - SQL Select row, Update that row, No race conditions
- php - 试图在 php 中组合这两个 json 数组
- python - 如何对散景表中的线条进行分组?
- c++ - How to read in Hebrew letters in Visual Studios C++
- javascript - 我可以在异步函数的 try/catch 块中使用多个“等待”吗?
- networking - 如果我从网站下载文件,可持续传输速度最低的可能是多少
- c# - C# - 无法删除 JSON 中的反斜杠