我已经编写了一个UDF,用“NA”替换名为“latest_travel_date”的列中的一些特定日期值。但是,此列还包含许多空值,因此我在UDF中也处理了这个问题。(请参见下文)

Query:
def date_cleaner(date_col):
    if type(date_col) == NoneType:
        pass
    else:
        if year(date_col) in ('1899','1900'):
            date_col= 'NA'
        else:
            pass
    return date_col

date_cleaner_udf = udf(date_cleaner, DateType())

Df3= Df2.withColumn("latest_cleaned", date_cleaner_udf("latest_travel_date"))

但是,我不断地发现错误:
名称错误:未定义全局名称“NoneType”
有人能帮我解决这个问题吗?

最佳答案

这个问题可以用两种方法来解决。
如果试图从数据帧中查找空值,则应使用NullType
这样地:

if type(date_col) == NullType

或者您可以找到日期栏是否不是这样的:
if date_col is None

我希望这能有帮助。

关于python - NameError:全局名称“NoneType”未在Spark中定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39041316/

10-11 10:26
查看更多