From dcb67f73cbf882f38ea7efc1b7bd5a6acd51d1af Mon Sep 17 00:00:00 2001 From: weikai202 Date: Thu, 11 Dec 2025 02:29:20 -0500 Subject: [PATCH 1/2] Update series.py --- pandas/core/series.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 1ea8bbbaa0cfb..91deadd64a261 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3,7 +3,7 @@ """ from __future__ import annotations - +from pandas._libs import lib from collections.abc import ( Callable, Hashable, @@ -6777,14 +6777,24 @@ def _flex_method(self, other, op, *, level=None, fill_value=None, axis: Axis = 0 if isinstance(other, Series): return self._binop(other, op, level=level, fill_value=fill_value) elif isinstance(other, (np.ndarray, list, tuple, ExtensionArray)): + if isinstance(other, np.ndarray) and other.ndim == 0: + scalar = lib.item_from_zerodim(other) + + if fill_value is not None: + if isna(scalar): + return op(self, fill_value) + self = self.fillna(fill_value) + + return op(self, scalar) + if len(other) != len(self): raise ValueError("Lengths must be equal") other = self._constructor(other, self.index, copy=False) result = self._binop(other, op, level=level, fill_value=fill_value) result._name = res_name return result + elif isinstance(other, ABCDataFrame): - # GH#46179 raise TypeError( f"Series.{op.__name__.strip('_')} does not support a DataFrame " f"`other`. Use df.{op.__name__.strip('_')}(ser) instead." @@ -6794,9 +6804,10 @@ def _flex_method(self, other, op, *, level=None, fill_value=None, axis: Axis = 0 if isna(other): return op(self, fill_value) self = self.fillna(fill_value) - + return op(self, other) + def eq( self, other, From 22865a28bf8aa2bb3e4dcb7be59b358cb2beb64c Mon Sep 17 00:00:00 2001 From: weikai202 Date: Thu, 11 Dec 2025 02:32:59 -0500 Subject: [PATCH 2/2] Update test_clip.py --- pandas/tests/series/methods/test_clip.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pandas/tests/series/methods/test_clip.py b/pandas/tests/series/methods/test_clip.py index 293f034ea322c..7f48fce1b7b0c 100644 --- a/pandas/tests/series/methods/test_clip.py +++ b/pandas/tests/series/methods/test_clip.py @@ -4,7 +4,6 @@ import pytest from pandas.errors import OutOfBoundsDatetime - import pandas as pd from pandas import ( Series, @@ -155,3 +154,18 @@ def test_clip_with_timestamps_and_oob_datetimes_non_nano(self): expected = Series([lower, upper], dtype=dtype) tm.assert_series_equal(result, expected) + + def test_clip_with_scalar_numpy_array_lower(): + s = pd.Series([-1, 2, 3]) + result = s.clip(lower=np.array(0)) + expected = pd.Series([0, 2, 3]) + tm.assert_series_equal(result, expected) + + def test_clip_with_scalar_numpy_array_upper(): + s = pd.Series([-1, 2, 3]) + result = s.clip(upper=np.array(2)) + expected = pd.Series([-1, 2, 2]) + tm.assert_series_equal(result, expected) + + +