Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
PERF: replace with list, closes #28084
  • Loading branch information
jbrockmendel committed Aug 22, 2019
commit 6c5326f111592ca0915f9f338428b456b003a01d
22 changes: 21 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,26 @@ def replace(
return [self]
return [self.copy()]

to_replace = [x for x in to_replace if self._can_hold_element(x)]
if not len(to_replace):
# GH#28084 avoid costly checks since we can infer
# that there is nothing to replace in this block
if inplace:
return [self]
return [self.copy()]

if len(to_replace) == 1:
# _can_hold_element checks have reduced this back to the
# scalar case and we can avoid a costly object cast
return self.replace(
to_replace[0],
value,
inplace=inplace,
filter=filter,
regex=regex,
convert=convert,
)

# GH 22083, TypeError or ValueError occurred within error handling
# causes infinite loop. Cast and retry only if not objectblock.
if is_object_dtype(self):
Expand All @@ -751,7 +771,7 @@ def replace(
# try again with a compatible block
block = self.astype(object)
return block.replace(
to_replace=original_to_replace,
to_replace=to_replace,
value=value,
inplace=inplace,
filter=filter,
Expand Down