Skip to content

BUG: astype_nansafe with copy=False #38835

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

Merged
merged 2 commits into from
Dec 31, 2020
Merged
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ Sparse
^^^^^^

- Bug in :meth:`DataFrame.sparse.to_coo` raising ``KeyError`` with columns that are a numeric :class:`Index` without a 0 (:issue:`18414`)
-
- Bug in :meth:`SparseArray.astype` with ``copy=False`` producing incorrect results when going from integer dtype to floating dtype (:issue:`34456`)
-

ExtensionArray
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,11 +1133,11 @@ def astype_nansafe(
)
raise ValueError(msg)

if copy or is_object_dtype(arr) or is_object_dtype(dtype):
if copy or is_object_dtype(arr.dtype) or is_object_dtype(dtype):
# Explicit copy, or required since NumPy can't view from / to object.
return arr.astype(dtype, copy=True)

return arr.view(dtype)
return arr.astype(dtype, copy=copy)


def soft_convert_objects(
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,14 @@ def test_astype_nan_raises(self):
with pytest.raises(ValueError, match="Cannot convert non-finite"):
arr.astype(int)

def test_astype_copy_false(self):
# GH#34456 bug caused by using .view instead of .astype in astype_nansafe
arr = SparseArray([1, 2, 3])

result = arr.astype(float, copy=False)
expected = SparseArray([1.0, 2.0, 3.0], fill_value=0.0)
tm.assert_sp_array_equal(result, expected)

def test_set_fill_value(self):
arr = SparseArray([1.0, np.nan, 2.0], fill_value=np.nan)
arr.fill_value = 2
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,17 @@ def test_astype_nansafe(val, typ):
astype_nansafe(arr, dtype=typ)


def test_astype_nansafe_copy_false(any_int_dtype):
# GH#34457 use astype, not view
arr = np.array([1, 2, 3], dtype=any_int_dtype)

dtype = np.dtype("float64")
result = astype_nansafe(arr, dtype, copy=False)

expected = np.array([1.0, 2.0, 3.0], dtype=dtype)
tm.assert_numpy_array_equal(result, expected)


@pytest.mark.parametrize("from_type", [np.datetime64, np.timedelta64])
@pytest.mark.parametrize(
"to_type",
Expand Down