88
99from pandas import _np_version_under1p8
1010from pandas .sparse .api import SparseArray
11- import pandas .sparse . array as sparray
11+ from pandas ._sparse import IntIndex
1212from pandas .util .testing import assert_almost_equal , assertRaisesRegexp
1313import pandas .util .testing as tm
1414
1515
16- class TestSparseArrayIndex (tm .TestCase ):
17-
18- _multiprocess_can_split_ = True
19-
20- def test_int_internal (self ):
21- idx = sparray ._make_index (4 , np .array ([2 , 3 ], dtype = np .int32 ),
22- kind = 'integer' )
23- self .assertIsInstance (idx , sparray .IntIndex )
24- self .assertEqual (idx .npoints , 2 )
25- tm .assert_numpy_array_equal (idx .indices ,
26- np .array ([2 , 3 ], dtype = np .int32 ))
27-
28- idx = sparray ._make_index (4 , np .array ([], dtype = np .int32 ),
29- kind = 'integer' )
30- self .assertIsInstance (idx , sparray .IntIndex )
31- self .assertEqual (idx .npoints , 0 )
32- tm .assert_numpy_array_equal (idx .indices ,
33- np .array ([], dtype = np .int32 ))
34-
35- idx = sparray ._make_index (4 , np .array ([0 , 1 , 2 , 3 ], dtype = np .int32 ),
36- kind = 'integer' )
37- self .assertIsInstance (idx , sparray .IntIndex )
38- self .assertEqual (idx .npoints , 4 )
39- tm .assert_numpy_array_equal (idx .indices ,
40- np .array ([0 , 1 , 2 , 3 ], dtype = np .int32 ))
41-
42- def test_block_internal (self ):
43- idx = sparray ._make_index (4 , np .array ([2 , 3 ], dtype = np .int32 ),
44- kind = 'block' )
45- self .assertIsInstance (idx , sparray .BlockIndex )
46- self .assertEqual (idx .npoints , 2 )
47- tm .assert_numpy_array_equal (idx .blocs ,
48- np .array ([2 ], dtype = np .int32 ))
49- tm .assert_numpy_array_equal (idx .blengths ,
50- np .array ([2 ], dtype = np .int32 ))
51-
52- idx = sparray ._make_index (4 , np .array ([], dtype = np .int32 ),
53- kind = 'block' )
54- self .assertIsInstance (idx , sparray .BlockIndex )
55- self .assertEqual (idx .npoints , 0 )
56- tm .assert_numpy_array_equal (idx .blocs ,
57- np .array ([], dtype = np .int32 ))
58- tm .assert_numpy_array_equal (idx .blengths ,
59- np .array ([], dtype = np .int32 ))
60-
61- idx = sparray ._make_index (4 , np .array ([0 , 1 , 2 , 3 ], dtype = np .int32 ),
62- kind = 'block' )
63- self .assertIsInstance (idx , sparray .BlockIndex )
64- self .assertEqual (idx .npoints , 4 )
65- tm .assert_numpy_array_equal (idx .blocs ,
66- np .array ([0 ], dtype = np .int32 ))
67- tm .assert_numpy_array_equal (idx .blengths ,
68- np .array ([4 ], dtype = np .int32 ))
69-
70- idx = sparray ._make_index (4 , np .array ([0 , 2 , 3 ], dtype = np .int32 ),
71- kind = 'block' )
72- self .assertIsInstance (idx , sparray .BlockIndex )
73- self .assertEqual (idx .npoints , 3 )
74- tm .assert_numpy_array_equal (idx .blocs ,
75- np .array ([0 , 2 ], dtype = np .int32 ))
76- tm .assert_numpy_array_equal (idx .blengths ,
77- np .array ([1 , 2 ], dtype = np .int32 ))
78-
79- def test_lookup (self ):
80- for kind in ['integer' , 'block' ]:
81- idx = sparray ._make_index (4 , np .array ([2 , 3 ], dtype = np .int32 ),
82- kind = kind )
83- self .assertEqual (idx .lookup (- 1 ), - 1 )
84- self .assertEqual (idx .lookup (0 ), - 1 )
85- self .assertEqual (idx .lookup (1 ), - 1 )
86- self .assertEqual (idx .lookup (2 ), 0 )
87- self .assertEqual (idx .lookup (3 ), 1 )
88- self .assertEqual (idx .lookup (4 ), - 1 )
89-
90- idx = sparray ._make_index (4 , np .array ([], dtype = np .int32 ),
91- kind = kind )
92- for i in range (- 1 , 5 ):
93- self .assertEqual (idx .lookup (i ), - 1 )
94-
95- idx = sparray ._make_index (4 , np .array ([0 , 1 , 2 , 3 ],
96- dtype = np .int32 ), kind = kind )
97- self .assertEqual (idx .lookup (- 1 ), - 1 )
98- self .assertEqual (idx .lookup (0 ), 0 )
99- self .assertEqual (idx .lookup (1 ), 1 )
100- self .assertEqual (idx .lookup (2 ), 2 )
101- self .assertEqual (idx .lookup (3 ), 3 )
102- self .assertEqual (idx .lookup (4 ), - 1 )
103-
104- idx = sparray ._make_index (4 , np .array ([0 , 2 , 3 ], dtype = np .int32 ),
105- kind = kind )
106- self .assertEqual (idx .lookup (- 1 ), - 1 )
107- self .assertEqual (idx .lookup (0 ), 0 )
108- self .assertEqual (idx .lookup (1 ), - 1 )
109- self .assertEqual (idx .lookup (2 ), 1 )
110- self .assertEqual (idx .lookup (3 ), 2 )
111- self .assertEqual (idx .lookup (4 ), - 1 )
112-
113- def test_lookup_array (self ):
114- for kind in ['integer' , 'block' ]:
115- idx = sparray ._make_index (4 , np .array ([2 , 3 ], dtype = np .int32 ),
116- kind = kind )
117-
118- res = idx .lookup_array (np .array ([- 1 , 0 , 2 ], dtype = np .int32 ))
119- exp = np .array ([- 1 , - 1 , 0 ], dtype = np .int32 )
120- self .assert_numpy_array_equal (res , exp )
121-
122- res = idx .lookup_array (np .array ([4 , 2 , 1 , 3 ], dtype = np .int32 ))
123- exp = np .array ([- 1 , 0 , - 1 , 1 ], dtype = np .int32 )
124- self .assert_numpy_array_equal (res , exp )
125-
126- idx = sparray ._make_index (4 , np .array ([], dtype = np .int32 ),
127- kind = kind )
128- res = idx .lookup_array (np .array ([- 1 , 0 , 2 , 4 ], dtype = np .int32 ))
129- exp = np .array ([- 1 , - 1 , - 1 , - 1 ], dtype = np .int32 )
130-
131- idx = sparray ._make_index (4 , np .array ([0 , 1 , 2 , 3 ],
132- dtype = np .int32 ),
133- kind = kind )
134- res = idx .lookup_array (np .array ([- 1 , 0 , 2 ], dtype = np .int32 ))
135- exp = np .array ([- 1 , 0 , 2 ], dtype = np .int32 )
136- self .assert_numpy_array_equal (res , exp )
137-
138- res = idx .lookup_array (np .array ([4 , 2 , 1 , 3 ], dtype = np .int32 ))
139- exp = np .array ([- 1 , 2 , 1 , 3 ], dtype = np .int32 )
140- self .assert_numpy_array_equal (res , exp )
141-
142- idx = sparray ._make_index (4 , np .array ([0 , 2 , 3 ], dtype = np .int32 ),
143- kind = kind )
144- res = idx .lookup_array (np .array ([2 , 1 , 3 , 0 ], dtype = np .int32 ))
145- exp = np .array ([1 , - 1 , 2 , 0 ], dtype = np .int32 )
146- self .assert_numpy_array_equal (res , exp )
147-
148- res = idx .lookup_array (np .array ([1 , 4 , 2 , 5 ], dtype = np .int32 ))
149- exp = np .array ([- 1 , - 1 , 1 , - 1 ], dtype = np .int32 )
150- self .assert_numpy_array_equal (res , exp )
151-
152-
15316class TestSparseArray (tm .TestCase ):
15417
15518 _multiprocess_can_split_ = True
@@ -159,6 +22,67 @@ def setUp(self):
15922 self .arr = SparseArray (self .arr_data )
16023 self .zarr = SparseArray ([0 , 0 , 1 , 2 , 3 , 0 , 4 , 5 , 0 , 6 ], fill_value = 0 )
16124
25+ def test_constructor_dtype (self ):
26+ arr = SparseArray ([np .nan , 1 , 2 , np .nan ])
27+ self .assertEqual (arr .dtype , np .float64 )
28+ self .assertTrue (np .isnan (arr .fill_value ))
29+
30+ arr = SparseArray ([np .nan , 1 , 2 , np .nan ], fill_value = 0 )
31+ self .assertEqual (arr .dtype , np .float64 )
32+ self .assertEqual (arr .fill_value , 0 )
33+
34+ arr = SparseArray ([0 , 1 , 2 , 4 ], dtype = np .int64 )
35+ self .assertEqual (arr .dtype , np .int64 )
36+ self .assertTrue (np .isnan (arr .fill_value ))
37+
38+ arr = SparseArray ([0 , 1 , 2 , 4 ], fill_value = 0 , dtype = np .int64 )
39+ self .assertEqual (arr .dtype , np .int64 )
40+ self .assertEqual (arr .fill_value , 0 )
41+
42+ arr = SparseArray ([0 , 1 , 2 , 4 ], dtype = None )
43+ self .assertEqual (arr .dtype , np .int64 )
44+ self .assertTrue (np .isnan (arr .fill_value ))
45+
46+ arr = SparseArray ([0 , 1 , 2 , 4 ], fill_value = 0 , dtype = None )
47+ self .assertEqual (arr .dtype , np .int64 )
48+ self .assertEqual (arr .fill_value , 0 )
49+
50+ def test_constructor_spindex_dtype (self ):
51+ arr = SparseArray (data = [1 , 2 ], sparse_index = IntIndex (4 , [1 , 2 ]))
52+ tm .assert_sp_array_equal (arr , SparseArray ([np .nan , 1 , 2 , np .nan ]))
53+ self .assertEqual (arr .dtype , np .float64 )
54+ self .assertTrue (np .isnan (arr .fill_value ))
55+
56+ arr = SparseArray (data = [0 , 1 , 2 , 3 ],
57+ sparse_index = IntIndex (4 , [0 , 1 , 2 , 3 ]),
58+ dtype = np .int64 )
59+ exp = SparseArray ([0 , 1 , 2 , 3 ], dtype = np .int64 )
60+ tm .assert_sp_array_equal (arr , exp )
61+ self .assertEqual (arr .dtype , np .int64 )
62+ self .assertTrue (np .isnan (arr .fill_value ))
63+
64+ arr = SparseArray (data = [1 , 2 ], sparse_index = IntIndex (4 , [1 , 2 ]),
65+ fill_value = 0 , dtype = np .int64 )
66+ exp = SparseArray ([0 , 1 , 2 , 0 ], fill_value = 0 , dtype = np .int64 )
67+ tm .assert_sp_array_equal (arr , exp )
68+ self .assertEqual (arr .dtype , np .int64 )
69+ self .assertEqual (arr .fill_value , 0 )
70+
71+ arr = SparseArray (data = [0 , 1 , 2 , 3 ],
72+ sparse_index = IntIndex (4 , [0 , 1 , 2 , 3 ]),
73+ dtype = None )
74+ exp = SparseArray ([0 , 1 , 2 , 3 ], dtype = None )
75+ tm .assert_sp_array_equal (arr , exp )
76+ self .assertEqual (arr .dtype , np .int64 )
77+ self .assertTrue (np .isnan (arr .fill_value ))
78+
79+ arr = SparseArray (data = [1 , 2 ], sparse_index = IntIndex (4 , [1 , 2 ]),
80+ fill_value = 0 , dtype = None )
81+ exp = SparseArray ([0 , 1 , 2 , 0 ], fill_value = 0 , dtype = None )
82+ tm .assert_sp_array_equal (arr , exp )
83+ self .assertEqual (arr .dtype , np .int64 )
84+ self .assertEqual (arr .fill_value , 0 )
85+
16286 def test_get_item (self ):
16387
16488 self .assertTrue (np .isnan (self .arr [1 ]))
0 commit comments