Skip to content

Implement to_pandas() #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2023
Merged
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
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ from having to lock the GIL when running those operations.
Its query engine, DataFusion, is written in [Rust](https://www.rust-lang.org/), which makes strong assumptions
about thread safety and lack of memory leaks.

There is also experimental support for executing SQL against other DataFrame libraries, such as Polars, Pandas, and any
There is also experimental support for executing SQL against other DataFrame libraries, such as Polars, Pandas, and any
drop-in replacements for Pandas.

Technically, zero-copy is achieved via the [c data interface](https://arrow.apache.org/docs/format/CDataInterface.html).
Expand Down Expand Up @@ -70,17 +70,11 @@ df = ctx.sql("select passenger_count, count(*) "
"group by passenger_count "
"order by passenger_count")

# collect as list of pyarrow.RecordBatch
results = df.collect()

# get first batch
batch = results[0]

# convert to Pandas
df = batch.to_pandas()
pandas_df = df.to_pandas()

# create a chart
fig = df.plot(kind="bar", title="Trip Count by Number of Passengers").get_figure()
fig = pandas_df.plot(kind="bar", title="Trip Count by Number of Passengers").get_figure()
fig.savefig('chart.png')
```

Expand Down
11 changes: 11 additions & 0 deletions datafusion/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,14 @@ def test_cache(df):
def test_count(df):
# Get number of rows
assert df.count() == 3


def test_to_pandas(df):
# Skip test if pandas is not installed
pd = pytest.importorskip("pandas")

# Convert datafusion dataframe to pandas dataframe
pandas_df = df.to_pandas()
assert type(pandas_df) == pd.DataFrame
assert pandas_df.shape == (3, 3)
assert set(pandas_df.columns) == {"a", "b", "c"}
10 changes: 2 additions & 8 deletions examples/sql-to-pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,11 @@
"order by passenger_count"
)

# collect as list of pyarrow.RecordBatch
results = df.collect()

# get first batch
batch = results[0]

# convert to Pandas
df = batch.to_pandas()
pandas_df = df.to_pandas()

# create a chart
fig = df.plot(
fig = pandas_df.plot(
kind="bar", title="Trip Count by Number of Passengers"
).get_figure()
fig.savefig("chart.png")
18 changes: 18 additions & 0 deletions src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,24 @@ impl PyDataFrame {
Ok(())
}

/// Convert to pandas dataframe with pyarrow
/// Collect the batches, pass to Arrow Table & then convert to Pandas DataFrame
fn to_pandas(&self, py: Python) -> PyResult<PyObject> {
let batches = self.collect(py);

Python::with_gil(|py| {
// Instantiate pyarrow Table object and use its from_batches method
let table_class = py.import("pyarrow")?.getattr("Table")?;
let args = PyTuple::new(py, batches);
let table: PyObject = table_class.call_method1("from_batches", args)?.into();

// Use Table.to_pandas() method to convert batches to pandas dataframe
// See also: https://arrow.apache.org/docs/python/generated/pyarrow.Table.html#pyarrow.Table.to_pandas
let result = table.call_method0(py, "to_pandas")?;
Ok(result)
})
}

// Executes this DataFrame to get the total number of rows.
fn count(&self, py: Python) -> PyResult<usize> {
Ok(wait_for_future(py, self.df.as_ref().clone().count())?)
Expand Down