Repository navigation
WIP: Use Arrow for Pandas serialization #643
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,6 @@ | |
|
|
||
| with ignoring(ImportError): | ||
| from . import netcdf4 | ||
|
|
||
| with ignoring(ImportError): | ||
| from . import pandas | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| from __future__ import print_function, division, absolute_import | ||
|
|
||
| from .serialize import register_serialization | ||
|
|
||
| import pandas as pd | ||
| from io import BytesIO | ||
|
|
||
| import pyarrow as pa | ||
| import pyarrow.io as io | ||
| import pyarrow.ipc as ipc | ||
|
|
||
|
|
||
| def serialize_pandas_dataframe(df): | ||
| batch = pa.RecordBatch.from_pandas(df) | ||
| buf = BytesIO() | ||
| writer = ipc.ArrowFileWriter(buf, batch.schema) | ||
| writer.write_record_batch(batch) | ||
| writer.close() | ||
| frames = [buf.getvalue()] # is there a memoryview we can pass instead? | ||
| header = {} | ||
|
|
||
| return header, frames | ||
|
|
||
|
|
||
| def deserialize_pandas_dataframe(header, frames): | ||
| buf = frames[0] | ||
| reader = ipc.ArrowFileReader(buf) | ||
| return [reader.get_record_batch(i).to_pandas() | ||
| for i in range(reader.num_record_batches)][0] # what is each batch? | ||
|
|
||
|
|
||
| register_serialization(pd.DataFrame, | ||
| serialize_pandas_dataframe, | ||
| deserialize_pandas_dataframe) | ||
|
|
||
|
|
||
| def serialize_pandas_series(s): | ||
| return serialize_pandas_dataframe(s.to_frame()) # unfortunate copy here | ||
|
|
||
|
|
||
| def deserialize_pandas_series(header, frames): | ||
| df = deserialize_pandas_dataframe(header, frames) | ||
| return df[df.columns[0]] # unfortunate copy here | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not a copy :)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah indeed, my apologies. |
||
|
|
||
|
|
||
| register_serialization(pd.Series, | ||
| serialize_pandas_series, | ||
| deserialize_pandas_series) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from __future__ import print_function, division, absolute_import | ||
|
|
||
| import pytest | ||
|
|
||
| pd = pytest.importorskip('pandas') | ||
|
|
||
| from dask.dataframe.utils import assert_eq | ||
| from distributed.protocol import serialize, deserialize | ||
|
|
||
|
|
||
| examples = [] | ||
|
|
||
| df = pd.DataFrame({'x': [1, 2, 3], | ||
| 'y': ['a', 'b', 'c'], | ||
| 'z': ['a', 'a', 'b'], | ||
| 'w': [1.0, 2.0, 3.0]}) | ||
| examples.append(df) | ||
|
|
||
| for col in df.columns: | ||
| examples.append(df[col]) | ||
|
|
||
|
|
||
| examples.append(pytest.mark.xfail(df.z.astype('category'))) | ||
|
|
||
| df = pd.DataFrame({}, index=[10, 20, 30]) | ||
| examples.append(pytest.mark.xfail(df)) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('df', examples) | ||
| def test_simple(df): | ||
| header, frames = serialize(df) | ||
| assert header['type'] | ||
| df2 = deserialize(header, frames) | ||
| assert_eq(df, df2) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is also not s copy iirc (if i am not remembering correctly it's easy to make this not a copy)