Skip to content
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

BUG: clip should handle null values #17288

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Other Enhancements
- :func:`DataFrame.add_prefix` and :func:`DataFrame.add_suffix` now accept strings containing the '%' character. (:issue:`17151`)
- `read_*` methods can now infer compression from non-string paths, such as ``pathlib.Path`` objects (:issue:`17206`).
- :func:`pd.read_sas()` now recognizes much more of the most frequently used date (datetime) formats in SAS7BDAT files (:issue:`15871`).
- :func:`Series.clip()` and `DataFrame.clip()` now treat NA values for upper and lower arguments as None instead of raising `ValueError` (:issue:`17276`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add :func: in front of the DataFrame one too.


.. _whatsnew_0210.api_breaking:

Expand Down
10 changes: 6 additions & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4660,9 +4660,6 @@ def _clip_with_one_bound(self, threshold, method, axis, inplace):
if axis is not None:
axis = self._get_axis_number(axis)

if np.any(isna(threshold)):
raise ValueError("Cannot use an NA value as a clip threshold")

# method is self.le for upper bound and self.ge for lower bound
if is_scalar(threshold) and is_number(threshold):
if method.__name__ == 'le':
Expand Down Expand Up @@ -4742,6 +4739,12 @@ def clip(self, lower=None, upper=None, axis=None, inplace=False,

axis = nv.validate_clip_with_axis(axis, args, kwargs)

# GH 17276
if np.any(pd.isnull(lower)):
lower = None
if np.any(pd.isnull(upper)):
upper = None

# GH 2747 (arguments were reversed)
if lower is not None and upper is not None:
if is_scalar(lower) and is_scalar(upper):
Expand All @@ -4758,7 +4761,6 @@ def clip(self, lower=None, upper=None, axis=None, inplace=False,
if upper is not None:
if inplace:
result = self

result = result.clip_upper(upper, axis, inplace=inplace)

return result
Expand Down
23 changes: 7 additions & 16 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1931,22 +1931,13 @@ def test_clip_against_frame(self, axis):
tm.assert_frame_equal(clipped_df[ub_mask], ub[ub_mask])
tm.assert_frame_equal(clipped_df[mask], df[mask])

def test_clip_na(self):
msg = "Cannot use an NA"
with tm.assert_raises_regex(ValueError, msg):
self.frame.clip(lower=np.nan)

with tm.assert_raises_regex(ValueError, msg):
self.frame.clip(lower=[np.nan])

with tm.assert_raises_regex(ValueError, msg):
self.frame.clip(upper=np.nan)

with tm.assert_raises_regex(ValueError, msg):
self.frame.clip(upper=[np.nan])

with tm.assert_raises_regex(ValueError, msg):
self.frame.clip(lower=np.nan, upper=np.nan)
def test_clip_with_na_args(self):
"""Should process np.nan argument as None """
# GH # 17276
self.frame.clip(np.nan)
self.frame.clip(upper=[1, 2, np.nan])
self.frame.clip(lower=[1, np.nan, 3])
self.frame.clip(upper=np.nan, lower=np.nan)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to check the result of these function calls as part of the test.


# Matrix-like

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,16 @@ def test_clip_types_and_nulls(self):
assert list(isna(s)) == list(isna(l))
assert list(isna(s)) == list(isna(u))

def test_clip_with_na_args(self):
"""Should process np.nan argument as None """
# GH # 17276
s = Series([1, 2, 3])

s.clip(np.nan)
s.clip(upper=[1, 2, np.nan])
s.clip(lower=[1, np.nan, 3])
s.clip(upper=np.nan, lower=np.nan)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above


def test_clip_against_series(self):
# GH #6966

Expand Down