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 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
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")
12 changes: 1 addition & 11 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,8 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
if not (isinstance(values, np.ndarray) and values.dtype == np.bool_):
raise TypeError(
"values should be boolean numpy array. Use "
"the 'array' function instead"
"the 'pd.array' function instead"
)
if not (isinstance(mask, np.ndarray) and mask.dtype == np.bool_):
raise TypeError(
"mask should be boolean numpy array. Use "
"the 'array' function instead"
)
if not values.ndim == 1:
raise ValueError("values must be a 1D array")
if not mask.ndim == 1:
raise ValueError("mask must be a 1D array")

self._dtype = BooleanDtype()
super().__init__(values, mask, copy=copy)

Expand Down
9 changes: 2 additions & 7 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,10 @@ 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)):
raise TypeError(
"mask should be boolean numpy array. Use "
"the 'integer_array' function instead"
"the 'pd.array' function instead"
)
super().__init__(values, mask, copy=copy)

Expand Down
11 changes: 11 additions & 0 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ class BaseMaskedArray(ExtensionArray, ExtensionOpsMixin):
_internal_fill_value: Scalar

def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
# values is supposed to already be validated in the subclass
if not (isinstance(mask, np.ndarray) and mask.dtype == np.bool_):
raise TypeError(
"mask should be boolean numpy array. Use "
"the 'pd.array' function instead"
)
if not values.ndim == 1:
raise ValueError("values must be a 1D array")
if not mask.ndim == 1:
raise ValueError("mask must be a 1D array")

if copy:
values = values.copy()
mask = mask.copy()
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/integer/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_integer_array_constructor():
expected = integer_array([1, 2, 3, np.nan], dtype="int64")
tm.assert_extension_array_equal(result, expected)

msg = r".* should be .* numpy array. Use the 'integer_array' function instead"
msg = r".* should be .* numpy array. Use the 'pd.array' function instead"
with pytest.raises(TypeError, match=msg):
IntegerArray(values.tolist(), mask)

Expand Down