@@ -7989,33 +7989,22 @@ def _clip_with_scalar(self, lower, upper, inplace: bool_t = False):
|
7989 | 7989 | ):
|
7990 | 7990 | raise ValueError("Cannot use an NA value as a clip threshold")
|
7991 | 7991 |
|
7992 |
| -mgr = self._mgr |
| 7992 | +result = self |
| 7993 | +mask = self.isna() |
7993 | 7994 |
|
7994 |
| -if inplace: |
7995 |
| -# cond (for putmask) identifies values to be updated. |
7996 |
| -# exclude boundary as values at the boundary should be no-ops. |
7997 |
| -if upper is not None: |
7998 |
| -cond = self > upper |
7999 |
| -mgr = mgr.putmask(mask=cond, new=upper, align=False) |
8000 |
| -if lower is not None: |
8001 |
| -cond = self < lower |
8002 |
| -mgr = mgr.putmask(mask=cond, new=lower, align=False) |
8003 |
| -else: |
8004 |
| -# cond (for where) identifies values to be left as-is. |
8005 |
| -# include boundary as values at the boundary should be no-ops. |
8006 |
| -mask = isna(self) |
8007 |
| -if upper is not None: |
8008 |
| -cond = mask | (self <= upper) |
8009 |
| -mgr = mgr.where(other=upper, cond=cond, align=False) |
8010 |
| -if lower is not None: |
8011 |
| -cond = mask | (self >= lower) |
8012 |
| -mgr = mgr.where(other=lower, cond=cond, align=False) |
8013 |
| - |
8014 |
| -result = self._constructor(mgr) |
8015 |
| -if inplace: |
8016 |
| -return self._update_inplace(result) |
8017 |
| -else: |
8018 |
| -return result.__finalize__(self) |
| 7995 | +if lower is not None: |
| 7996 | +cond = mask | (self >= lower) |
| 7997 | +result = result.where( |
| 7998 | +cond, lower, inplace=inplace |
| 7999 | +) # type: ignore[assignment] |
| 8000 | +if upper is not None: |
| 8001 | +cond = mask | (self <= upper) |
| 8002 | +result = self if inplace else result |
| 8003 | +result = result.where( |
| 8004 | +cond, upper, inplace=inplace |
| 8005 | +) # type: ignore[assignment] |
| 8006 | + |
| 8007 | +return result |
8019 | 8008 |
|
8020 | 8009 | @final
|
8021 | 8010 | def _clip_with_one_bound(self, threshold, method, axis, inplace):
|
@@ -9629,6 +9618,12 @@ def _where(
|
9629 | 9618 | for _dt in cond.dtypes:
|
9630 | 9619 | if not is_bool_dtype(_dt):
|
9631 | 9620 | raise ValueError(msg.format(dtype=_dt))
|
| 9621 | +if cond._mgr.any_extension_types: |
| 9622 | +# GH51574: avoid object ndarray conversion later on |
| 9623 | +cond = cond._constructor( |
| 9624 | +cond.to_numpy(dtype=bool, na_value=fill_value), |
| 9625 | +**cond._construct_axes_dict(), |
| 9626 | +) |
9632 | 9627 | else:
|
9633 | 9628 | # GH#21947 we have an empty DataFrame/Series, could be object-dtype
|
9634 | 9629 | cond = cond.astype(bool)
|
|
0 commit comments