Skip to content

BUG: Fix series clipping NA issue #41141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 30, 2021
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ Numeric
- Bug in :meth:`DataFrame.transform` would raise ``SpecificationError`` when passed a dictionary and columns were missing; will now raise a ``KeyError`` instead (:issue:`40004`)
- Bug in :meth:`DataFrameGroupBy.rank` giving incorrect results with ``pct=True`` and equal values between consecutive groups (:issue:`40518`)
- Bug in :meth:`Series.count` would result in an ``int32`` result on 32-bit platforms when argument ``level=None`` (:issue:`40908`)
- Bug in :meth:`Series.clip` would fail if series contains NA values and has nullable int or float as a data type (:issue:`40851`)

Conversion
^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7404,10 +7404,10 @@ def _clip_with_scalar(self, lower, upper, inplace: bool_t = False):

with np.errstate(all="ignore"):
if upper is not None:
subset = self.to_numpy() <= upper
subset = (self <= upper).to_numpy()
result = result.where(subset, upper, axis=None, inplace=False)
if lower is not None:
subset = self.to_numpy() >= lower
subset = (self >= lower).to_numpy()
result = result.where(subset, lower, axis=None, inplace=False)

if np.any(mask):
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/series/methods/test_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ def test_clip_types_and_nulls(self):
assert list(isna(s)) == list(isna(lower))
assert list(isna(s)) == list(isna(upper))

def test_series_clipping_with_na_values(
self, any_nullable_numeric_dtype, nulls_fixture
):
# Ensure that clipping method can handle NA values with out failing
# GH#40581

s = Series([nulls_fixture, 1.0, 3.0], dtype=any_nullable_numeric_dtype)
s_clipped_upper = s.clip(upper=2.0)
s_clipped_lower = s.clip(lower=2.0)

expected_upper = Series(
[nulls_fixture, 1.0, 2.0], dtype=any_nullable_numeric_dtype
)
expected_lower = Series(
[nulls_fixture, 2.0, 3.0], dtype=any_nullable_numeric_dtype
)

tm.assert_series_equal(s_clipped_upper, expected_upper)
tm.assert_series_equal(s_clipped_lower, expected_lower)

def test_clip_with_na_args(self):
"""Should process np.nan argument as None """
# GH#17276
Expand Down