45
45
46
46
import numpy as np
47
47
from pandas import compat
48
- from pandas .compat import u
48
+ from pandas .compat import u , PY3
49
49
from pandas import (
50
50
Timestamp , Period , Series , DataFrame , Panel , Panel4D ,
51
51
Index , MultiIndex , Int64Index , PeriodIndex , DatetimeIndex , Float64Index , NaT
@@ -168,6 +168,10 @@ def convert(values):
168
168
values = values .view ('i8' )
169
169
v = values .ravel ()
170
170
171
+ # convert object
172
+ if dtype == np .object_ :
173
+ return v .tolist ()
174
+
171
175
if compressor == 'zlib' :
172
176
173
177
# return string arrays like they are
@@ -189,12 +193,7 @@ def convert(values):
189
193
return blosc .compress (v , typesize = dtype .itemsize )
190
194
191
195
# ndarray (on original dtype)
192
- if dtype == 'float64' or dtype == 'int64' :
193
- return v
194
-
195
- # as a list
196
- return v .tolist ()
197
-
196
+ return v .tostring ()
198
197
199
198
def unconvert (values , dtype , compress = None ):
200
199
@@ -216,9 +215,8 @@ def unconvert(values, dtype, compress=None):
216
215
217
216
return np .frombuffer (values , dtype = dtype )
218
217
219
- # as a list
220
- return np .array (values , dtype = dtype )
221
-
218
+ # from a string
219
+ return np .fromstring (values .encode ('latin1' ),dtype = dtype )
222
220
223
221
def encode (obj ):
224
222
"""
@@ -253,19 +251,20 @@ def encode(obj):
253
251
'klass' : obj .__class__ .__name__ ,
254
252
'name' : getattr (obj , 'name' , None ),
255
253
'dtype' : obj .dtype .num ,
256
- 'data' : obj .tolist ( )}
254
+ 'data' : convert ( obj .values )}
257
255
elif isinstance (obj , Series ):
258
256
if isinstance (obj , SparseSeries ):
259
- d = {'typ' : 'sparse_series' ,
260
- 'klass' : obj .__class__ .__name__ ,
261
- 'dtype' : obj .dtype .num ,
262
- 'index' : obj .index ,
263
- 'sp_index' : obj .sp_index ,
264
- 'sp_values' : convert (obj .sp_values ),
265
- 'compress' : compressor }
266
- for f in ['name' , 'fill_value' , 'kind' ]:
267
- d [f ] = getattr (obj , f , None )
268
- return d
257
+ raise NotImplementedError ("msgpack sparse series is not implemented" )
258
+ #d = {'typ': 'sparse_series',
259
+ # 'klass': obj.__class__.__name__,
260
+ # 'dtype': obj.dtype.num,
261
+ # 'index': obj.index,
262
+ # 'sp_index': obj.sp_index,
263
+ # 'sp_values': convert(obj.sp_values),
264
+ # 'compress': compressor}
265
+ #for f in ['name', 'fill_value', 'kind']:
266
+ # d[f] = getattr(obj, f, None)
267
+ #return d
269
268
else :
270
269
return {'typ' : 'series' ,
271
270
'klass' : obj .__class__ .__name__ ,
@@ -276,23 +275,25 @@ def encode(obj):
276
275
'compress' : compressor }
277
276
elif issubclass (tobj , NDFrame ):
278
277
if isinstance (obj , SparseDataFrame ):
279
- d = {'typ' : 'sparse_dataframe' ,
280
- 'klass' : obj .__class__ .__name__ ,
281
- 'columns' : obj .columns }
282
- for f in ['default_fill_value' , 'default_kind' ]:
283
- d [f ] = getattr (obj , f , None )
284
- d ['data' ] = dict ([(name , ss )
285
- for name , ss in compat .iteritems (obj )])
286
- return d
278
+ raise NotImplementedError ("msgpack sparse frame is not implemented" )
279
+ #d = {'typ': 'sparse_dataframe',
280
+ # 'klass': obj.__class__.__name__,
281
+ # 'columns': obj.columns}
282
+ #for f in ['default_fill_value', 'default_kind']:
283
+ # d[f] = getattr(obj, f, None)
284
+ #d['data'] = dict([(name, ss)
285
+ # for name, ss in compat.iteritems(obj)])
286
+ #return d
287
287
elif isinstance (obj , SparsePanel ):
288
- d = {'typ' : 'sparse_panel' ,
289
- 'klass' : obj .__class__ .__name__ ,
290
- 'items' : obj .items }
291
- for f in ['default_fill_value' , 'default_kind' ]:
292
- d [f ] = getattr (obj , f , None )
293
- d ['data' ] = dict ([(name , df )
294
- for name , df in compat .iteritems (obj )])
295
- return d
288
+ raise NotImplementedError ("msgpack sparse frame is not implemented" )
289
+ #d = {'typ': 'sparse_panel',
290
+ # 'klass': obj.__class__.__name__,
291
+ # 'items': obj.items}
292
+ #for f in ['default_fill_value', 'default_kind']:
293
+ # d[f] = getattr(obj, f, None)
294
+ #d['data'] = dict([(name, df)
295
+ # for name, df in compat.iteritems(obj)])
296
+ #return d
296
297
else :
297
298
298
299
data = obj ._data
@@ -354,7 +355,7 @@ def encode(obj):
354
355
'klass' : obj .__class__ .__name__ ,
355
356
'indices' : obj .indices ,
356
357
'length' : obj .length }
357
- elif isinstance (obj , np .ndarray ) and obj . dtype not in [ 'float64' , 'int64' ] :
358
+ elif isinstance (obj , np .ndarray ):
358
359
return {'typ' : 'ndarray' ,
359
360
'shape' : obj .shape ,
360
361
'ndim' : obj .ndim ,
@@ -394,14 +395,18 @@ def decode(obj):
394
395
return Period (ordinal = obj ['ordinal' ], freq = obj ['freq' ])
395
396
elif typ == 'index' :
396
397
dtype = dtype_for (obj ['dtype' ])
397
- data = obj ['data' ]
398
+ data = unconvert ( obj ['data' ], np . typeDict [ obj [ 'dtype' ]], obj . get ( 'compress' ))
398
399
return globals ()[obj ['klass' ]](data , dtype = dtype , name = obj ['name' ])
399
400
elif typ == 'multi_index' :
400
- return globals ()[obj ['klass' ]].from_tuples (obj ['data' ], names = obj ['names' ])
401
+ data = unconvert (obj ['data' ], np .typeDict [obj ['dtype' ]], obj .get ('compress' ))
402
+ data = [ tuple (x ) for x in data ]
403
+ return globals ()[obj ['klass' ]].from_tuples (data , names = obj ['names' ])
401
404
elif typ == 'period_index' :
402
- return globals ()[obj ['klass' ]](obj ['data' ], name = obj ['name' ], freq = obj ['freq' ])
405
+ data = unconvert (obj ['data' ], np .int64 , obj .get ('compress' ))
406
+ return globals ()[obj ['klass' ]](data , name = obj ['name' ], freq = obj ['freq' ])
403
407
elif typ == 'datetime_index' :
404
- return globals ()[obj ['klass' ]](obj ['data' ], freq = obj ['freq' ], tz = obj ['tz' ], name = obj ['name' ])
408
+ data = unconvert (obj ['data' ], np .int64 , obj .get ('compress' ))
409
+ return globals ()[obj ['klass' ]](data , freq = obj ['freq' ], tz = obj ['tz' ], name = obj ['name' ])
405
410
elif typ == 'series' :
406
411
dtype = dtype_for (obj ['dtype' ])
407
412
index = obj ['index' ]
@@ -425,17 +430,17 @@ def create_block(b):
425
430
return timedelta (* obj ['data' ])
426
431
elif typ == 'timedelta64' :
427
432
return np .timedelta64 (int (obj ['data' ]))
428
- elif typ == 'sparse_series' :
429
- dtype = dtype_for (obj ['dtype' ])
430
- return globals (
431
- )[obj ['klass' ]](unconvert (obj ['sp_values' ], dtype , obj ['compress' ]), sparse_index = obj ['sp_index' ],
432
- index = obj ['index' ], fill_value = obj ['fill_value' ], kind = obj ['kind' ], name = obj ['name' ])
433
- elif typ == 'sparse_dataframe' :
434
- return globals ()[obj ['klass' ]](obj ['data' ],
435
- columns = obj ['columns' ], default_fill_value = obj ['default_fill_value' ], default_kind = obj ['default_kind' ])
436
- elif typ == 'sparse_panel' :
437
- return globals ()[obj ['klass' ]](obj ['data' ],
438
- items = obj ['items' ], default_fill_value = obj ['default_fill_value' ], default_kind = obj ['default_kind' ])
433
+ # elif typ == 'sparse_series':
434
+ # dtype = dtype_for(obj['dtype'])
435
+ # return globals(
436
+ # )[obj['klass']](unconvert(obj['sp_values'], dtype, obj['compress']), sparse_index=obj['sp_index'],
437
+ # index=obj['index'], fill_value=obj['fill_value'], kind=obj['kind'], name=obj['name'])
438
+ # elif typ == 'sparse_dataframe':
439
+ # return globals()[obj['klass']](obj['data'],
440
+ # columns=obj['columns'], default_fill_value=obj['default_fill_value'], default_kind=obj['default_kind'])
441
+ # elif typ == 'sparse_panel':
442
+ # return globals()[obj['klass']](obj['data'],
443
+ # items=obj['items'], default_fill_value=obj['default_fill_value'], default_kind=obj['default_kind'])
439
444
elif typ == 'block_index' :
440
445
return globals ()[obj ['klass' ]](obj ['length' ], obj ['blocs' ], obj ['blengths' ])
441
446
elif typ == 'int_index' :
@@ -460,7 +465,7 @@ def create_block(b):
460
465
461
466
462
467
def pack (o , default = encode ,
463
- encoding = 'utf-8 ' , unicode_errors = 'strict' , use_single_float = False ):
468
+ encoding = 'latin1 ' , unicode_errors = 'strict' , use_single_float = False ):
464
469
"""
465
470
Pack an object and return the packed bytes.
466
471
"""
@@ -471,7 +476,7 @@ def pack(o, default=encode,
471
476
472
477
473
478
def unpack (packed , object_hook = decode ,
474
- list_hook = None , use_list = False , encoding = 'utf-8 ' ,
479
+ list_hook = None , use_list = False , encoding = 'latin1 ' ,
475
480
unicode_errors = 'strict' , object_pairs_hook = None ):
476
481
"""
477
482
Unpack a packed object, return an iterator
@@ -488,7 +493,7 @@ def unpack(packed, object_hook=decode,
488
493
class Packer (_Packer ):
489
494
490
495
def __init__ (self , default = encode ,
491
- encoding = 'utf-8 ' ,
496
+ encoding = 'latin1 ' ,
492
497
unicode_errors = 'strict' ,
493
498
use_single_float = False ):
494
499
super (Packer , self ).__init__ (default = default ,
@@ -501,7 +506,7 @@ class Unpacker(_Unpacker):
501
506
502
507
def __init__ (self , file_like = None , read_size = 0 , use_list = False ,
503
508
object_hook = decode ,
504
- object_pairs_hook = None , list_hook = None , encoding = 'utf-8 ' ,
509
+ object_pairs_hook = None , list_hook = None , encoding = 'latin1 ' ,
505
510
unicode_errors = 'strict' , max_buffer_size = 0 ):
506
511
super (Unpacker , self ).__init__ (file_like = file_like ,
507
512
read_size = read_size ,
0 commit comments