Skip to content

ENH: should a sequence of integers be a valid input to BooleanArray? #37614

Open
@arw2019

Description

@arw2019

Came across this while working on #37377

xref #28778 for potentially relevant prior discussions

This works on NumPy:

In [10]: np.array([0, 1, 2], dtype=bool)
Out[10]: array([False,  True,  True])

but the following fails with BooleanArray:

In [11]: arr  = pd.array([0, 1, 2], dtype="boolean")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-44c03a9227eb> in <module>
----> 1 arr  = pd.array([0, 1, 2], dtype="boolean")

/workspaces/pandas/pandas/core/construction.py in array(data, dtype, copy)
    296     if is_extension_array_dtype(dtype):
    297         cls = cast(ExtensionDtype, dtype).construct_array_type()
--> 298         return cls._from_sequence(data, dtype=dtype, copy=copy)
    299 
    300     if dtype is None:

/workspaces/pandas/pandas/core/arrays/boolean.py in _from_sequence(cls, scalars, dtype, copy)
    276         if dtype:
    277             assert dtype == "boolean"
--> 278         values, mask = coerce_to_array(scalars, copy=copy)
    279         return BooleanArray(values, mask)
    280 

/workspaces/pandas/pandas/core/arrays/boolean.py in coerce_to_array(values, mask, copy)
    176                 == values_object[~mask_values].astype(float)
    177             ):
--> 178                 raise TypeError("Need to pass bool-like values")
    179 
    180     if mask is None and mask_values is None:

TypeError: Need to pass bool-like values

I'm +0 on supporting NumPy-like behavior here (or we should make sure to document the current behavior)

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConstructorsSeries/DataFrame/Index/pd.array ConstructorsEnhancementNA - MaskedArraysRelated to pd.NA and nullable extension arraysNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions