Skip to content

Commit 5b0f14e

Browse files
BUG: Fixed GH 16179 and modified tests accordingly
1 parent 096e215 commit 5b0f14e

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

pandas/core/sparse/frame.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,13 @@ def _init_spmatrix(self, data, index, columns, dtype=None,
191191
for col, rowvals in values.groupby(data.col):
192192
# get_blocks expects int32 row indices in sorted order
193193
rows = rowvals.index.values.astype(np.int32)
194+
vals = np.array([y for x, y in sorted(rowvals.iteritems())],
195+
dtype=rowvals.dtype)
194196
rows.sort()
195197
blocs, blens = get_blocks(rows)
196198

197199
sdict[columns[col]] = SparseSeries(
198-
rowvals.values, index=index,
200+
vals, index=index,
199201
fill_value=fill_value,
200202
sparse_index=BlockIndex(N, blocs, blens))
201203

pandas/tests/sparse/test_frame.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1146,8 +1146,8 @@ def test_isnotnull(self):
11461146
tm.assert_frame_equal(res.to_dense(), exp)
11471147

11481148

1149-
@pytest.mark.parametrize('index', [None, list('ab')]) # noqa: F811
1150-
@pytest.mark.parametrize('columns', [None, list('cd')])
1149+
@pytest.mark.parametrize('index', [None, list('abc')]) # noqa: F811
1150+
@pytest.mark.parametrize('columns', [None, list('def')])
11511151
@pytest.mark.parametrize('fill_value', [None, 0, np.nan])
11521152
@pytest.mark.parametrize('dtype', [bool, int, float, np.uint16])
11531153
def test_from_to_scipy(spmatrix, index, columns, fill_value, dtype):
@@ -1156,7 +1156,8 @@ def test_from_to_scipy(spmatrix, index, columns, fill_value, dtype):
11561156

11571157
# Make one ndarray and from it one sparse matrix, both to be used for
11581158
# constructing frames and comparing results
1159-
arr = np.eye(2, dtype=dtype)
1159+
arr = np.eye(3, dtype=dtype)
1160+
arr[0, 1] = dtype(2)
11601161
try:
11611162
spm = spmatrix(arr)
11621163
assert spm.dtype == arr.dtype

0 commit comments

Comments
 (0)