Skip to content

BUG: Cannot astype from IntegerArray to BooleanArray with missing values #31102

@jorisvandenbossche

Description

@jorisvandenbossche

For the boolean -> integer array conversion, I added a special path for that. But astypeing integer to boolean is currently not working:

In [23]: a = pd.array([1, 0, pd.NA])  

In [24]: a  
Out[24]: 
<IntegerArray>
[1, 0, <NA>]
Length: 3, dtype: Int64

In [25]: a.astype("boolean")  
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-41973ed53ee3> in <module>
----> 1 a.astype("boolean")

~/scipy/pandas/pandas/core/arrays/integer.py in astype(self, dtype, copy)
    454             kwargs = {}
    455 
--> 456         data = self.to_numpy(dtype=dtype, **kwargs)
    457         return astype_nansafe(data, dtype, copy=False)
    458 

~/scipy/pandas/pandas/core/arrays/masked.py in to_numpy(self, dtype, copy, na_value)
    124             ):
    125                 raise ValueError(
--> 126                     f"cannot convert to '{dtype}'-dtype NumPy array "
    127                     "with missing values. Specify an appropriate 'na_value' "
    128                     "for this dtype."

ValueError: cannot convert to 'boolean'-dtype NumPy array with missing values. Specify an appropriate 'na_value' for this dtype.

In [26]: a.astype(pd.BooleanDtype()) 
...
ValueError: cannot convert to 'boolean'-dtype NumPy array with missing values. Specify an appropriate 'na_value' for this dtype.

while for conversions to other nullable dtypes, this should be possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugExtensionArrayExtending pandas with custom dtypes or arrays.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions