@@ -38,8 +38,7 @@ class TestSparseSeriesConcat(object):
38
38
39
39
@pytest .mark .parametrize ('kind' , [
40
40
'integer' ,
41
- pytest .param ('block' ,
42
- marks = pytest .mark .xfail (reason = 'Broken' , strict = "TODO" )),
41
+ 'block' ,
43
42
])
44
43
def test_concat (self , kind ):
45
44
val1 = np .array ([1 , 2 , np .nan , np .nan , 0 , np .nan ])
@@ -51,15 +50,15 @@ def test_concat(self, kind):
51
50
res = pd .concat ([sparse1 , sparse2 ])
52
51
exp = pd .concat ([pd .Series (val1 ), pd .Series (val2 )])
53
52
exp = pd .SparseSeries (exp , kind = kind )
54
- tm .assert_sp_series_equal (res , exp )
53
+ tm .assert_sp_series_equal (res , exp , consolidate_block_indices = True )
55
54
56
55
sparse1 = pd .SparseSeries (val1 , fill_value = 0 , name = 'x' , kind = kind )
57
56
sparse2 = pd .SparseSeries (val2 , fill_value = 0 , name = 'y' , kind = kind )
58
57
59
58
res = pd .concat ([sparse1 , sparse2 ])
60
59
exp = pd .concat ([pd .Series (val1 ), pd .Series (val2 )])
61
60
exp = pd .SparseSeries (exp , fill_value = 0 , kind = kind )
62
- tm .assert_sp_series_equal (res , exp )
61
+ tm .assert_sp_series_equal (res , exp , consolidate_block_indices = True )
63
62
64
63
def test_concat_axis1 (self ):
65
64
val1 = np .array ([1 , 2 , np .nan , np .nan , 0 , np .nan ])
@@ -127,10 +126,8 @@ def test_concat_different_kind(self):
127
126
tm .assert_sp_series_equal (res , exp , consolidate_block_indices = True )
128
127
129
128
@pytest .mark .parametrize ('kind' , [
130
- pytest .param ('integer' ,
131
- marks = pytest .mark .xfail (reason = "Return Series[Sparse]" )),
132
- pytest .param ('block' ,
133
- marks = pytest .mark .xfail (reason = 'Broken' , strict = "TODO" )),
129
+ 'integer' ,
130
+ 'block' ,
134
131
])
135
132
def test_concat_sparse_dense (self , kind ):
136
133
# use first input's fill_value
@@ -147,27 +144,43 @@ def test_concat_sparse_dense(self, kind):
147
144
148
145
res = pd .concat ([dense , sparse , dense ])
149
146
exp = pd .concat ([dense , pd .Series (val1 ), dense ])
150
- exp = pd .SparseSeries (exp , kind = kind )
151
- tm .assert_sp_series_equal (res , exp )
147
+ # XXX: changed from SparseSeries to Series[sparse]
148
+ exp = pd .Series (
149
+ pd .SparseArray (exp , kind = kind ),
150
+ index = exp .index ,
151
+ name = exp .name ,
152
+ )
153
+ tm .assert_series_equal (res , exp )
152
154
153
155
sparse = pd .SparseSeries (val1 , name = 'x' , kind = kind , fill_value = 0 )
154
156
dense = pd .Series (val2 , name = 'y' )
155
157
156
158
res = pd .concat ([sparse , dense ])
159
+ # XXX: changed from SparseSeries to Series[sparse]
157
160
exp = pd .concat ([pd .Series (val1 ), dense ])
158
- exp = pd .SparseSeries (exp , kind = kind , fill_value = 0 )
159
- tm .assert_sp_series_equal (res , exp )
161
+ exp = pd .Series (
162
+ pd .SparseArray (exp , kind = kind , fill_value = 0 ),
163
+ index = exp .index ,
164
+ name = exp .name ,
165
+ )
166
+ tm .assert_series_equal (res , exp )
160
167
161
168
res = pd .concat ([dense , sparse , dense ])
162
169
exp = pd .concat ([dense , pd .Series (val1 ), dense ])
163
- exp = pd .SparseSeries (exp , kind = kind , fill_value = 0 )
164
- tm .assert_sp_series_equal (res , exp )
170
+ # XXX: changed from SparseSeries to Series[sparse]
171
+ exp = pd .Series (
172
+ pd .SparseArray (exp , kind = kind , fill_value = 0 ),
173
+ index = exp .index ,
174
+ name = exp .name ,
175
+ )
176
+ tm .assert_series_equal (res , exp )
165
177
166
178
@pytest .mark .xfail (reason = "Correct result is unclear." , strict = True )
167
179
def test_concat_mixed_dtypes (self ):
168
180
# Concatenating sparse, regular, and categorical.
169
181
# Who should "win" in the dtype determination?
170
182
# This test assumes that sparse wins.
183
+ # At the moment, we're just object.
171
184
df1 = pd .DataFrame ({"A" : pd .SparseArray ([1 , 2 , 3 ])})
172
185
df2 = pd .DataFrame ({"A" : [1 , 2 , 3 ]})
173
186
df3 = pd .DataFrame ({"A" : ['a' , 'b' , 'c' ]}).astype ('category' )
@@ -273,54 +286,54 @@ def test_concat_different_columns_sort_warns(self):
273
286
274
287
def test_concat_different_columns (self ):
275
288
# fill_value = np.nan
276
- sparse = self .dense1 .to_sparse ()
277
- sparse3 = self .dense3 .to_sparse ()
278
-
279
- res = pd .concat ([sparse , sparse3 ], sort = True )
280
- exp = pd .concat ([self .dense1 , self .dense3 ], sort = True ).to_sparse ()
281
- tm .assert_sp_frame_equal (res , exp , check_kind = False )
282
-
283
- res = pd .concat ([sparse3 , sparse ], sort = True )
284
- exp = pd .concat ([self .dense3 , self .dense1 ], sort = True ).to_sparse ()
285
- exp ._default_fill_value = np .nan
286
- tm .assert_sp_frame_equal (res , exp , check_kind = False )
289
+ # sparse = self.dense1.to_sparse()
290
+ # sparse3 = self.dense3.to_sparse()
287
291
288
- # fill_value = 0
292
+ # res = pd.concat([sparse, sparse3], sort=True)
293
+ # exp = pd.concat([self.dense1, self.dense3], sort=True).to_sparse()
294
+ # tm.assert_sp_frame_equal(res, exp, check_kind=False)
295
+ #
296
+ # res = pd.concat([sparse3, sparse], sort=True)
297
+ # exp = pd.concat([self.dense3, self.dense1], sort=True).to_sparse()
298
+ # exp._default_fill_value = np.nan
299
+ # tm.assert_sp_frame_equal(res, exp, check_kind=False)
300
+ #
301
+ # # fill_value = 0
289
302
sparse = self .dense1 .to_sparse (fill_value = 0 )
290
303
sparse3 = self .dense3 .to_sparse (fill_value = 0 )
291
304
292
305
# this test is buggy. from here on out
293
306
# exp doesn't handle C (all NaN) correctly.
294
307
# We correctly don't have any sparse values since the
295
308
# values are all NaN, and the fill_value is 0.
296
- raise pytest .xfail ("Test is buggy." )
297
- # res = pd.concat([sparse, sparse3], sort=True)
298
- # exp = (pd.concat([self.dense1, self.dense3], sort=True)
299
- # .to_sparse(fill_value=0))
300
- # exp._default_fill_value = np.nan
309
+ # raise pytest.xfail("Test is buggy.")
310
+ res = pd .concat ([sparse , sparse3 ], sort = True )
311
+ exp = (pd .concat ([self .dense1 , self .dense3 ], sort = True )
312
+ .to_sparse (fill_value = 0 ))
313
+ exp ._default_fill_value = np .nan
301
314
302
- # tm.assert_sp_frame_equal(res, exp, check_kind=False,
303
- # consolidate_block_indices=True)
315
+ tm .assert_sp_frame_equal (res , exp , check_kind = False ,
316
+ consolidate_block_indices = True )
304
317
305
- # res = pd.concat([sparse3, sparse], sort=True)
306
- # exp = (pd.concat([self.dense3, self.dense1], sort=True)
307
- # .to_sparse(fill_value=0))
308
- # exp._default_fill_value = np.nan
309
- # tm.assert_sp_frame_equal(res, exp, check_kind=False)
310
- #
311
- # # different fill values
312
- # sparse = self.dense1.to_sparse()
313
- # sparse3 = self.dense3.to_sparse(fill_value=0)
314
- # # each columns keeps its fill_value, thus compare in dense
315
- # res = pd.concat([sparse, sparse3], sort=True)
316
- # exp = pd.concat([self.dense1, self.dense3], sort=True)
317
- # assert isinstance(res, pd.SparseDataFrame)
318
- # tm.assert_frame_equal(res.to_dense(), exp)
319
- #
320
- # res = pd.concat([sparse3, sparse], sort=True)
321
- # exp = pd.concat([self.dense3, self.dense1], sort=True)
322
- # assert isinstance(res, pd.SparseDataFrame)
323
- # tm.assert_frame_equal(res.to_dense(), exp)
318
+ res = pd .concat ([sparse3 , sparse ], sort = True )
319
+ exp = (pd .concat ([self .dense3 , self .dense1 ], sort = True )
320
+ .to_sparse (fill_value = 0 ))
321
+ exp ._default_fill_value = np .nan
322
+ tm .assert_sp_frame_equal (res , exp , check_kind = False )
323
+
324
+ # different fill values
325
+ sparse = self .dense1 .to_sparse ()
326
+ sparse3 = self .dense3 .to_sparse (fill_value = 0 )
327
+ # each columns keeps its fill_value, thus compare in dense
328
+ res = pd .concat ([sparse , sparse3 ], sort = True )
329
+ exp = pd .concat ([self .dense1 , self .dense3 ], sort = True )
330
+ assert isinstance (res , pd .SparseDataFrame )
331
+ tm .assert_frame_equal (res .to_dense (), exp )
332
+
333
+ res = pd .concat ([sparse3 , sparse ], sort = True )
334
+ exp = pd .concat ([self .dense3 , self .dense1 ], sort = True )
335
+ assert isinstance (res , pd .SparseDataFrame )
336
+ tm .assert_frame_equal (res .to_dense (), exp )
324
337
325
338
def test_concat_series (self ):
326
339
# fill_value = np.nan
0 commit comments