Skip to content

BUG: to_dense now preserves dtype in SparseArray #10655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pandas/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def values(self):
"""
Dense values
"""
output = np.empty(len(self), dtype=np.float64)
output = np.empty(len(self), dtype=self.dtype)
int_index = self.sp_index.to_int_index()
output.fill(self.fill_value)
output.put(int_index.indices, self)
Expand All @@ -266,7 +266,8 @@ def to_dense(self, fill=None):
# fill the nans
if fill is None:
fill = self.fill_value
if not np.isnan(fill):
# nans can only occur arrays of floating scalars
if np.issubdtype(self.dtype, np.inexact) and not np.isnan(fill):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a better way to do this is just use isnull which handles all of these things.

values[np.isnan(values)] = fill

return values
Expand Down
24 changes: 21 additions & 3 deletions pandas/sparse/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class TestSparseArray(tm.TestCase):
def setUp(self):
self.arr_data = np.array([nan, nan, 1, 2, 3, nan, 4, 5, nan, 6])
self.arr = SparseArray(self.arr_data)

self.barr_data = np.array([False, False, True, True, False, False])
self.barr = SparseArray(self.barr_data, fill_value=False, dtype=bool)

self.zarr = SparseArray([0, 0, 1, 2, 3, 0, 4, 5, 0, 6], fill_value=0)

def test_get_item(self):
Expand Down Expand Up @@ -62,6 +66,10 @@ def test_constructor_copy(self):
not_copy.sp_values[:3] = 0
self.assertTrue((self.arr.sp_values[:3] == 0).all())

def test_constructor_match_dtype(self):
res = SparseArray(self.barr_data, dtype=bool)
self.assertEqual(res.dtype, bool)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually construct what it should look like as an expected and use assert_sp_array_equal (which might need the same change to use isnull above)


def test_astype(self):
res = self.arr.astype('f8')
res.sp_values[:3] = 27
Expand All @@ -81,9 +89,19 @@ def _get_base(values):
assert(_get_base(arr2) is _get_base(self.arr))

def test_values_asarray(self):
assert_almost_equal(self.arr.values, self.arr_data)
assert_almost_equal(self.arr.to_dense(), self.arr_data)
assert_almost_equal(self.arr.sp_values, np.asarray(self.arr))
for arr, arr_data in ((self.arr, self.arr_data),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include the issue number as a comment here

(self.barr, self.barr_data)):
vals = arr.values
assert_almost_equal(vals, arr_data)
self.assertEqual(vals.dtype, arr_data.dtype)

dense = arr.to_dense()
assert_almost_equal(dense, arr_data)
self.assertEqual(dense.dtype, arr_data.dtype)

sp_vals = arr.sp_values
assert_almost_equal(sp_vals, np.asarray(arr))
self.assertEqual(sp_vals.dtype, arr_data.dtype)

def test_getitem(self):
def _checkit(i):
Expand Down