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

WIP - Bool Extension Array #22226

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import numpy as np

from pandas.core.arrays.integer import IntegerArray, _IntegerDtype
from pandas.core.dtypes.dtypes import registry
from pandas.util._decorators import cache_readonly


class BooleanDtype(_IntegerDtype):
name = "EABool"
type = np.bool
na_value = np.nan

@cache_readonly
def is_signed_integer(self):
return False

@cache_readonly
def is_unsigned_integer(self):
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what it's worth, you should be able to get is_signed_integer and is_unsigned_integer for free without needing to override since np.dtype(np.bool).kind returns 'b'. This does save performing a single comparison and is more explicit though.


@classmethod
def construct_array_type(cls):
return BooleanArray


class BooleanArray(IntegerArray):

@cache_readonly
def dtype(self):
return BooleanDtype()


def to_boolean_array(values):
"""
Infer and return an integer array of the values.

Parameters
----------
values : 1D list-like

Returns
-------
IntegerArray

Raises
------
TypeError if incompatible types
"""
return BooleanArray(values, dtype='uint8', copy=False)


registry.register(BooleanDtype)
4 changes: 3 additions & 1 deletion pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pandas.core.dtypes.generic import ABCSeries, ABCIndexClass
from pandas.core.dtypes.common import (
is_integer, is_scalar, is_float,
is_bool_dtype,
is_float_dtype,
is_integer_dtype,
is_object_dtype,
Expand Down Expand Up @@ -158,7 +159,8 @@ def coerce_to_array(values, dtype, mask=None, copy=False):
raise TypeError("{} cannot be converted to an IntegerDtype".format(
values.dtype))

elif not (is_integer_dtype(values) or is_float_dtype(values)):
elif not (is_integer_dtype(values) or is_float_dtype(values) or
is_bool_dtype(values)):
raise TypeError("{} cannot be converted to an IntegerDtype".format(
values.dtype))

Expand Down
1 change: 1 addition & 0 deletions pandas/tests/extension/base/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def test_name(self, dtype):

def test_kind(self, dtype):
valid = set('biufcmMOSUV')

if dtype.kind is not None:
assert dtype.kind in valid

Expand Down
Loading