Skip to content

Commit f727477

Browse files
committed
pass a dict of {'engine': engine_string, 'data': data dict}
1 parent d4ba982 commit f727477

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

pandas/io/ipc.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
""" ipc format compat """
22

33
from pandas.types.generic import ABCIndexClass, ABCSeries, ABCDataFrame
4-
from pandas.compat import BytesIO, string_types
4+
from pandas.compat import string_types, cPickle
55
from pandas._libs.lib import is_string_array, is_unicode_array
66
from pandas.types.common import is_object_dtype
77

@@ -82,18 +82,18 @@ def to_ipc(obj, engine='infer'):
8282
def _to_pyarrow(df):
8383
""" helper routine to return via pyarrow """
8484
pyarrow = _try_import()
85-
return pyarrow.write_ipc(df)
85+
d = pyarrow.write_ipc(df)
86+
d['engine'] = 'pyarrow'
87+
return d
8688

8789

8890
def _to_pickle(obj):
8991
""" helper routine to return a pickle of an object """
90-
from pandas import to_pickle
91-
db = BytesIO()
92-
to_pickle(obj, db)
93-
return db.getvalue()
92+
d = {'engine': 'pickle', 'data': cPickle.dumps(obj)}
93+
return d
9494

9595

96-
def read_ipc(db, engine='infer'):
96+
def read_ipc(db):
9797
"""
9898
Load a pyarrow ipc format object from the file dict-of-bytes
9999
@@ -102,21 +102,20 @@ def read_ipc(db, engine='infer'):
102102
Parameters
103103
----------
104104
dict-of-meta-and-bytes : a dictionary of meta data & bytes
105-
engine : string, optional
106-
string to indicate the engine {'infer', 'pickle', 'pyarrow'}
107-
'infer' will pick an engine based upon performance considerations
108105
109106
Returns
110107
-------
111-
DataFrame
108+
Pandas Object
112109
113110
"""
111+
engine = db['engine']
112+
114113
if engine == 'pickle':
115-
return _read_pickle(db)
114+
return _read_pickle(db['data'])
116115
try:
117-
return _read_pyarrow(db)
116+
return _read_pyarrow(db['data'])
118117
except: # pragma
119-
return _read_pickle(db)
118+
return _read_pickle(db['data'])
120119

121120

122121
def _read_pyarrow(db):
@@ -127,7 +126,4 @@ def _read_pyarrow(db):
127126

128127
def _read_pickle(db):
129128
""" helper to return via pickle """
130-
from pandas import read_pickle
131-
132-
db = BytesIO(db)
133-
return read_pickle(db)
129+
return cPickle.loads(db)

pandas/tests/io/test_ipc.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ def check_round_trip(self, df, engine=None):
5757
if engine is None:
5858
writer = to_ipc
5959
reader = read_ipc
60+
b = writer(df)
6061
else:
6162
_, writer, reader = engine
63+
b = writer(df)
64+
65+
# we are calling a lower-level routine
66+
b = b['data']
6267

63-
b = writer(df)
6468
result = reader(b)
6569
tm.assert_frame_equal(result, df)
6670

0 commit comments

Comments
 (0)