Skip to content

Commit d7efe26

Browse files
authored
REF: put Block replace methods together (#39810)
1 parent 5b8239c commit d7efe26

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

pandas/core/internals/blocks.py

+54-49
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,9 @@ def copy(self, deep: bool = True):
720720
values = values.copy()
721721
return self.make_block_same_class(values, ndim=self.ndim)
722722

723+
# ---------------------------------------------------------------------
724+
# Replace
725+
723726
def replace(
724727
self,
725728
to_replace,
@@ -870,6 +873,57 @@ def _replace_list(
870873
rb = new_rb
871874
return rb
872875

876+
def _replace_coerce(
877+
self,
878+
to_replace,
879+
value,
880+
mask: np.ndarray,
881+
inplace: bool = True,
882+
regex: bool = False,
883+
) -> List[Block]:
884+
"""
885+
Replace value corresponding to the given boolean array with another
886+
value.
887+
888+
Parameters
889+
----------
890+
to_replace : object or pattern
891+
Scalar to replace or regular expression to match.
892+
value : object
893+
Replacement object.
894+
mask : np.ndarray[bool]
895+
True indicate corresponding element is ignored.
896+
inplace : bool, default True
897+
Perform inplace modification.
898+
regex : bool, default False
899+
If true, perform regular expression substitution.
900+
901+
Returns
902+
-------
903+
List[Block]
904+
"""
905+
if mask.any():
906+
if not regex:
907+
nb = self.coerce_to_target_dtype(value)
908+
if nb is self and not inplace:
909+
nb = nb.copy()
910+
putmask_inplace(nb.values, mask, value)
911+
return [nb]
912+
else:
913+
regex = should_use_regex(regex, to_replace)
914+
if regex:
915+
return self._replace_regex(
916+
to_replace,
917+
value,
918+
inplace=inplace,
919+
convert=False,
920+
mask=mask,
921+
)
922+
return self.replace(to_replace, value, inplace=inplace, regex=False)
923+
return [self]
924+
925+
# ---------------------------------------------------------------------
926+
873927
def setitem(self, indexer, value):
874928
"""
875929
Attempt self.values[indexer] = value, possibly creating a new array.
@@ -1400,55 +1454,6 @@ def quantile(
14001454

14011455
return make_block(result, placement=self.mgr_locs, ndim=2)
14021456

1403-
def _replace_coerce(
1404-
self,
1405-
to_replace,
1406-
value,
1407-
mask: np.ndarray,
1408-
inplace: bool = True,
1409-
regex: bool = False,
1410-
) -> List[Block]:
1411-
"""
1412-
Replace value corresponding to the given boolean array with another
1413-
value.
1414-
1415-
Parameters
1416-
----------
1417-
to_replace : object or pattern
1418-
Scalar to replace or regular expression to match.
1419-
value : object
1420-
Replacement object.
1421-
mask : np.ndarray[bool]
1422-
True indicate corresponding element is ignored.
1423-
inplace : bool, default True
1424-
Perform inplace modification.
1425-
regex : bool, default False
1426-
If true, perform regular expression substitution.
1427-
1428-
Returns
1429-
-------
1430-
List[Block]
1431-
"""
1432-
if mask.any():
1433-
if not regex:
1434-
nb = self.coerce_to_target_dtype(value)
1435-
if nb is self and not inplace:
1436-
nb = nb.copy()
1437-
putmask_inplace(nb.values, mask, value)
1438-
return [nb]
1439-
else:
1440-
regex = should_use_regex(regex, to_replace)
1441-
if regex:
1442-
return self._replace_regex(
1443-
to_replace,
1444-
value,
1445-
inplace=inplace,
1446-
convert=False,
1447-
mask=mask,
1448-
)
1449-
return self.replace(to_replace, value, inplace=inplace, regex=False)
1450-
return [self]
1451-
14521457

14531458
class ExtensionBlock(Block):
14541459
"""

0 commit comments

Comments
 (0)