gpt4 book ai didi

python - 名称错误 : global name 'NoneType' is not defined in Spark

转载 作者:行者123 更新时间:2023-11-28 21:45:42 27 4
gpt4 key购买 nike

我编写了一个 UDF,用于将名为“latest_travel_date”的列中的一些特定日期值替换为“NA”。但是,此列还包含许多空值,因此我也在 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"))

但是,我不断收到错误消息:NameError:未定义全局名称“NoneType”

谁能帮我解决这个问题?

最佳答案

这个问题可以通过两种方式解决。

如果你试图从你的数据帧中找到 Null 值,你应该使用 NullType .

像这样:

if type(date_col) == NullType

或者您可以像这样查找 date_col 是否为 None:

if date_col is None

希望对您有所帮助。

关于python - 名称错误 : global name 'NoneType' is not defined in Spark,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39041316/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com