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
3 changes: 3 additions & 0 deletions distributed/protocol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@

with ignoring(ImportError):
from . import netcdf4

with ignoring(ImportError):
from . import pandas
48 changes: 48 additions & 0 deletions distributed/protocol/pandas.py
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

Copy link
Copy Markdown
Contributor

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)



def deserialize_pandas_series(header, frames):
df = deserialize_pandas_dataframe(header, frames)
return df[df.columns[0]] # unfortunate copy here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a copy :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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)
34 changes: 34 additions & 0 deletions distributed/protocol/tests/test_pandas.py
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)