49
49
from dateutil .parser import parse
50
50
51
51
import numpy as np
52
+ from pandas import compat
53
+ from pandas .compat import u
52
54
from pandas import (
53
55
Timestamp , Period , Series , TimeSeries , DataFrame , Panel , Panel4D ,
54
- Index , MultiIndex , Int64Index , PeriodIndex , DatetimeIndex , NaT
56
+ Index , MultiIndex , Int64Index , PeriodIndex , DatetimeIndex , Float64Index , NaT
55
57
)
56
58
from pandas .sparse .api import SparseSeries , SparseDataFrame , SparsePanel
57
59
from pandas .sparse .array import BlockIndex , IntIndex
@@ -80,12 +82,13 @@ def to_msgpack(path, *args, **kwargs):
80
82
"""
81
83
msgpack (serialize) object to input file path
82
84
85
+ THIS IS AN EXPERIMENTAL LIBRARY and the storage format
86
+ may not be stable until a future release.
87
+
83
88
Parameters
84
89
----------
85
- path : string
86
- File path
90
+ path : string File path
87
91
args : an object or objects to serialize
88
-
89
92
append : boolean whether to append to an existing msgpack
90
93
(default is False)
91
94
compress : type of compressor (zlib or blosc), default to None (no compression)
@@ -112,6 +115,9 @@ def read_msgpack(path, iterator=False, **kwargs):
112
115
Load msgpack pandas object from the specified
113
116
file path
114
117
118
+ THIS IS AN EXPERIMENTAL LIBRARY and the storage format
119
+ may not be stable until a future release.
120
+
115
121
Parameters
116
122
----------
117
123
path : string
@@ -134,11 +140,11 @@ def read_msgpack(path, iterator=False, **kwargs):
134
140
return l
135
141
136
142
dtype_dict = { 21 : np .dtype ('M8[ns]' ),
137
- u'datetime64[ns]' : np .dtype ('M8[ns]' ),
138
- u'datetime64[us]' : np .dtype ('M8[us]' ),
143
+ u ( 'datetime64[ns]' ) : np .dtype ('M8[ns]' ),
144
+ u ( 'datetime64[us]' ) : np .dtype ('M8[us]' ),
139
145
22 : np .dtype ('m8[ns]' ),
140
- u'timedelta64[ns]' : np .dtype ('m8[ns]' ),
141
- u'timedelta64[us]' : np .dtype ('m8[us]' ) }
146
+ u ( 'timedelta64[ns]' ) : np .dtype ('m8[ns]' ),
147
+ u ( 'timedelta64[us]' ) : np .dtype ('m8[us]' ) }
142
148
143
149
def dtype_for (t ):
144
150
if t in dtype_dict :
@@ -157,7 +163,7 @@ def c2f(r, i, ctype_name):
157
163
"""
158
164
Convert strings to complex number instance with specified numpy type.
159
165
"""
160
-
166
+
161
167
ftype = c2f_dict [ctype_name ]
162
168
return np .typeDict [ctype_name ](ftype (r )+ 1j * ftype (i ))
163
169
@@ -224,7 +230,7 @@ def encode(obj):
224
230
"""
225
231
Data encoder
226
232
"""
227
-
233
+
228
234
tobj = type (obj )
229
235
if isinstance (obj , Index ):
230
236
if isinstance (obj , PeriodIndex ):
@@ -281,15 +287,15 @@ def encode(obj):
281
287
'columns' : obj .columns }
282
288
for f in ['default_fill_value' ,'default_kind' ]:
283
289
d [f ] = getattr (obj ,f ,None )
284
- d ['data' ] = dict ([ (name ,ss ) for name ,ss in obj . iterkv ( ) ])
290
+ d ['data' ] = dict ([ (name ,ss ) for name ,ss in compat . iteritems ( obj ) ])
285
291
return d
286
292
elif isinstance (obj , SparsePanel ):
287
293
d = {'typ' : 'sparse_panel' ,
288
294
'klass' : obj .__class__ .__name__ ,
289
295
'items' : obj .items }
290
296
for f in ['default_fill_value' ,'default_kind' ]:
291
297
d [f ] = getattr (obj ,f ,None )
292
- d ['data' ] = dict ([ (name ,df ) for name ,df in obj . iterkv ( ) ])
298
+ d ['data' ] = dict ([ (name ,df ) for name ,df in compat . iteritems ( obj ) ])
293
299
return d
294
300
else :
295
301
@@ -301,8 +307,8 @@ def encode(obj):
301
307
return {'typ' : 'block_manager' ,
302
308
'klass' : obj .__class__ .__name__ ,
303
309
'axes' : data .axes ,
304
- 'blocks' : [ { 'items' : b .items ,
305
- 'values' : convert (b .values ),
310
+ 'blocks' : [ { 'items' : b .items ,
311
+ 'values' : convert (b .values ),
306
312
'shape' : b .values .shape ,
307
313
'dtype' : b .dtype .num ,
308
314
'klass' : b .__class__ .__name__ ,
@@ -381,7 +387,7 @@ def decode(obj):
381
387
"""
382
388
Decoder for deserializing numpy data types.
383
389
"""
384
-
390
+
385
391
typ = obj .get ('typ' )
386
392
if typ is None :
387
393
return obj
@@ -408,7 +414,7 @@ def decode(obj):
408
414
409
415
def create_block (b ):
410
416
dtype = dtype_for (b ['dtype' ])
411
- return make_block (unconvert (b ['values' ],dtype ,b ['compress' ]).reshape (b ['shape' ]),b ['items' ],axes [0 ],klass = getattr (internals ,b ['klass' ]))
417
+ return make_block (unconvert (b ['values' ],dtype ,b ['compress' ]).reshape (b ['shape' ]),b ['items' ],axes [0 ],klass = getattr (internals ,b ['klass' ]))
412
418
413
419
blocks = [ create_block (b ) for b in obj ['blocks' ] ]
414
420
return globals ()[obj ['klass' ]](BlockManager (blocks , axes ))
@@ -454,17 +460,17 @@ def create_block(b):
454
460
else :
455
461
return obj
456
462
457
- def pack (o , default = encode ,
463
+ def pack (o , default = encode ,
458
464
encoding = 'utf-8' , unicode_errors = 'strict' , use_single_float = False ):
459
465
"""
460
466
Pack an object and return the packed bytes.
461
467
"""
462
468
463
469
return Packer (default = default , encoding = encoding ,
464
- unicode_errors = unicode_errors ,
470
+ unicode_errors = unicode_errors ,
465
471
use_single_float = use_single_float ).pack (o )
466
472
467
- def unpack (packed , object_hook = decode ,
473
+ def unpack (packed , object_hook = decode ,
468
474
list_hook = None , use_list = False , encoding = 'utf-8' ,
469
475
unicode_errors = 'strict' , object_pairs_hook = None ):
470
476
"""
@@ -473,17 +479,17 @@ def unpack(packed, object_hook=decode,
473
479
"""
474
480
475
481
return Unpacker (packed , object_hook = object_hook ,
476
- list_hook = list_hook ,
482
+ list_hook = list_hook ,
477
483
use_list = use_list , encoding = encoding ,
478
- unicode_errors = unicode_errors ,
484
+ unicode_errors = unicode_errors ,
479
485
object_pairs_hook = object_pairs_hook )
480
486
481
487
class Packer (_Packer ):
482
- def __init__ (self , default = encode ,
488
+ def __init__ (self , default = encode ,
483
489
encoding = 'utf-8' ,
484
490
unicode_errors = 'strict' ,
485
491
use_single_float = False ):
486
- super (Packer , self ).__init__ (default = default ,
492
+ super (Packer , self ).__init__ (default = default ,
487
493
encoding = encoding ,
488
494
unicode_errors = unicode_errors ,
489
495
use_single_float = use_single_float )
@@ -493,14 +499,14 @@ def __init__(self, file_like=None, read_size=0, use_list=False,
493
499
object_hook = decode ,
494
500
object_pairs_hook = None , list_hook = None , encoding = 'utf-8' ,
495
501
unicode_errors = 'strict' , max_buffer_size = 0 ):
496
- super (Unpacker , self ).__init__ (file_like = file_like ,
497
- read_size = read_size ,
498
- use_list = use_list ,
499
- object_hook = object_hook ,
500
- object_pairs_hook = object_pairs_hook ,
502
+ super (Unpacker , self ).__init__ (file_like = file_like ,
503
+ read_size = read_size ,
504
+ use_list = use_list ,
505
+ object_hook = object_hook ,
506
+ object_pairs_hook = object_pairs_hook ,
501
507
list_hook = list_hook ,
502
- encoding = encoding ,
503
- unicode_errors = unicode_errors ,
508
+ encoding = encoding ,
509
+ unicode_errors = unicode_errors ,
504
510
max_buffer_size = max_buffer_size )
505
511
506
512
class Iterator (object ):
0 commit comments