Skip to content

Commit 5ecd94c

Browse files
jbrockmendeljreback
authored andcommitted
PERF: avoid copies if possible in fill_binop (pandas-dev#31300)
1 parent 53b0e0e commit 5ecd94c

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

pandas/core/ops/__init__.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,25 @@ def fill_binop(left, right, fill_value):
329329
330330
Notes
331331
-----
332-
Makes copies if fill_value is not None
332+
Makes copies if fill_value is not None and NAs are present.
333333
"""
334-
# TODO: can we make a no-copy implementation?
335334
if fill_value is not None:
336335
left_mask = isna(left)
337336
right_mask = isna(right)
338-
left = left.copy()
339-
right = right.copy()
340337

341338
# one but not both
342339
mask = left_mask ^ right_mask
343-
left[left_mask & mask] = fill_value
344-
right[right_mask & mask] = fill_value
340+
341+
if left_mask.any():
342+
# Avoid making a copy if we can
343+
left = left.copy()
344+
left[left_mask & mask] = fill_value
345+
346+
if right_mask.any():
347+
# Avoid making a copy if we can
348+
right = right.copy()
349+
right[right_mask & mask] = fill_value
350+
345351
return left, right
346352

347353

0 commit comments

Comments
 (0)