Skip to content

Commit 2d13410

Browse files
sinhrksjreback
authored andcommitted
BUG: to_dense does not preserve dtype in SparseArray
closes #12778 closes #10648
1 parent 02478d0 commit 2d13410

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

doc/source/whatsnew/v0.18.1.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,14 @@ Other Enhancements
7272
Sparse changes
7373
~~~~~~~~~~~~~~
7474

75-
These changes conform sparse handling to return the correct types & work smoother with indexing.
75+
These changes conform sparse handling to return the correct types and work to make a smoother experience with indexing.
7676

7777
- Bug in ``SparseSeries.loc[]`` with list-like input raises ``TypeError`` (:issue:`10560`)
7878
- Bug in ``SparseSeries.iloc[]`` with scalar input may raise ``IndexError`` (:issue:`10560`)
7979
- Bug in ``SparseSeries.loc[]``, ``.iloc[]`` with ``slice`` returns ``SparseArray``, rather than ``SparseSeries`` (:issue:`10560`)
8080
- Bug in ``SparseSeries.__repr__`` raises ``TypeError`` when it is longer than ``max_rows`` (:issue:`10560`)
8181
- Bug in ``SparseSeries.shape`` ignores ``fill_value`` (:issue:`10452`)
82+
- Bug in ``SparseArray.to_dense()`` does not preserve ``dtype`` (:issue:`10648`)
8283
- ``SparseArray.take`` now returns scalar for scalar input, ``SparseArray`` for others (:issue:`10560`)
8384

8485
.. ipython:: python

pandas/sparse/array.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def values(self):
237237
"""
238238
Dense values
239239
"""
240-
output = np.empty(len(self), dtype=np.float64)
240+
output = np.empty(len(self), dtype=self.dtype)
241241
int_index = self.sp_index.to_int_index()
242242
output.fill(self.fill_value)
243243
output.put(int_index.indices, self)
@@ -261,8 +261,8 @@ def to_dense(self, fill=None):
261261
# fill the nans
262262
if fill is None:
263263
fill = self.fill_value
264-
if not np.isnan(fill):
265-
values[np.isnan(values)] = fill
264+
if not com.isnull(fill):
265+
values[com.isnull(values)] = fill
266266

267267
return values
268268

pandas/sparse/tests/test_array.py

+28
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,34 @@ def test_constructor_copy(self):
7474
not_copy.sp_values[:3] = 0
7575
self.assertTrue((self.arr.sp_values[:3] == 0).all())
7676

77+
def test_constructor_bool(self):
78+
# GH 10648
79+
data = np.array([False, False, True, True, False, False])
80+
arr = SparseArray(data, fill_value=False, dtype=bool)
81+
82+
self.assertEqual(arr.dtype, bool)
83+
tm.assert_numpy_array_equal(arr.sp_values, np.array([True, True]))
84+
tm.assert_numpy_array_equal(arr.sp_values, np.asarray(arr))
85+
tm.assert_numpy_array_equal(arr.sp_index.indices, np.array([2, 3]))
86+
87+
for dense in [arr.to_dense(), arr.values]:
88+
self.assertEqual(dense.dtype, bool)
89+
tm.assert_numpy_array_equal(dense, data)
90+
91+
def test_constructor_float32(self):
92+
# GH 10648
93+
data = np.array([1., np.nan, 3], dtype=np.float32)
94+
arr = SparseArray(data, dtype=np.float32)
95+
96+
self.assertEqual(arr.dtype, np.float32)
97+
tm.assert_numpy_array_equal(arr.sp_values, np.array([1, 3]))
98+
tm.assert_numpy_array_equal(arr.sp_values, np.asarray(arr))
99+
tm.assert_numpy_array_equal(arr.sp_index.indices, np.array([0, 2]))
100+
101+
for dense in [arr.to_dense(), arr.values]:
102+
self.assertEqual(dense.dtype, np.float32)
103+
self.assert_numpy_array_equal(dense, data)
104+
77105
def test_astype(self):
78106
res = self.arr.astype('f8')
79107
res.sp_values[:3] = 27

0 commit comments

Comments
 (0)