4
4
from __future__ import annotations
5
5
6
6
from typing import Any
7
- import warnings
8
7
9
8
import numpy as np
10
9
15
14
)
16
15
17
16
from pandas .core .dtypes .cast import (
17
+ can_hold_element ,
18
18
convert_scalar_for_putitemlike ,
19
19
find_common_type ,
20
20
infer_dtype_from ,
21
21
)
22
- from pandas .core .dtypes .common import (
23
- is_float_dtype ,
24
- is_integer_dtype ,
25
- is_list_like ,
26
- )
27
- from pandas .core .dtypes .missing import isna_compat
22
+ from pandas .core .dtypes .common import is_list_like
28
23
29
24
from pandas .core .arrays import ExtensionArray
30
25
@@ -75,7 +70,7 @@ def putmask_smart(values: np.ndarray, mask: npt.NDArray[np.bool_], new) -> np.nd
75
70
`values`, updated in-place.
76
71
mask : np.ndarray[bool]
77
72
Applies to both sides (array like).
78
- new : `new values` either scalar or an array like aligned with `values`
73
+ new : listlike `new values` aligned with `values`
79
74
80
75
Returns
81
76
-------
@@ -89,9 +84,6 @@ def putmask_smart(values: np.ndarray, mask: npt.NDArray[np.bool_], new) -> np.nd
89
84
# we cannot use np.asarray() here as we cannot have conversions
90
85
# that numpy does when numeric are mixed with strings
91
86
92
- if not is_list_like (new ):
93
- new = np .broadcast_to (new , mask .shape )
94
-
95
87
# see if we are only masking values that if putted
96
88
# will work in the current dtype
97
89
try :
@@ -100,27 +92,12 @@ def putmask_smart(values: np.ndarray, mask: npt.NDArray[np.bool_], new) -> np.nd
100
92
# TypeError: only integer scalar arrays can be converted to a scalar index
101
93
pass
102
94
else :
103
- # make sure that we have a nullable type if we have nulls
104
- if not isna_compat (values , nn [0 ]):
105
- pass
106
- elif not (is_float_dtype (nn .dtype ) or is_integer_dtype (nn .dtype )):
107
- # only compare integers/floats
108
- pass
109
- elif not (is_float_dtype (values .dtype ) or is_integer_dtype (values .dtype )):
110
- # only compare integers/floats
111
- pass
112
- else :
113
-
114
- # we ignore ComplexWarning here
115
- with warnings .catch_warnings (record = True ):
116
- warnings .simplefilter ("ignore" , np .ComplexWarning )
117
- nn_at = nn .astype (values .dtype )
118
-
119
- comp = nn == nn_at
120
- if is_list_like (comp ) and comp .all ():
121
- nv = values .copy ()
122
- nv [mask ] = nn_at
123
- return nv
95
+ # We only get to putmask_smart when we cannot hold 'new' in values.
96
+ # The "smart" part of putmask_smart is checking if we can hold new[mask]
97
+ # in values, in which case we can still avoid the need to cast.
98
+ if can_hold_element (values , nn ):
99
+ values [mask ] = nn
100
+ return values
124
101
125
102
new = np .asarray (new )
126
103
0 commit comments