|
1 | 1 | # pylint: disable-msg=E1101,W0612
|
| 2 | +import pytest |
2 | 3 |
|
3 | 4 | import numpy as np
|
4 | 5 | import pandas as pd
|
5 | 6 | import pandas.util.testing as tm
|
| 7 | +import itertools |
6 | 8 |
|
7 | 9 |
|
8 | 10 | class TestSparseSeriesConcat(object):
|
@@ -317,37 +319,52 @@ def test_concat_axis1(self):
|
317 | 319 | assert isinstance(res, pd.SparseDataFrame)
|
318 | 320 | tm.assert_frame_equal(res.to_dense(), exp)
|
319 | 321 |
|
320 |
| - def test_concat_sparse_dense(self): |
321 |
| - sparse = self.dense1.to_sparse() |
322 |
| - |
323 |
| - res = pd.concat([sparse, self.dense2]) |
324 |
| - exp = pd.concat([self.dense1, self.dense2]) |
325 |
| - assert isinstance(res, pd.SparseDataFrame) |
326 |
| - tm.assert_frame_equal(res.to_dense(), exp) |
327 |
| - |
328 |
| - res = pd.concat([self.dense2, sparse]) |
329 |
| - exp = pd.concat([self.dense2, self.dense1]) |
330 |
| - assert isinstance(res, pd.SparseDataFrame) |
331 |
| - tm.assert_frame_equal(res.to_dense(), exp) |
332 |
| - |
333 |
| - sparse = self.dense1.to_sparse(fill_value=0) |
334 |
| - |
335 |
| - res = pd.concat([sparse, self.dense2]) |
336 |
| - exp = pd.concat([self.dense1, self.dense2]) |
337 |
| - assert isinstance(res, pd.SparseDataFrame) |
338 |
| - tm.assert_frame_equal(res.to_dense(), exp) |
339 |
| - |
340 |
| - res = pd.concat([self.dense2, sparse]) |
341 |
| - exp = pd.concat([self.dense2, self.dense1]) |
342 |
| - assert isinstance(res, pd.SparseDataFrame) |
343 |
| - tm.assert_frame_equal(res.to_dense(), exp) |
344 |
| - |
345 |
| - res = pd.concat([self.dense3, sparse], axis=1) |
346 |
| - exp = pd.concat([self.dense3, self.dense1], axis=1) |
347 |
| - assert isinstance(res, pd.SparseDataFrame) |
348 |
| - tm.assert_frame_equal(res, exp) |
349 |
| - |
350 |
| - res = pd.concat([sparse, self.dense3], axis=1) |
351 |
| - exp = pd.concat([self.dense1, self.dense3], axis=1) |
352 |
| - assert isinstance(res, pd.SparseDataFrame) |
353 |
| - tm.assert_frame_equal(res, exp) |
| 322 | + @pytest.mark.parametrize('fill_value,sparse_idx,dense_idx', |
| 323 | + itertools.product([None, 0, 1, np.nan], |
| 324 | + [0, 1], |
| 325 | + [1, 0])) |
| 326 | + def test_concat_sparse_dense_rows(self, fill_value, sparse_idx, dense_idx): |
| 327 | + frames = [self.dense1, self.dense2] |
| 328 | + sparse_frame = [frames[dense_idx], |
| 329 | + frames[sparse_idx].to_sparse(fill_value=fill_value)] |
| 330 | + dense_frame = [frames[dense_idx], frames[sparse_idx]] |
| 331 | + |
| 332 | + # This will try both directions sparse + dense and dense + sparse |
| 333 | + for _ in range(2): |
| 334 | + res = pd.concat(sparse_frame) |
| 335 | + exp = pd.concat(dense_frame) |
| 336 | + |
| 337 | + assert isinstance(res, pd.SparseDataFrame) |
| 338 | + tm.assert_frame_equal(res.to_dense(), exp) |
| 339 | + |
| 340 | + sparse_frame = sparse_frame[::-1] |
| 341 | + dense_frame = dense_frame[::-1] |
| 342 | + |
| 343 | + @pytest.mark.parametrize('fill_value,sparse_idx,dense_idx', |
| 344 | + itertools.product([None, 0, 1, np.nan], |
| 345 | + [0, 1], |
| 346 | + [1, 0])) |
| 347 | + def test_concat_sparse_dense_cols(self, fill_value, sparse_idx, dense_idx): |
| 348 | + # See GH16874, GH18914 and #18686 for why this should be a DataFrame |
| 349 | + |
| 350 | + frames = [self.dense1, self.dense3] |
| 351 | + |
| 352 | + sparse_frame = [frames[dense_idx], |
| 353 | + frames[sparse_idx].to_sparse(fill_value=fill_value)] |
| 354 | + dense_frame = [frames[dense_idx], frames[sparse_idx]] |
| 355 | + |
| 356 | + # This will try both directions sparse + dense and dense + sparse |
| 357 | + for _ in range(2): |
| 358 | + res = pd.concat(sparse_frame, axis=1) |
| 359 | + exp = pd.concat(dense_frame, axis=1) |
| 360 | + |
| 361 | + for column in frames[dense_idx].columns: |
| 362 | + if dense_idx == sparse_idx: |
| 363 | + tm.assert_frame_equal(res[column], exp[column]) |
| 364 | + else: |
| 365 | + tm.assert_series_equal(res[column], exp[column]) |
| 366 | + |
| 367 | + tm.assert_frame_equal(res, exp) |
| 368 | + |
| 369 | + sparse_frame = sparse_frame[::-1] |
| 370 | + dense_frame = dense_frame[::-1] |
0 commit comments