Skip to content

Commit 29512dc

Browse files
authored
BUG: Fix series clipping NA issue (#41141)
1 parent 4f3acf1 commit 29512dc

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ Numeric
696696
- Bug in :meth:`DataFrame.transform` would raise ``SpecificationError`` when passed a dictionary and columns were missing; will now raise a ``KeyError`` instead (:issue:`40004`)
697697
- Bug in :meth:`DataFrameGroupBy.rank` giving incorrect results with ``pct=True`` and equal values between consecutive groups (:issue:`40518`)
698698
- Bug in :meth:`Series.count` would result in an ``int32`` result on 32-bit platforms when argument ``level=None`` (:issue:`40908`)
699+
- Bug in :meth:`Series.clip` would fail if series contains NA values and has nullable int or float as a data type (:issue:`40851`)
699700

700701
Conversion
701702
^^^^^^^^^^

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7314,10 +7314,10 @@ def _clip_with_scalar(self, lower, upper, inplace: bool_t = False):
73147314

73157315
with np.errstate(all="ignore"):
73167316
if upper is not None:
7317-
subset = self.to_numpy() <= upper
7317+
subset = (self <= upper).to_numpy()
73187318
result = result.where(subset, upper, axis=None, inplace=False)
73197319
if lower is not None:
7320-
subset = self.to_numpy() >= lower
7320+
subset = (self >= lower).to_numpy()
73217321
result = result.where(subset, lower, axis=None, inplace=False)
73227322

73237323
if np.any(mask):

pandas/tests/series/methods/test_clip.py

+20
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ def test_clip_types_and_nulls(self):
4040
assert list(isna(s)) == list(isna(lower))
4141
assert list(isna(s)) == list(isna(upper))
4242

43+
def test_series_clipping_with_na_values(
44+
self, any_nullable_numeric_dtype, nulls_fixture
45+
):
46+
# Ensure that clipping method can handle NA values with out failing
47+
# GH#40581
48+
49+
s = Series([nulls_fixture, 1.0, 3.0], dtype=any_nullable_numeric_dtype)
50+
s_clipped_upper = s.clip(upper=2.0)
51+
s_clipped_lower = s.clip(lower=2.0)
52+
53+
expected_upper = Series(
54+
[nulls_fixture, 1.0, 2.0], dtype=any_nullable_numeric_dtype
55+
)
56+
expected_lower = Series(
57+
[nulls_fixture, 2.0, 3.0], dtype=any_nullable_numeric_dtype
58+
)
59+
60+
tm.assert_series_equal(s_clipped_upper, expected_upper)
61+
tm.assert_series_equal(s_clipped_lower, expected_lower)
62+
4363
def test_clip_with_na_args(self):
4464
"""Should process np.nan argument as None """
4565
# GH#17276

0 commit comments

Comments
 (0)