@@ -2712,13 +2712,64 @@ def test_type_error_multiindex(self):
2712
2712
result = dg ['x' , 0 ]
2713
2713
assert_series_equal (result , expected )
2714
2714
2715
- def test_sparse_indexing_single_multitype (self ):
2716
- from pandas import SparseDataFrame , SparseSeries
2717
- sdf = SparseDataFrame ([[1 , 2 , 'a' ], [4 , 5 , 'b' ]])
2718
- tm .assert_sp_series_equal (sdf .iloc [0 ],
2719
- SparseSeries ([1 , 2 , 'a' ], name = 0 ))
2720
- tm .assert_sp_series_equal (sdf .iloc [1 ],
2721
- SparseSeries ([4 , 5 , 'b' ], name = 1 ))
2715
+
2716
+ class TestSparseDataFrameMultitype (tm .TestCase ):
2717
+ def setUp (self ):
2718
+ super (TestSparseDataFrameMultitype , self ).setUp ()
2719
+ self .string_series = pd .SparseSeries (['a' , 'b' , 'c' ])
2720
+ self .int_series = pd .SparseSeries ([1 , 2 , 3 ])
2721
+ self .float_series = pd .SparseSeries ([1.1 , 1.2 , 1.3 ])
2722
+ self .object_series = pd .SparseSeries ([[], {}, set ()])
2723
+ self .sdf = pd .SparseDataFrame ({
2724
+ 'string' : self .string_series ,
2725
+ 'int' : self .int_series ,
2726
+ 'float' : self .float_series ,
2727
+ 'object' : self .object_series ,
2728
+ })
2729
+ self .cols = ['string' , 'int' , 'float' , 'object' ]
2730
+ self .sdf = self .sdf [self .cols ]
2731
+
2732
+ def test_basic_dtypes (self ):
2733
+ for _ , row in self .sdf .iterrows ():
2734
+ self .assertEqual (row .dtype , object )
2735
+ tm .assert_sp_series_equal (self .sdf ['string' ], self .string_series ,
2736
+ check_names = False )
2737
+ tm .assert_sp_series_equal (self .sdf ['int' ], self .int_series ,
2738
+ check_names = False )
2739
+ tm .assert_sp_series_equal (self .sdf ['float' ], self .float_series ,
2740
+ check_names = False )
2741
+ tm .assert_sp_series_equal (self .sdf ['object' ], self .object_series ,
2742
+ check_names = False )
2743
+
2744
+ def test_indexing_single (self ):
2745
+ tm .assert_sp_series_equal (self .sdf .iloc [0 ],
2746
+ pd .SparseSeries (['a' , 1 , 1.1 , []],
2747
+ index = self .cols ),
2748
+ check_names = False )
2749
+ tm .assert_sp_series_equal (self .sdf .iloc [1 ],
2750
+ pd .SparseSeries (['b' , 2 , 1.2 , {}],
2751
+ index = self .cols ),
2752
+ check_names = False )
2753
+ tm .assert_sp_series_equal (self .sdf .iloc [2 ],
2754
+ pd .SparseSeries (['c' , 3 , 1.3 , set ()],
2755
+ index = self .cols ),
2756
+ check_names = False )
2757
+
2758
+ def test_indexing_multiple (self ):
2759
+ tm .assert_sp_frame_equal (self .sdf , self .sdf [:])
2760
+ tm .assert_sp_frame_equal (self .sdf , self .sdf .loc [:])
2761
+ tm .assert_sp_frame_equal (self .sdf .iloc [[1 , 2 ]],
2762
+ pd .SparseDataFrame ({
2763
+ 'string' : ['b' , 'c' ],
2764
+ 'int' : [2 , 3 ],
2765
+ 'float' : [1.2 , 1.3 ],
2766
+ 'object' : [{}, set ()]
2767
+ }, index = [1 , 2 ])[self .cols ])
2768
+ tm .assert_sp_frame_equal (self .sdf [['int' , 'string' ]],
2769
+ pd .SparseDataFrame ({
2770
+ 'int' : self .int_series ,
2771
+ 'string' : self .string_series ,
2772
+ }))
2722
2773
2723
2774
2724
2775
class TestDataFrameIndexingDatetimeWithTZ (tm .TestCase , TestData ):
0 commit comments