Skip to content

BUG: inconsistent creation of IntegerArray from boolean values #31104

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

Closed
jorisvandenbossche opened this issue Jan 17, 2020 · 2 comments · Fixed by #51982
Closed

BUG: inconsistent creation of IntegerArray from boolean values #31104

jorisvandenbossche opened this issue Jan 17, 2020 · 2 comments · Fixed by #51982
Labels
Constructors Series/DataFrame/Index/pd.array Constructors ExtensionArray Extending pandas with custom dtypes or arrays. good first issue NA - MaskedArrays Related to pd.NA and nullable extension arrays Needs Tests Unit test(s) needed to prevent regressions

Comments

@jorisvandenbossche
Copy link
Member

When creating a nullable integer array from boolean values, we are being inconsistent between a boolean numpy array and a object array with bools:

>>> pd.array(np.array([True, False]), dtype="Int64")  
<IntegerArray>
[1, 0]
Length: 2, dtype: Int64

>>> pd.array(np.array([True, False], dtype=object), dtype="Int64")  
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-33-bc23b09ea959> in <module>
----> 1 pd.array(np.array([True, False], dtype=object), dtype="Int64")

~/scipy/pandas/pandas/core/construction.py in array(data, dtype, copy)
    282     if is_extension_array_dtype(dtype):
    283         cls = cast(ExtensionDtype, dtype).construct_array_type()
--> 284         return cls._from_sequence(data, dtype=dtype, copy=copy)
    285 
    286     if dtype is None:

~/scipy/pandas/pandas/core/arrays/integer.py in _from_sequence(cls, scalars, dtype, copy)
    346     @classmethod
    347     def _from_sequence(cls, scalars, dtype=None, copy=False):
--> 348         return integer_array(scalars, dtype=dtype, copy=copy)
    349 
    350     @classmethod

~/scipy/pandas/pandas/core/arrays/integer.py in integer_array(values, dtype, copy)
    127     TypeError if incompatible types
    128     """
--> 129     values, mask = coerce_to_array(values, dtype=dtype, copy=copy)
    130     return IntegerArray(values, mask)
    131 

~/scipy/pandas/pandas/core/arrays/integer.py in coerce_to_array(values, dtype, mask, copy)
    210             "mixed-integer-float",
    211         ]:
--> 212             raise TypeError(f"{values.dtype} cannot be converted to an IntegerDtype")
    213 
    214     elif is_bool_dtype(values) and is_integer_dtype(dtype):

TypeError: object cannot be converted to an IntegerDtype
@jorisvandenbossche jorisvandenbossche added Bug ExtensionArray Extending pandas with custom dtypes or arrays. labels Jan 17, 2020
@jbrockmendel jbrockmendel added Constructors Series/DataFrame/Index/pd.array Constructors NA - MaskedArrays Related to pd.NA and nullable extension arrays labels Dec 21, 2021
@phofl
Copy link
Member

phofl commented Jan 18, 2023

Works now, may need tests

@phofl phofl added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug labels Jan 18, 2023
mpark1994 added a commit to mpark1994/pandas that referenced this issue Feb 8, 2023
@mpark1994
Copy link

hello I created a test for this issue - can someone please review it?

#51246

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Constructors Series/DataFrame/Index/pd.array Constructors ExtensionArray Extending pandas with custom dtypes or arrays. good first issue NA - MaskedArrays Related to pd.NA and nullable extension arrays Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
4 participants