MCPcopy
hub / github.com/pandas-dev/pandas / _replace_coerce

Method _replace_coerce

pandas/core/internals/blocks.py:882–937  ·  view source on GitHub ↗

Replace value corresponding to the given boolean array with another value. Parameters ---------- to_replace : object or pattern Scalar to replace or regular expression to match. value : object Replacement object. mask

(
        self,
        to_replace,
        value,
        mask: npt.NDArray[np.bool_],
        inplace: bool = True,
        regex: bool = False,
    )

Source from the content-addressed store, hash-verified

880
881 @final
882 def _replace_coerce(
883 self,
884 to_replace,
885 value,
886 mask: npt.NDArray[np.bool_],
887 inplace: bool = True,
888 regex: bool = False,
889 ) -> list[Block]:
890 """
891 Replace value corresponding to the given boolean array with another
892 value.
893
894 Parameters
895 ----------
896 to_replace : object or pattern
897 Scalar to replace or regular expression to match.
898 value : object
899 Replacement object.
900 mask : np.ndarray[bool]
901 True indicate corresponding element is ignored.
902 inplace : bool, default True
903 Perform inplace modification.
904 regex : bool, default False
905 If true, perform regular expression substitution.
906
907 Returns
908 -------
909 List[Block]
910 """
911 if should_use_regex(regex, to_replace):
912 return self._replace_regex(
913 to_replace,
914 value,
915 inplace=inplace,
916 mask=mask,
917 )
918 else:
919 if value is None:
920 # gh-45601, gh-45836, gh-46634
921 if mask.any():
922 has_ref = self.refs.has_reference()
923 nb = self.astype(np.dtype(object))
924 if not inplace:
925 nb = nb.copy(deep=True)
926 elif inplace and has_ref and nb.refs.has_reference():
927 # no copy in astype and we had refs before
928 nb = nb.copy(deep=True)
929 putmask_inplace(nb.values, mask, value)
930 return [nb]
931 return [self.copy(deep=False)]
932 return self.replace(
933 to_replace=to_replace,
934 value=value,
935 inplace=inplace,
936 mask=mask,
937 )
938
939 # ---------------------------------------------------------------------

Callers 1

replace_listMethod · 0.80

Calls 8

_replace_regexMethod · 0.95
astypeMethod · 0.95
copyMethod · 0.95
replaceMethod · 0.95
should_use_regexFunction · 0.90
putmask_inplaceFunction · 0.90
anyMethod · 0.45
dtypeMethod · 0.45

Tested by

no test coverage detected