@@ -32,6 +32,14 @@ def test_getitem(self):
32
32
exp = orig [orig % 2 == 1 ].to_sparse ()
33
33
tm .assert_sp_series_equal (result , exp )
34
34
35
+ def test_getitem_slice (self ):
36
+ orig = pd .Series ([1 , np .nan , np .nan , 3 , np .nan ])
37
+ sparse = orig .to_sparse ()
38
+ tm .assert_sp_series_equal (sparse [:2 ], orig [:2 ].to_sparse ())
39
+ tm .assert_sp_series_equal (sparse [4 :2 ], orig [4 :2 ].to_sparse ())
40
+ tm .assert_sp_series_equal (sparse [::2 ], orig [::2 ].to_sparse ())
41
+ tm .assert_sp_series_equal (sparse [- 5 :], orig [- 5 :].to_sparse ())
42
+
35
43
def test_getitem_fill_value (self ):
36
44
orig = pd .Series ([1 , np .nan , 0 , 3 , 0 ])
37
45
sparse = orig .to_sparse (fill_value = 0 )
@@ -63,6 +71,18 @@ def test_getitem_ellipsis(self):
63
71
s = pd .SparseSeries ([1 , np .nan , 2 , 0 , np .nan ], fill_value = 0 )
64
72
tm .assert_sp_series_equal (s [...], s )
65
73
74
+ def test_getitem_slice_fill_value (self ):
75
+ orig = pd .Series ([1 , np .nan , 0 , 3 , 0 ])
76
+ sparse = orig .to_sparse (fill_value = 0 )
77
+ tm .assert_sp_series_equal (sparse [:2 ],
78
+ orig [:2 ].to_sparse (fill_value = 0 ))
79
+ tm .assert_sp_series_equal (sparse [4 :2 ],
80
+ orig [4 :2 ].to_sparse (fill_value = 0 ))
81
+ tm .assert_sp_series_equal (sparse [::2 ],
82
+ orig [::2 ].to_sparse (fill_value = 0 ))
83
+ tm .assert_sp_series_equal (sparse [- 5 :],
84
+ orig [- 5 :].to_sparse (fill_value = 0 ))
85
+
66
86
def test_loc (self ):
67
87
orig = pd .Series ([1 , np .nan , np .nan , 3 , np .nan ])
68
88
sparse = orig .to_sparse ()
@@ -237,6 +257,25 @@ def test_iat_fill_value(self):
237
257
self .assertEqual (sparse .iat [- 1 ], orig .iat [- 1 ])
238
258
self .assertEqual (sparse .iat [- 5 ], orig .iat [- 5 ])
239
259
260
+ def test_get (self ):
261
+ s = pd .SparseSeries ([1 , np .nan , np .nan , 3 , np .nan ])
262
+ self .assertEqual (s .get (0 ), 1 )
263
+ self .assertTrue (np .isnan (s .get (1 )))
264
+ self .assertIsNone (s .get (5 ))
265
+
266
+ s = pd .SparseSeries ([1 , np .nan , 0 , 3 , 0 ], index = list ('ABCDE' ))
267
+ self .assertEqual (s .get ('A' ), 1 )
268
+ self .assertTrue (np .isnan (s .get ('B' )))
269
+ self .assertEqual (s .get ('C' ), 0 )
270
+ self .assertIsNone (s .get ('XX' ))
271
+
272
+ s = pd .SparseSeries ([1 , np .nan , 0 , 3 , 0 ], index = list ('ABCDE' ),
273
+ fill_value = 0 )
274
+ self .assertEqual (s .get ('A' ), 1 )
275
+ self .assertTrue (np .isnan (s .get ('B' )))
276
+ self .assertEqual (s .get ('C' ), 0 )
277
+ self .assertIsNone (s .get ('XX' ))
278
+
240
279
def test_take (self ):
241
280
orig = pd .Series ([1 , np .nan , np .nan , 3 , np .nan ],
242
281
index = list ('ABCDE' ))
@@ -321,6 +360,53 @@ class TestSparseDataFrameIndexing(tm.TestCase):
321
360
322
361
_multiprocess_can_split_ = True
323
362
363
+ def test_getitem (self ):
364
+ orig = pd .DataFrame ([[1 , np .nan , np .nan ],
365
+ [2 , 3 , np .nan ],
366
+ [np .nan , np .nan , 4 ],
367
+ [0 , np .nan , 5 ]],
368
+ columns = list ('xyz' ))
369
+ sparse = orig .to_sparse ()
370
+
371
+ tm .assert_sp_series_equal (sparse ['x' ], orig ['x' ].to_sparse ())
372
+ tm .assert_sp_frame_equal (sparse [['x' ]], orig [['x' ]].to_sparse ())
373
+ tm .assert_sp_frame_equal (sparse [['z' , 'x' ]],
374
+ orig [['z' , 'x' ]].to_sparse ())
375
+
376
+ tm .assert_sp_frame_equal (sparse [[True , False , True , True ]],
377
+ orig [[True , False , True , True ]].to_sparse ())
378
+
379
+ tm .assert_sp_frame_equal (sparse [[1 , 2 ]],
380
+ orig [[1 , 2 ]].to_sparse ())
381
+
382
+ def test_getitem_fill_value (self ):
383
+ orig = pd .DataFrame ([[1 , np .nan , 0 ],
384
+ [2 , 3 , np .nan ],
385
+ [0 , np .nan , 4 ],
386
+ [0 , np .nan , 5 ]],
387
+ columns = list ('xyz' ))
388
+ sparse = orig .to_sparse (fill_value = 0 )
389
+
390
+ tm .assert_sp_series_equal (sparse ['y' ],
391
+ orig ['y' ].to_sparse (fill_value = 0 ))
392
+
393
+ exp = orig [['x' ]].to_sparse (fill_value = 0 )
394
+ exp ._default_fill_value = np .nan
395
+ tm .assert_sp_frame_equal (sparse [['x' ]], exp )
396
+
397
+ exp = orig [['z' , 'x' ]].to_sparse (fill_value = 0 )
398
+ exp ._default_fill_value = np .nan
399
+ tm .assert_sp_frame_equal (sparse [['z' , 'x' ]], exp )
400
+
401
+ indexer = [True , False , True , True ]
402
+ exp = orig [indexer ].to_sparse (fill_value = 0 )
403
+ exp ._default_fill_value = np .nan
404
+ tm .assert_sp_frame_equal (sparse [indexer ], exp )
405
+
406
+ exp = orig [[1 , 2 ]].to_sparse (fill_value = 0 )
407
+ exp ._default_fill_value = np .nan
408
+ tm .assert_sp_frame_equal (sparse [[1 , 2 ]], exp )
409
+
324
410
def test_loc (self ):
325
411
orig = pd .DataFrame ([[1 , np .nan , np .nan ],
326
412
[2 , 3 , np .nan ],
@@ -477,3 +563,151 @@ def test_iloc_slice(self):
477
563
columns = list ('xyz' ))
478
564
sparse = orig .to_sparse ()
479
565
tm .assert_sp_frame_equal (sparse .iloc [2 :], orig .iloc [2 :].to_sparse ())
566
+
567
+ def test_at (self ):
568
+ orig = pd .DataFrame ([[1 , np .nan , 0 ],
569
+ [2 , 3 , np .nan ],
570
+ [0 , np .nan , 4 ],
571
+ [0 , np .nan , 5 ]],
572
+ index = list ('ABCD' ), columns = list ('xyz' ))
573
+ sparse = orig .to_sparse ()
574
+ self .assertEqual (sparse .at ['A' , 'x' ], orig .at ['A' , 'x' ])
575
+ self .assertTrue (np .isnan (sparse .at ['B' , 'z' ]))
576
+ self .assertTrue (np .isnan (sparse .at ['C' , 'y' ]))
577
+ self .assertEqual (sparse .at ['D' , 'x' ], orig .at ['D' , 'x' ])
578
+
579
+ def test_at_fill_value (self ):
580
+ orig = pd .DataFrame ([[1 , np .nan , 0 ],
581
+ [2 , 3 , np .nan ],
582
+ [0 , np .nan , 4 ],
583
+ [0 , np .nan , 5 ]],
584
+ index = list ('ABCD' ), columns = list ('xyz' ))
585
+ sparse = orig .to_sparse (fill_value = 0 )
586
+ self .assertEqual (sparse .at ['A' , 'x' ], orig .at ['A' , 'x' ])
587
+ self .assertTrue (np .isnan (sparse .at ['B' , 'z' ]))
588
+ self .assertTrue (np .isnan (sparse .at ['C' , 'y' ]))
589
+ self .assertEqual (sparse .at ['D' , 'x' ], orig .at ['D' , 'x' ])
590
+
591
+ def test_iat (self ):
592
+ orig = pd .DataFrame ([[1 , np .nan , 0 ],
593
+ [2 , 3 , np .nan ],
594
+ [0 , np .nan , 4 ],
595
+ [0 , np .nan , 5 ]],
596
+ index = list ('ABCD' ), columns = list ('xyz' ))
597
+ sparse = orig .to_sparse ()
598
+ self .assertEqual (sparse .iat [0 , 0 ], orig .iat [0 , 0 ])
599
+ self .assertTrue (np .isnan (sparse .iat [1 , 2 ]))
600
+ self .assertTrue (np .isnan (sparse .iat [2 , 1 ]))
601
+ self .assertEqual (sparse .iat [2 , 0 ], orig .iat [2 , 0 ])
602
+
603
+ self .assertTrue (np .isnan (sparse .iat [- 1 , - 2 ]))
604
+ self .assertEqual (sparse .iat [- 1 , - 1 ], orig .iat [- 1 , - 1 ])
605
+
606
+ def test_iat_fill_value (self ):
607
+ orig = pd .DataFrame ([[1 , np .nan , 0 ],
608
+ [2 , 3 , np .nan ],
609
+ [0 , np .nan , 4 ],
610
+ [0 , np .nan , 5 ]],
611
+ index = list ('ABCD' ), columns = list ('xyz' ))
612
+ sparse = orig .to_sparse (fill_value = 0 )
613
+ self .assertEqual (sparse .iat [0 , 0 ], orig .iat [0 , 0 ])
614
+ self .assertTrue (np .isnan (sparse .iat [1 , 2 ]))
615
+ self .assertTrue (np .isnan (sparse .iat [2 , 1 ]))
616
+ self .assertEqual (sparse .iat [2 , 0 ], orig .iat [2 , 0 ])
617
+
618
+ self .assertTrue (np .isnan (sparse .iat [- 1 , - 2 ]))
619
+ self .assertEqual (sparse .iat [- 1 , - 1 ], orig .iat [- 1 , - 1 ])
620
+
621
+ def test_take (self ):
622
+ orig = pd .DataFrame ([[1 , np .nan , 0 ],
623
+ [2 , 3 , np .nan ],
624
+ [0 , np .nan , 4 ],
625
+ [0 , np .nan , 5 ]],
626
+ columns = list ('xyz' ))
627
+ sparse = orig .to_sparse ()
628
+
629
+ tm .assert_sp_frame_equal (sparse .take ([0 ]),
630
+ orig .take ([0 ]).to_sparse ())
631
+ tm .assert_sp_frame_equal (sparse .take ([0 , 1 ]),
632
+ orig .take ([0 , 1 ]).to_sparse ())
633
+ tm .assert_sp_frame_equal (sparse .take ([- 1 , - 2 ]),
634
+ orig .take ([- 1 , - 2 ]).to_sparse ())
635
+
636
+ def test_take_fill_value (self ):
637
+ orig = pd .DataFrame ([[1 , np .nan , 0 ],
638
+ [2 , 3 , np .nan ],
639
+ [0 , np .nan , 4 ],
640
+ [0 , np .nan , 5 ]],
641
+ columns = list ('xyz' ))
642
+ sparse = orig .to_sparse (fill_value = 0 )
643
+
644
+ exp = orig .take ([0 ]).to_sparse (fill_value = 0 )
645
+ exp ._default_fill_value = np .nan
646
+ tm .assert_sp_frame_equal (sparse .take ([0 ]), exp )
647
+
648
+ exp = orig .take ([0 , 1 ]).to_sparse (fill_value = 0 )
649
+ exp ._default_fill_value = np .nan
650
+ tm .assert_sp_frame_equal (sparse .take ([0 , 1 ]), exp )
651
+
652
+ exp = orig .take ([- 1 , - 2 ]).to_sparse (fill_value = 0 )
653
+ exp ._default_fill_value = np .nan
654
+ tm .assert_sp_frame_equal (sparse .take ([- 1 , - 2 ]), exp )
655
+
656
+ def test_reindex (self ):
657
+ orig = pd .DataFrame ([[1 , np .nan , 0 ],
658
+ [2 , 3 , np .nan ],
659
+ [0 , np .nan , 4 ],
660
+ [0 , np .nan , 5 ]],
661
+ index = list ('ABCD' ), columns = list ('xyz' ))
662
+ sparse = orig .to_sparse ()
663
+
664
+ res = sparse .reindex (['A' , 'C' , 'B' ])
665
+ exp = orig .reindex (['A' , 'C' , 'B' ]).to_sparse ()
666
+ tm .assert_sp_frame_equal (res , exp )
667
+
668
+ orig = pd .DataFrame ([[np .nan , np .nan , np .nan ],
669
+ [np .nan , np .nan , np .nan ],
670
+ [np .nan , np .nan , np .nan ],
671
+ [np .nan , np .nan , np .nan ]],
672
+ index = list ('ABCD' ), columns = list ('xyz' ))
673
+ sparse = orig .to_sparse ()
674
+
675
+ res = sparse .reindex (['A' , 'C' , 'B' ])
676
+ exp = orig .reindex (['A' , 'C' , 'B' ]).to_sparse ()
677
+ tm .assert_sp_frame_equal (res , exp )
678
+
679
+ def test_reindex_fill_value (self ):
680
+ orig = pd .DataFrame ([[1 , np .nan , 0 ],
681
+ [2 , 3 , np .nan ],
682
+ [0 , np .nan , 4 ],
683
+ [0 , np .nan , 5 ]],
684
+ index = list ('ABCD' ), columns = list ('xyz' ))
685
+ sparse = orig .to_sparse (fill_value = 0 )
686
+
687
+ res = sparse .reindex (['A' , 'C' , 'B' ])
688
+ exp = orig .reindex (['A' , 'C' , 'B' ]).to_sparse (fill_value = 0 )
689
+ tm .assert_sp_frame_equal (res , exp )
690
+
691
+ # all missing
692
+ orig = pd .DataFrame ([[np .nan , np .nan , np .nan ],
693
+ [np .nan , np .nan , np .nan ],
694
+ [np .nan , np .nan , np .nan ],
695
+ [np .nan , np .nan , np .nan ]],
696
+ index = list ('ABCD' ), columns = list ('xyz' ))
697
+ sparse = orig .to_sparse (fill_value = 0 )
698
+
699
+ res = sparse .reindex (['A' , 'C' , 'B' ])
700
+ exp = orig .reindex (['A' , 'C' , 'B' ]).to_sparse (fill_value = 0 )
701
+ tm .assert_sp_frame_equal (res , exp )
702
+
703
+ # all fill_value
704
+ orig = pd .DataFrame ([[0 , 0 , 0 ],
705
+ [0 , 0 , 0 ],
706
+ [0 , 0 , 0 ],
707
+ [0 , 0 , 0 ]],
708
+ index = list ('ABCD' ), columns = list ('xyz' ))
709
+ sparse = orig .to_sparse (fill_value = 0 )
710
+
711
+ res = sparse .reindex (['A' , 'C' , 'B' ])
712
+ exp = orig .reindex (['A' , 'C' , 'B' ]).to_sparse (fill_value = 0 )
713
+ tm .assert_sp_frame_equal (res , exp )
0 commit comments