Github user BryanCutler commented on a diff in the pull request:
https://github.com/apache/spark/pull/15821#discussion_r106237765
--- Diff: python/pyspark/sql/dataframe.py ---
@@ -1597,21 +1598,46 @@ def toDF(self, *cols):
return DataFrame(jdf, self.sql_ctx)
@since(1.3)
- def toPandas(self):
- """Returns the contents of this :class:`DataFrame` as Pandas
``pandas.DataFrame``.
+ def toPandas(self, useArrow=False):
+ """
+ Returns the contents of this :class:`DataFrame` as Pandas
``pandas.DataFrame``.
This is only available if Pandas is installed and available.
+ :param useArrow: Make use of Apache Arrow for conversion, pyarrow
must be installed
+ and available on the calling Python process (Experimental).
+
.. note:: This method should only be used if the resulting
Pandas's DataFrame is expected
to be small, as all the data is loaded into the driver's
memory.
+ .. note:: Using pyarrow is experimental and currently supports the
following data types:
+ StringType, BinaryType, BooleanType, DoubleType, FloatType,
ByteType, IntegerType,
+ LongType, ShortType
+
>>> df.toPandas() # doctest: +SKIP
age name
0 2 Alice
1 5 Bob
"""
- import pandas as pd
- return pd.DataFrame.from_records(self.collect(),
columns=self.columns)
+ if useArrow:
+ from pyarrow.table import concat_tables
+ tables = self._collectAsArrow()
+ table = concat_tables(tables)
+ return table.to_pandas()
+ else:
+ import pandas as pd
+ return pd.DataFrame.from_records(self.collect(),
columns=self.columns)
+
+ def _collectAsArrow(self):
--- End diff --
This is done on the Scala tests, the error that is thrown is:
```
java.lang.UnsupportedOperationException: Unsupported data type:
ArrayType(IntegerType,false)
at org.apache.spark.sql.ColumnWriter$.apply(ArrowConverters.scala:416)
at
org.apache.spark.sql.ArrowConverters$$anonfun$1.apply(ArrowConverters.scala:162)
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]