我正在尝试将函数应用于 pyspark 中的每组数据集。我得到的第一个错误是
Py4JError: An error occurred while calling o62.__getnewargs__. Trace: py4j.Py4JException: Method __getnewargs__([]) does not exist
为了解决上述问题,我删除了 spark 功能(我有spark.range()
)。现在错误已解决,但我现在得到以下信息:
File "/opt/cloudera/parcels/SPARK2-2.3.0.cloudera2-1.cdh5.13.3.p0.316101/lib/spark2/python/pyspark/serializers.py", line 276, in load_stream import pyarrow as pa ImportError: No module named pyarrow
但是当我自己尝试它时,它可以工作,即。
df = pd.DataFrame({"a": [1, 2, 3]})
pa.Table.from_pandas(df)
pyarrow.Table
a: int64
__index_level_0__: int64
metadata
--------
{'pandas': '{"pandas_version": "0.23.0", "index_columns": ["__index_level_0__"], "columns": [{"metadata": null, "field_name": "a", "name": "a", "numpy_type": "int64", "pandas_type": "int64"}, {"metadata": null, "field_name": "__index_level_0__", "name": null, "numpy_type": "int64", "pandas_type": "int64"}], "column_indexes": [{"metadata": null, "field_name": null, "name": null, "numpy_type": "object", "pandas_type": "bytes"}]}'}
失败的例子——取自这里
import pyspark.sql.functions as F
import pandas as pd
cols = ['id', 'val']
vals = [('A', 5), ('A', 3), ('A', 7), ('B', 12), ('B', 15), ('C', 3)]
d1 = sqlContext.createDataFrame(vals, cols)
>>> @F.pandas_udf(d1.schema, F.PandasUDFType.GROUPED_MAP)
... def subtract_mean(pdf):
... return pdf.assign(v=pdf.v - pdf.v.mean())
...
>>> try1 = d1.groupby('id').apply(subtract_mean)
>>> try1.show()
如果我尝试转换为 pandas 数据框,它不起作用,因为它没有该schema
属性。
我在这里想念什么?