Skip to content
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

BUG: behaviour of astype_nansafe(.., copy=False) / SparseArray.astype bug #34456

Closed
jorisvandenbossche opened this issue May 29, 2020 · 1 comment · Fixed by #38835
Closed
Labels
Bug Sparse Sparse Data Type
Milestone

Comments

@jorisvandenbossche
Copy link
Member

So our internal astype_nansafe has the following behaviour:

In [53]: pd.core.dtypes.cast.astype_nansafe(np.array([1, 2, 3]), np.dtype("float64")) 
Out[53]: array([1., 2., 3.])

In [54]: pd.core.dtypes.cast.astype_nansafe(np.array([1, 2, 3]), np.dtype("float64"), copy=False) 
Out[54]: array([4.9e-324, 9.9e-324, 1.5e-323])

where it seems it assumes it can take a view if copy=False, which of course depends on the dtype it is casting to:

return arr.view(dtype)

I am not fully sure about the intent of astype_nansafe or whether this was intentional (I would say it is a bug, as I would expect that function to take care of whether copy=False is possible or not).

This also causes a bug in SparseArray's astype with copy=False:

In [59]: pd.arrays.SparseArray([1, 2, 3]).astype(float, copy=False)  
Out[59]: 
[5e-324, 1e-323, 1.5e-323]
Fill: 0.0
IntIndex
Indices: array([0, 1, 2], dtype=int32)
@jbrockmendel
Copy link
Member

ran into this today too. taking a view there definitely seems weird.

@jreback jreback added this to the 1.3 milestone Dec 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Sparse Sparse Data Type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants