Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions python/pyspark/sql/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,11 @@ def collect(self):
@ignore_unicode_prefix
@since(2.0)
def collectAsArrow(self):
"""Returns all the records as an ArrowRecordBatch
"""Returns all records as list of deserialized ArrowPayloads
"""
with SCCallSiteSync(self._sc) as css:
port = self._jdf.collectAsArrowToPython()
return list(_load_from_socket(port, ArrowSerializer()))[0]
return list(_load_from_socket(port, ArrowSerializer()))

@ignore_unicode_prefix
@since(2.0)
Expand Down Expand Up @@ -1573,6 +1573,9 @@ def toPandas(self, useArrow=False):

This is only available if Pandas is installed and available.

:param useArrow: Make use of Apache Arrow for conversion, pyarrow must be installed
on the calling Python process.

.. 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.

Expand All @@ -1581,11 +1584,13 @@ def toPandas(self, useArrow=False):
0 2 Alice
1 5 Bob
"""
import pandas as pd

if useArrow:
return self.collectAsArrow().to_pandas()
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)

##########################################################################################
Expand Down
Loading