我编写了一个 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/