Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from __future__ import annotations

from pandas._libs import lib
from collections.abc import (
Callable,
Hashable,
Expand Down Expand Up @@ -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."
Expand All @@ -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,
Expand Down
16 changes: 15 additions & 1 deletion pandas/tests/series/methods/test_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest

from pandas.errors import OutOfBoundsDatetime

import pandas as pd
from pandas import (
Series,
Expand Down Expand Up @@ -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)



Loading