Skip to content

Commit 44e6f52

Browse files
phoflim-vinicius
authored and
im-vinicius
committed
ENH: Don't fragment manager if convert is no-op (pandas-dev#53977)
1 parent 8e70ba3 commit 44e6f52

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pandas/core/internals/blocks.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,15 @@ def convert(
480480
return [self.copy()] if copy else [self]
481481

482482
if self.ndim != 1 and self.shape[0] != 1:
483-
return self.split_and_operate(Block.convert, copy=copy, using_cow=using_cow)
483+
blocks = self.split_and_operate(
484+
Block.convert, copy=copy, using_cow=using_cow
485+
)
486+
if all(blk.dtype.kind == "O" for blk in blocks):
487+
# Avoid fragmenting the block if convert is a no-op
488+
if using_cow:
489+
return [self.copy(deep=False)]
490+
return [self.copy()] if copy else [self]
491+
return blocks
484492

485493
values = self.values
486494
if values.ndim == 2:

pandas/tests/frame/methods/test_replace.py

+7
Original file line numberDiff line numberDiff line change
@@ -1592,3 +1592,10 @@ def test_replace_categorical_no_replacement(self):
15921592

15931593
result = df.replace(to_replace=[".", "def"], value=["_", None])
15941594
tm.assert_frame_equal(result, expected)
1595+
1596+
def test_replace_object_splitting(self):
1597+
# GH#53977
1598+
df = DataFrame({"a": ["a"], "b": "b"})
1599+
assert len(df._mgr.blocks) == 1
1600+
df.replace(to_replace=r"^\s*$", value="", inplace=True, regex=True)
1601+
assert len(df._mgr.blocks) == 1

0 commit comments

Comments
 (0)