PySpark - 使用 TimeStamp 数据类型解决 isnan 错误

PySpark - Resolving isnan errors with TimeStamp datatype

我正在尝试创建一个函数来检查数据质量(nans/nulls 等) 我在 PySpark DataFrame

上有以下代码 运行
df.select([f.count(f.when((f.isnan(c) | f.col(c).isNull()), c)).alias(c) for c in cols_check]).show()

只要要检查的列是 strings/integers,我就没有问题。但是,当我检查数据类型为 datetimestamp 的列时,我收到以下错误:

cannot resolve 'isnan(Date_Time)' due to data type mismatch: argument 1 requires (double or float) type, however, 'Date_Time' is of timestamp type.;;\n'Aggregate...

列中有明确的空值,我该如何解决?

您可以使用 df.dtypes 检查每列的类型,并能够像这样以不同方式处理 timestampdate 空计数:

from pyspark.sql import functions as F

df.select(*[
    (
        F.count(F.when((F.isnan(c) | F.col(c).isNull()), c)) if t not in ("timestamp", "date")
        else F.count(F.when(F.col(c).isNull(), c))
    ).alias(c)
    for c, t in df.dtypes if c in cols_check
]).show()