Skip to content

Commit 5e09d2d

Browse files
committed
Elaborate function usage, add examples and fix bug
1 parent 9e2bd2b commit 5e09d2d

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

pandas/core/dtypes/cast.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,12 @@ def trans(x): # noqa
167167
return result
168168

169169

170-
def maybe_upcast_putmask(result, mask, other):
170+
def maybe_upast_putmask(result, mask, other):
171171
"""
172-
A safe version of putmask that potentially upcasts the result
172+
A safe version of putmask that potentially upcasts the result.
173+
The result is replaced with the first N elements of other,
174+
where N is the number of True values in mask.
175+
If the length of other is shorter than N, other will be repeated.
173176
174177
Parameters
175178
----------
@@ -185,8 +188,18 @@ def maybe_upcast_putmask(result, mask, other):
185188
result : ndarray
186189
changed : boolean
187190
Set to true if the result array was upcasted
191+
192+
Examples
193+
--------
194+
>>> result, _ = maybe_upcast_putmask(np.arange(1,6),
195+
np.array([False, True, False, True, True]), np.arange(21,23))
196+
>>> result
197+
array([1, 21, 3, 22, 21])
188198
"""
189199

200+
if not isinstance(result, np.ndarray):
201+
raise ValueError("The result input must be a ndarray.")
202+
190203
if mask.any():
191204
# Two conversions for date-like dtypes that can't be done automatically
192205
# in np.place:
@@ -241,7 +254,7 @@ def changeit():
241254
# we have an ndarray and the masking has nans in it
242255
else:
243256

244-
if isna(other[mask]).any():
257+
if isna(other).any():
245258
return changeit()
246259

247260
try:

0 commit comments

Comments
 (0)