Skip to content

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

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions