-
-
Notifications
You must be signed in to change notification settings - Fork 747
Pandas serialization #931
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
Pandas serialization #931
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
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,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 | ||
| 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) | ||
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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| from zlib import crc32 | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
| import pandas.util.testing as tm | ||
| import pytest | ||
|
|
@@ -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)), | ||
|
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. should test various dtypes (int16, float32), datetimes, timedelta, period ( |
||
| 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(), | ||
|
|
@@ -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) | ||
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.
using
.valueswill 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 usedf[column]._valueswhich preserves the structure.