Skip to content

PERF: improve IntegerArray fast constructor #33359

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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions asv_bench/benchmarks/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ def setup(self):
self.values_float = np.array([1.0, 0.0, 1.0, 0.0])
self.values_integer = np.array([1, 0, 1, 0])
self.values_integer_like = [1, 0, 1, 0]
self.data = np.array([True, False, True, False])
self.mask = np.array([False, False, True, False])

def time_constructor(self):
pd.arrays.BooleanArray(self.data, self.mask)

def time_from_bool_array(self):
pd.array(self.values_bool, dtype="boolean")
Expand All @@ -21,3 +26,16 @@ def time_from_integer_like(self):

def time_from_float_array(self):
pd.array(self.values_float, dtype="boolean")


class IntegerArray:
def setup(self):
self.values_integer = np.array([1, 0, 1, 0])
self.data = np.array([1, 2, 3, 4], dtype="int64")
self.mask = np.array([False, False, True, False])

def time_constructor(self):
pd.arrays.IntegerArray(self.data, self.mask)

def time_from_integer_array(self):
pd.array(self.values_integer, dtype="Int64")
4 changes: 2 additions & 2 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,12 @@ def dtype(self) -> _IntegerDtype:
return _dtypes[str(self._data.dtype)]

def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
if not (isinstance(values, np.ndarray) and is_integer_dtype(values.dtype)):
if not (isinstance(values, np.ndarray) and values.dtype.kind in ["i", "u"]):
raise TypeError(
"values should be integer numpy array. Use "
"the 'integer_array' function instead"
)
if not (isinstance(mask, np.ndarray) and is_bool_dtype(mask.dtype)):
if not (isinstance(mask, np.ndarray) and mask.dtype == np.bool_):
Copy link
Member

Choose a reason for hiding this comment

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

this check is common with boolean array (boolean already uses mask.dtype == np.bool_) (and maybe string array constructor should allow a mask). can this check be moved to the base class to avoid duplication.

Also, in boolean array, we check the dim of values and mask, should we also check dim for integer array (may affect perfomance?) or in the base masked array class.

raise TypeError(
"mask should be boolean numpy array. Use "
"the 'integer_array' function instead"
Expand Down