14
14
from pandas .util .testing import ensure_clean
15
15
from pandas .tests .test_series import assert_series_equal
16
16
from pandas .tests .test_frame import assert_frame_equal
17
+ from pandas .tests .test_panel import assert_panel_equal
18
+
19
+ import pandas
20
+ from pandas .sparse .tests .test_sparse import assert_sp_series_equal , assert_sp_frame_equal
17
21
from pandas import concat , Timestamp , tslib
18
22
19
23
from numpy .testing .decorators import slow
@@ -32,6 +36,8 @@ def check_arbitrary(a, b):
32
36
assert (len (a ) == len (b ))
33
37
for a_ , b_ in zip (a ,b ):
34
38
check_arbitrary (a_ ,b_ )
39
+ elif isinstance (a ,Panel ):
40
+ assert_panel_equal (a ,b )
35
41
elif isinstance (a ,DataFrame ):
36
42
assert_frame_equal (a ,b )
37
43
elif isinstance (a ,Series ):
@@ -225,10 +231,10 @@ def test_basic(self):
225
231
i_rec = self .encode_decode (i )
226
232
assert_series_equal (i ,i_rec )
227
233
228
- class TestFrame (Test ):
234
+ class TestNDFrame (Test ):
229
235
230
236
def setUp (self ):
231
- super (TestFrame , self ).setUp ()
237
+ super (TestNDFrame , self ).setUp ()
232
238
233
239
data = {
234
240
'A' : [0. , 1. , 2. , 3. , np .nan ],
@@ -238,98 +244,107 @@ def setUp(self):
238
244
'E' : [0. , 1 , Timestamp ('20100101' ),'foo' ,2. ],
239
245
}
240
246
241
- self .d = { 'float' : DataFrame (dict (A = data ['A' ], B = Series (data ['A' ]) + 1 )),
242
- 'int' : DataFrame (dict (A = data ['B' ], B = Series (data ['B' ]) + 1 )),
243
- 'mixed' : DataFrame (dict ([ (k ,data [k ]) for k in ['A' ,'B' ,'C' ,'D' ]])) }
247
+ self .frame = { 'float' : DataFrame (dict (A = data ['A' ], B = Series (data ['A' ]) + 1 )),
248
+ 'int' : DataFrame (dict (A = data ['B' ], B = Series (data ['B' ]) + 1 )),
249
+ 'mixed' : DataFrame (dict ([ (k ,data [k ]) for k in ['A' ,'B' ,'C' ,'D' ]])) }
250
+
251
+ self .panel = { 'float' : Panel (dict (ItemA = self .frame ['float' ], ItemB = self .frame ['float' ]+ 1 )) }
244
252
245
- def test_basic (self ):
253
+ def test_basic_frame (self ):
246
254
247
- for s , i in self .d .items ():
255
+ for s , i in self .frame .items ():
248
256
i_rec = self .encode_decode (i )
249
257
assert_frame_equal (i ,i_rec )
250
258
259
+ def test_basic_panel (self ):
260
+
261
+ for s , i in self .panel .items ():
262
+ i_rec = self .encode_decode (i )
263
+ assert_panel_equal (i ,i_rec )
264
+
251
265
def test_multi (self ):
252
266
253
- i_rec = self .encode_decode (self .d )
254
- for k in self .d .keys ():
255
- assert_frame_equal (self .d [k ],i_rec [k ])
267
+ i_rec = self .encode_decode (self .frame )
268
+ for k in self .frame .keys ():
269
+ assert_frame_equal (self .frame [k ],i_rec [k ])
256
270
257
- l = tuple ([ self .d ['float' ], self .d ['float' ].A , self .d ['float' ].B , None ])
271
+ l = tuple ([ self .frame ['float' ], self .frame ['float' ].A , self .frame ['float' ].B , None ])
258
272
l_rec = self .encode_decode (l )
259
273
check_arbitrary (l ,l_rec )
260
274
261
275
# this is an oddity in that packed lists will be returned as tuples
262
- l = [ self .d ['float' ], self .d ['float' ].A , self .d ['float' ].B , None ]
276
+ l = [ self .frame ['float' ], self .frame ['float' ].A , self .frame ['float' ].B , None ]
263
277
l_rec = self .encode_decode (l )
264
278
self .assert_ (isinstance (l_rec ,tuple ))
265
279
check_arbitrary (l ,l_rec )
266
280
267
281
def test_iterator (self ):
268
282
269
- l = [ self .d ['float' ], self .d ['float' ].A , self .d ['float' ].B , None ]
283
+ l = [ self .frame ['float' ], self .frame ['float' ].A , self .frame ['float' ].B , None ]
270
284
271
285
with ensure_clean (self .path ) as path :
272
286
to_msgpack (path ,* l )
273
287
for i , packed in enumerate (read_msgpack (path , iterator = True )):
274
288
check_arbitrary (packed ,l [i ])
275
289
276
- def _create_sp_series ( ):
290
+ class TestSparse ( Test ):
277
291
278
- # nan-based
279
- arr = np .arange (15 , dtype = float )
280
- index = np .arange (15 )
281
- arr [7 :12 ] = nan
282
- arr [- 1 :] = nan
292
+ def _check_roundtrip (self , obj , comparator , ** kwargs ):
283
293
284
- date_index = bdate_range ('1/1/2011' , periods = len (index ))
285
- bseries = SparseSeries (arr , index = index , kind = 'block' )
286
- bseries .name = 'bseries'
287
- return bseries
294
+ i_rec = self .encode_decode (obj )
295
+ comparator (obj ,i_rec ,** kwargs )
288
296
289
- def _create_sp_frame ( ):
297
+ def test_sparse_series ( self ):
290
298
291
- data = {'A' : [nan , nan , nan , 0 , 1 , 2 , 3 , 4 , 5 , 6 ],
292
- 'B' : [0 , 1 , 2 , nan , nan , nan , 3 , 4 , 5 , 6 ],
293
- 'C' : np .arange (10 ),
294
- 'D' : [0 , 1 , 2 , 3 , 4 , 5 , nan , nan , nan , nan ]}
295
-
296
- dates = bdate_range ('1/1/2011' , periods = 10 )
297
- return SparseDataFrame (data , index = dates )
299
+ s = tm .makeStringSeries ()
300
+ s [3 :5 ] = np .nan
301
+ ss = s .to_sparse ()
302
+ self ._check_roundtrip (ss , tm .assert_series_equal ,
303
+ check_series_type = True )
304
+
305
+ ss2 = s .to_sparse (kind = 'integer' )
306
+ self ._check_roundtrip (ss2 , tm .assert_series_equal ,
307
+ check_series_type = True )
308
+
309
+ ss3 = s .to_sparse (fill_value = 0 )
310
+ self ._check_roundtrip (ss3 , tm .assert_series_equal ,
311
+ check_series_type = True )
312
+
313
+ def test_sparse_frame (self ):
314
+
315
+ s = tm .makeDataFrame ()
316
+ s .ix [3 :5 , 1 :3 ] = np .nan
317
+ s .ix [8 :10 , - 2 ] = np .nan
318
+ ss = s .to_sparse ()
319
+
320
+ self ._check_roundtrip (ss , tm .assert_frame_equal ,
321
+ check_frame_type = True )
322
+
323
+ ss2 = s .to_sparse (kind = 'integer' )
324
+ self ._check_roundtrip (ss2 , tm .assert_frame_equal ,
325
+ check_frame_type = True )
326
+
327
+ ss3 = s .to_sparse (fill_value = 0 )
328
+ self ._check_roundtrip (ss3 , tm .assert_frame_equal ,
329
+ check_frame_type = True )
330
+
331
+ def test_sparse_panel (self ):
332
+
333
+ items = ['x' , 'y' , 'z' ]
334
+ p = Panel (dict ((i , tm .makeDataFrame ().ix [:2 , :2 ]) for i in items ))
335
+ sp = p .to_sparse ()
336
+
337
+ self ._check_roundtrip (sp , tm .assert_panel_equal ,
338
+ check_panel_type = True )
339
+
340
+ sp2 = p .to_sparse (kind = 'integer' )
341
+ self ._check_roundtrip (sp2 , tm .assert_panel_equal ,
342
+ check_panel_type = True )
343
+
344
+ sp3 = p .to_sparse (fill_value = 0 )
345
+ self ._check_roundtrip (sp3 , tm .assert_panel_equal ,
346
+ check_panel_type = True )
298
347
299
- def create_data ():
300
- """ create the pickle data """
301
-
302
- data = {
303
- 'A' : [0. , 1. , 2. , 3. , np .nan ],
304
- 'B' : [0 , 1 , 0 , 1 , 0 ],
305
- 'C' : ['foo1' , 'foo2' , 'foo3' , 'foo4' , 'foo5' ],
306
- 'D' : date_range ('1/1/2009' , periods = 5 ),
307
- 'E' : [0. , 1 , Timestamp ('20100101' ),'foo' ,2. ],
308
- }
309
-
310
- index = dict (int = Index (np .arange (10 )),
311
- date = date_range ('20130101' ,periods = 10 ))
312
- mi = dict (reg = MultiIndex .from_tuples (zip ([['bar' , 'bar' , 'baz' , 'baz' , 'foo' , 'foo' , 'qux' , 'qux' ],
313
- ['one' , 'two' , 'one' , 'two' , 'one' , 'two' , 'one' , 'two' ]]),
314
- names = ['first' , 'second' ]))
315
- series = dict (float = Series (data ['A' ]),
316
- int = Series (data ['B' ]),
317
- mixed = Series (data ['E' ]))
318
- frame = dict (float = DataFrame (dict (A = series ['float' ], B = series ['float' ] + 1 )),
319
- int = DataFrame (dict (A = series ['int' ] , B = series ['int' ] + 1 )),
320
- mixed = DataFrame (dict ([ (k ,data [k ]) for k in ['A' ,'B' ,'C' ,'D' ]])))
321
- panel = dict (float = Panel (dict (ItemA = frame ['float' ], ItemB = frame ['float' ]+ 1 )))
322
-
323
-
324
-
325
- return dict ( series = series ,
326
- frame = frame ,
327
- panel = panel ,
328
- index = index ,
329
- mi = mi ,
330
- sp_series = dict (float = _create_sp_series ()),
331
- sp_frame = dict (float = _create_sp_frame ())
332
- )
333
348
334
349
if __name__ == '__main__' :
335
350
import nose
0 commit comments