Spark 2.0 toPandas方法

时间:2017-02-17 06:22:11

标签: python apache-spark pyspark

我有一个如下所示的火花数据框:

topics.show(2)
+-----+--------------------+--------------------+--------------------+
|topic|         termIndices|         termWeights|        topics_words|
+-----+--------------------+--------------------+--------------------+
|    0|[0, 39, 68, 43, 5...|[0.06362107696025...|[, management, sa...|
|    1|[3, 1, 8, 6, 4, 1...|[0.03164821806301...|[objectives, lear...|
+-----+--------------------+--------------------+--------------------+
only showing top 2 rows

然而,当我尝试使用1.6中的下面的方法转换为pandas数据帧时,我收到错误。

topics.toPandas()

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-165-4c1231b68769> in <module>()
----> 1 topics.toPandas()

/Users/i854319/spark2/python/pyspark/sql/dataframe.pyc in toPandas(self)
   1440         """
   1441         import pandas as pd
-> 1442         return pd.DataFrame.from_records(self.collect(), columns=self.columns)
   1443 
   1444     ##########################################################################################

/Users/i854319/spark2/python/pyspark/sql/dataframe.pyc in collect(self)
    307         [Row(age=2, name=u'Alice'), Row(age=5, name=u'Bob')]
    308         """
--> 309         with SCCallSiteSync(self._sc) as css:
    310             port = self._jdf.collectToPython()
    311         return list(_load_from_socket(port, BatchedSerializer(PickleSerializer())))

/Users/i854319/spark2/python/pyspark/traceback_utils.pyc in __enter__(self)
     70     def __enter__(self):
     71         if SCCallSiteSync._spark_stack_depth == 0:
---> 72             self._context._jsc.setCallSite(self._call_site)
     73         SCCallSiteSync._spark_stack_depth += 1
     74 

AttributeError: 'NoneType' object has no attribute 'setCallSite'

所以不确定Spark 2.0.2中是否存在此方法中的错误或出现问题?

1 个答案:

答案 0 :(得分:0)

复制我的answer from a related question

这有一个未解决的问题:

https://issues.apache.org/jira/browse/SPARK-27335?jql=text%20~%20%22setcallsite%22

发布者建议强制将DF的后端与Spark上下文同步:

df.sql_ctx.sparkSession._jsparkSession = spark._jsparkSession
df._sc = spark._sc

这对我们有用,希望在其他情况下也可以。