@@ -596,18 +596,22 @@ def replace_list(
596
596
# figure out our mask apriori to avoid repeated replacements
597
597
values = self .as_array ()
598
598
599
- def comp (s , regex = False ):
599
+ def comp (s : Scalar , mask : np . ndarray , regex : bool = False ):
600
600
"""
601
601
Generate a bool array by perform an equality check, or perform
602
602
an element-wise regular expression matching
603
603
"""
604
604
if isna (s ):
605
- return isna ( values )
605
+ return ~ mask
606
606
607
607
s = com .maybe_box_datetimelike (s )
608
- return _compare_or_regex_search (values , s , regex )
608
+ return _compare_or_regex_search (values , s , regex , mask )
609
609
610
- masks = [comp (s , regex ) for s in src_list ]
610
+ # Calculate the mask once, prior to the call of comp
611
+ # in order to avoid repeating the same computations
612
+ mask = ~ isna (values )
613
+
614
+ masks = [comp (s , mask , regex ) for s in src_list ]
611
615
612
616
result_blocks = []
613
617
src_len = len (src_list ) - 1
@@ -1895,7 +1899,7 @@ def _merge_blocks(
1895
1899
1896
1900
1897
1901
def _compare_or_regex_search (
1898
- a : ArrayLike , b : Scalar , regex : bool = False
1902
+ a : ArrayLike , b : Scalar , regex : bool = False , mask : Optional [ ArrayLike ] = None
1899
1903
) -> Union [ArrayLike , bool ]:
1900
1904
"""
1901
1905
Compare two array_like inputs of the same shape or two scalar values
@@ -1908,6 +1912,7 @@ def _compare_or_regex_search(
1908
1912
a : array_like
1909
1913
b : scalar
1910
1914
regex : bool, default False
1915
+ mask : array_like or None (default)
1911
1916
1912
1917
Returns
1913
1918
-------
@@ -1941,7 +1946,7 @@ def _check_comparison_types(
1941
1946
)
1942
1947
1943
1948
# GH#32621 use mask to avoid comparing to NAs
1944
- if isinstance (a , np .ndarray ) and not isinstance (b , np .ndarray ):
1949
+ if mask is None and isinstance (a , np .ndarray ) and not isinstance (b , np .ndarray ):
1945
1950
mask = np .reshape (~ (isna (a )), a .shape )
1946
1951
if isinstance (a , np .ndarray ):
1947
1952
a = a [mask ]
@@ -1953,7 +1958,7 @@ def _check_comparison_types(
1953
1958
1954
1959
result = op (a )
1955
1960
1956
- if isinstance (result , np .ndarray ):
1961
+ if isinstance (result , np .ndarray ) and mask is not None :
1957
1962
# The shape of the mask can differ to that of the result
1958
1963
# since we may compare only a subset of a's or b's elements
1959
1964
tmp = np .zeros (mask .shape , dtype = np .bool_ )
0 commit comments