Skip to content

Commit 015917c

Browse files
committed
PERF: improve efficiency of BaseMaskedArray.__setitem__
This somewhat deals with pandas-dev#44172, though that won't be fully resolved until 2D `ExtensionArray`s are supported (per the comments there).
1 parent 7c00e0c commit 015917c

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

pandas/core/apply.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def frame_apply(
8585
args=None,
8686
kwargs=None,
8787
) -> FrameApply:
88-
"""construct and return a row or column based frame apply object"""
88+
"""Construct and return a row- or column-based frame apply object."""
8989
axis = obj._get_axis_number(axis)
9090
klass: type[FrameApply]
9191
if axis == 0:
@@ -693,7 +693,7 @@ def dtypes(self) -> Series:
693693
return self.obj.dtypes
694694

695695
def apply(self) -> DataFrame | Series:
696-
"""compute the results"""
696+
"""Compute the results."""
697697
# dispatch to agg
698698
if is_list_like(self.f):
699699
return self.apply_multiple()
@@ -1011,7 +1011,7 @@ def result_columns(self) -> Index:
10111011
def wrap_results_for_axis(
10121012
self, results: ResType, res_index: Index
10131013
) -> DataFrame | Series:
1014-
"""return the results for the columns"""
1014+
"""Return the results for the columns."""
10151015
result: DataFrame | Series
10161016

10171017
# we have requested to expand

pandas/core/arrays/boolean.py

+12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
BaseMaskedArray,
4545
BaseMaskedDtype,
4646
)
47+
from pandas.core.indexers import check_array_indexer
4748

4849
if TYPE_CHECKING:
4950
import pyarrow
@@ -364,6 +365,17 @@ def map_string(s):
364365

365366
_HANDLED_TYPES = (np.ndarray, numbers.Number, bool, np.bool_)
366367

368+
def __setitem__(self, key, value):
369+
if lib.is_bool(value):
370+
key = check_array_indexer(self, key)
371+
self._data[key] = value
372+
self._mask[key] = False
373+
elif value is libmissing.NA:
374+
key = check_array_indexer(self, key)
375+
self._mask[key] = True
376+
else:
377+
super().__setitem__(key, value)
378+
367379
def _coerce_to_array(self, value) -> tuple[np.ndarray, np.ndarray]:
368380
return coerce_to_array(value)
369381

pandas/core/arrays/floating.py

+12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
NumericArray,
4040
NumericDtype,
4141
)
42+
from pandas.core.indexers import check_array_indexer
4243
from pandas.core.ops import invalid_comparison
4344
from pandas.core.tools.numeric import to_numeric
4445

@@ -275,6 +276,17 @@ def _from_sequence_of_strings(
275276
scalars = to_numeric(strings, errors="raise")
276277
return cls._from_sequence(scalars, dtype=dtype, copy=copy)
277278

279+
def __setitem__(self, key, value):
280+
if lib.is_float(value):
281+
key = check_array_indexer(self, key)
282+
self._data[key] = value
283+
self._mask[key] = False
284+
elif value is libmissing.NA:
285+
key = check_array_indexer(self, key)
286+
self._mask[key] = True
287+
else:
288+
super().__setitem__(key, value)
289+
278290
def _coerce_to_array(self, value) -> tuple[np.ndarray, np.ndarray]:
279291
return coerce_to_array(value, dtype=self.dtype)
280292

pandas/core/arrays/integer.py

+12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
NumericArray,
4747
NumericDtype,
4848
)
49+
from pandas.core.indexers import check_array_indexer
4950
from pandas.core.ops import invalid_comparison
5051
from pandas.core.tools.numeric import to_numeric
5152

@@ -342,6 +343,17 @@ def _from_sequence_of_strings(
342343
scalars = to_numeric(strings, errors="raise")
343344
return cls._from_sequence(scalars, dtype=dtype, copy=copy)
344345

346+
def __setitem__(self, key, value):
347+
if lib.is_integer(value):
348+
key = check_array_indexer(self, key)
349+
self._data[key] = value
350+
self._mask[key] = False
351+
elif value is libmissing.NA:
352+
key = check_array_indexer(self, key)
353+
self._mask[key] = True
354+
else:
355+
super().__setitem__(key, value)
356+
345357
def _coerce_to_array(self, value) -> tuple[np.ndarray, np.ndarray]:
346358
return coerce_to_array(value, dtype=self.dtype)
347359

pandas/core/arrays/masked.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777

7878
class BaseMaskedDtype(ExtensionDtype):
7979
"""
80-
Base class for dtypes for BasedMaskedArray subclasses.
80+
Base class for dtypes for BaseMaskedArray subclasses.
8181
"""
8282

8383
name: str

0 commit comments

Comments
 (0)