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,
)
| 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 | # --------------------------------------------------------------------- |
no test coverage detected