当前位置:网站首页>解决报错TypeError:unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘
解决报错TypeError:unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘
2022-07-31 10:13:00 【山顶夕景】
一、问题描述
from pyspark.sql.types import StringType
@udf(returnType = StringType())
def bad_funify(s):
return s + " is fun!"
countries2 = spark.createDataFrame([("Thailand", 3), (None, 4)], ["country", "id"])
countries2.withColumn("fun_country", bad_funify("country")).show()
用一个udf想让df(有country和id两个字段)生成新的一列fun_country
(内容是字符串,内容为country xx is fun
),但是df中有的country
字段内容没有数据(注意类型是None
而不是null
),结果报错如下:
PythonException:
An exception was thrown from the Python worker. Please see the stack trace below.
Traceback (most recent call last):
File "/usr/lib/spark-current/python/lib/pyspark.zip/pyspark/worker.py", line 619, in main
process()
File "/usr/lib/spark-current/python/lib/pyspark.zip/pyspark/worker.py", line 611, in process
serializer.dump_stream(out_iter, outfile)
File "/usr/lib/spark-current/python/lib/pyspark.zip/pyspark/serializers.py", line 211, in dump_stream
self.serializer.dump_stream(self._batched(iterator), stream)
File "/usr/lib/spark-current/python/lib/pyspark.zip/pyspark/serializers.py", line 132, in dump_stream
for obj in iterator:
File "/usr/lib/spark-current/python/lib/pyspark.zip/pyspark/serializers.py", line 200, in _batched
for item in iterator:
File "/usr/lib/spark-current/python/lib/pyspark.zip/pyspark/worker.py", line 452, in mapper
result = tuple(f(*[a[o] for o in arg_offsets]) for (arg_offsets, f) in udfs)
File "/usr/lib/spark-current/python/lib/pyspark.zip/pyspark/worker.py", line 452, in <genexpr>
result = tuple(f(*[a[o] for o in arg_offsets]) for (arg_offsets, f) in udfs)
File "/usr/lib/spark-current/python/lib/pyspark.zip/pyspark/worker.py", line 87, in <lambda>
return lambda *a: f(*a)
File "/usr/lib/spark-current/python/lib/pyspark.zip/pyspark/util.py", line 74, in wrapper
return f(*args, **kwargs)
File "<ipython-input-1051-5a6c51e7c332>", line 5, in bad_funify
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
二、解决方案
这是个很蠢的问题。其实如果country
为空值时,fun_country
应该也是空的,所以就简单加多个判断的逻辑即可。修改udf为good_funity
后:
@udf(returnType=StringType())
def good_funify(s):
return None if s == None else s + " is fun!"
countries2.withColumn("fun_country", good_funify("country")).show()
+--------+---+----------------+
| country| id| fun_country|
+--------+---+----------------+
|Thailand| 3|Thailand is fun!|
| null| 4| null|
+--------+---+----------------+
Reference
边栏推荐
猜你喜欢
随机推荐
NowCoderTOP28-34 binary tree - continuous update ing
WEB核心【记录网站登录人数,记录用户名案例】Cookie技术实现
Kotlin入门介绍篇
内联元素居中
Source code analysis of GZIPInputStream class
loadrunner-controller-手动场景Schedule配置
NowCoderTOP23-27 Binary tree traversal - continuous update ing
Meikle Studio--Hongmeng 14-day development training notes (8)
Mysql+Navicat for Mysql
【LeetCode】387. 字符串中的第一个唯一字符
Data Middle Office Construction (6): Data System Construction
双链表的创建
Dart Log tool class
DC-7-vulnhub
C#多态的实现
【LeetCode】1161.最大层内元素和
单点登录原理及实现方式
Come n times - 09. Implement queues with two stacks
csdn file export to pdf
Day113.尚医通:用户认证、阿里云OSS、就诊人管理