|
54 | 54 | from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna
|
55 | 55 |
|
56 | 56 | import pandas.core.algorithms as algos
|
57 |
| -from pandas.core.array_algos.putmask import putmask_inplace, putmask_smart |
| 57 | +from pandas.core.array_algos.putmask import ( |
| 58 | + putmask_inplace, |
| 59 | + putmask_smart, |
| 60 | + putmask_without_repeat, |
| 61 | +) |
58 | 62 | from pandas.core.array_algos.replace import compare_or_regex_search, replace_regex
|
59 | 63 | from pandas.core.array_algos.transforms import shift
|
60 | 64 | from pandas.core.arrays import (
|
@@ -1030,38 +1034,7 @@ def putmask(self, mask, new, axis: int = 0) -> List["Block"]:
|
1030 | 1034 | if transpose:
|
1031 | 1035 | new_values = new_values.T
|
1032 | 1036 |
|
1033 |
| - # If the default repeat behavior in np.putmask would go in the |
1034 |
| - # wrong direction, then explicitly repeat and reshape new instead |
1035 |
| - if getattr(new, "ndim", 0) >= 1: |
1036 |
| - new = new.astype(new_values.dtype, copy=False) |
1037 |
| - |
1038 |
| - # we require exact matches between the len of the |
1039 |
| - # values we are setting (or is compat). np.putmask |
1040 |
| - # doesn't check this and will simply truncate / pad |
1041 |
| - # the output, but we want sane error messages |
1042 |
| - # |
1043 |
| - # TODO: this prob needs some better checking |
1044 |
| - # for 2D cases |
1045 |
| - if ( |
1046 |
| - is_list_like(new) |
1047 |
| - and np.any(mask[mask]) |
1048 |
| - and getattr(new, "ndim", 1) == 1 |
1049 |
| - ): |
1050 |
| - if mask[mask].shape[-1] == len(new): |
1051 |
| - # GH 30567 |
1052 |
| - # If length of ``new`` is less than the length of ``new_values``, |
1053 |
| - # `np.putmask` would first repeat the ``new`` array and then |
1054 |
| - # assign the masked values hence produces incorrect result. |
1055 |
| - # `np.place` on the other hand uses the ``new`` values at it is |
1056 |
| - # to place in the masked locations of ``new_values`` |
1057 |
| - np.place(new_values, mask, new) |
1058 |
| - # i.e. new_values[mask] = new |
1059 |
| - elif mask.shape[-1] == len(new) or len(new) == 1: |
1060 |
| - np.putmask(new_values, mask, new) |
1061 |
| - else: |
1062 |
| - raise ValueError("cannot assign mismatch length to masked array") |
1063 |
| - else: |
1064 |
| - np.putmask(new_values, mask, new) |
| 1037 | + putmask_without_repeat(new_values, mask, new) |
1065 | 1038 |
|
1066 | 1039 | # maybe upcast me
|
1067 | 1040 | elif mask.any():
|
|
0 commit comments