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
4 changes: 4 additions & 0 deletions distributed/protocol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
def _register_numpy():
from . import numpy

@partial(register_serialization_lazy, "pandas")
def _register_pandas():
from . import pandas

@partial(register_serialization_lazy, "h5py")
def _register_h5py():
from . import h5py
Expand Down
100 changes: 100 additions & 0 deletions distributed/protocol/pandas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from __future__ import print_function, division, absolute_import

import pickle
import sys

import pandas as pd

from .serialize import register_serialization, serialize, deserialize


def serialize_pandas_dataframe(df):
head = pickle.dumps(df.head(0))
headers = []
framess = []
compression = [None]
lengths = [len(head)]
for column in df.columns:
x = df[column].values
Copy link
Contributor

Choose a reason for hiding this comment

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

using .values will cause coercion to numpy datatypes for most things (except categoricals), IOW, datetimes w/tz are converted to UTC (and thus this is not idempotent). Better to use df[column]._values which preserves the structure.

header, frames = serialize(x)
headers.append(header)
framess.append(frames)
compression.extend(header.pop('compression', [None] * len(frames)))
lengths.extend(header.pop('lengths', [len(f) for f in frames]))


# TODO: avoid if trivial index
x = df.index.values
index_header, index_frames = serialize(x)
compression.extend(index_header.pop('compression',
[None] * len(index_frames)))
lengths.extend(index_header.pop('lengths', [len(f) for f in index_frames]))

framess.append(index_frames)

header = {'frame-counts': [len(f) for f in framess],
'headers': headers,
'index-header': index_header,
'lengths': lengths}

if any(compression):
header['compression'] = compression

return header, [head] + sum(framess, [])


def deserialize_pandas_dataframe(header, frames):
head = pickle.loads(frames[0])
n = 1
d = {}
for column, h, count in zip(head.columns, header['headers'], header['frame-counts']):
x = deserialize(h, frames[n:n + count])
n += count
d[column] = x

index = deserialize(header['index-header'], frames[n:])

df = pd.DataFrame(d, columns=head.columns, index=index)
df.index.name = head.index.name
return df


def serialize_pandas_series(s):
value_header, value_frames = serialize(s.values)
index_header, index_frames = serialize(s.index)

compression = []
lengths = []
for h, f in [(value_header, value_frames), (index_header, index_frames)]:
compression.extend(h.pop('compression', [None] * len(f)))
lengths.extend(h.pop('lengths', [len(ff) for ff in f]))

header = {'name': s.name,
'value-header': value_header,
'index-header': index_header,
'n_value_frames': len(value_frames),
'index-name': s.index.name,
'lengths': lengths}

if any(compression):
header['compression'] = compression

return header, value_frames + index_frames


def deserialize_pandas_series(header, frames):
values = deserialize(header['value-header'], frames[:header['n_value_frames']])
index = deserialize(header['index-header'], frames[header['n_value_frames']:])

return pd.Series(values,
name=header['name'],
index=pd.Index(index, name=header['index-name']))


register_serialization(pd.DataFrame,
serialize_pandas_dataframe,
deserialize_pandas_dataframe)

register_serialization(pd.Series,
serialize_pandas_series,
deserialize_pandas_series)
28 changes: 25 additions & 3 deletions distributed/protocol/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from zlib import crc32

import numpy as np
import pandas as pd
import pandas.util.testing as tm
import pytest
Expand Down Expand Up @@ -32,13 +33,16 @@
pd.DataFrame({'x': [b'a', b'b', b'c']}),
pd.DataFrame({'x': pd.Categorical(['a', 'b', 'a'], ordered=True)}),
pd.DataFrame({'x': pd.Categorical(['a', 'b', 'a'], ordered=False)}),
pd.Series(np.arange(10000000)),
Copy link
Contributor

Choose a reason for hiding this comment

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

should test various dtypes (int16, float32), datetimes, timedelta, period (pd.period_range), datetime w/tz (pd.date_range(...., tz='US/Eastern')

pd.DataFrame({'x': np.arange(10000000)}),
tm.makeCategoricalIndex(),
tm.makeCustomDataframe(5, 3),
tm.makeDataFrame(),
tm.makeDateIndex(),
tm.makeMissingDataframe(),
tm.makeMixedDataFrame(),
tm.makeObjectSeries(),
pytest.mark.xfail(tm.makeObjectSeries(),
reason='date to timestamp conversion'),
tm.makePeriodFrame(),
tm.makeRangeIndex(),
tm.makeTimeDataFrame(),
Expand All @@ -48,10 +52,28 @@


@pytest.mark.parametrize('df', dfs)
def test_dumps_serialize_numpy(df):
def test_serialize_pandas(df):
header, frames = serialize(df)
if 'compression' in header:
frames = decompress(header, frames)
df2 = deserialize(header, frames)

assert_eq(df, df2)
if isinstance(df, pd.DataFrame):
tm.assert_frame_equal(df, df2)
elif isinstance(df, pd.Series):
tm.assert_series_equal(df, df2)
else:
assert_eq(df, df2)


@pytest.mark.parametrize('df', dfs)
def test_dumps_pandas(df):
frames = dumps({'x': to_serialize(df)})
df2 = loads(frames)['x']

if isinstance(df, pd.DataFrame):
tm.assert_frame_equal(df, df2)
elif isinstance(df, pd.Series):
tm.assert_series_equal(df, df2)
else:
assert_eq(df, df2)