Skip to content

BUG: Use Series.where rather than np.where in clip #3998

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 1 commit into from
Jun 25, 2013
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/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ pandas 0.11.1
- Fix index name not propogating when using ``shift``
- Fixed dropna=False being ignored with multi-index stack (:issue:`3997`)
- Fixed flattening of columns when renaming MultiIndex columns DataFrame (:issue:`4004`)
- Fix ``Series.clip`` for datetime series. NA/NaN threshold values will now throw ValueError (:issue:`3996`)

.. _Gh3616: https://github.com/pydata/pandas/issues/3616

Expand Down
10 changes: 8 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1984,7 +1984,10 @@ def clip_upper(self, threshold):
-------
clipped : Series
"""
return pa.where(self > threshold, threshold, self)
if isnull(threshold):
raise ValueError("Cannot use an NA value as a clip threshold")

return self.where((self <= threshold) | isnull(self), threshold)

def clip_lower(self, threshold):
"""
Expand All @@ -1998,7 +2001,10 @@ def clip_lower(self, threshold):
-------
clipped : Series
"""
return pa.where(self < threshold, threshold, self)
if isnull(threshold):
raise ValueError("Cannot use an NA value as a clip threshold")

return self.where((self >= threshold) | isnull(self), threshold)

def dot(self, other):
"""
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2840,6 +2840,21 @@ def test_clip(self):
assert_series_equal(result, expected)
self.assert_(isinstance(expected, Series))

def test_clip_types_and_nulls(self):

sers = [Series([np.nan, 1.0, 2.0, 3.0]),
Series([None, 'a', 'b', 'c']),
Series(pd.to_datetime([np.nan, 1, 2, 3], unit='D'))]

for s in sers:
thresh = s[2]
l = s.clip_lower(thresh)
u = s.clip_upper(thresh)
self.assertEqual(l[notnull(l)].min(), thresh)
self.assertEqual(u[notnull(u)].max(), thresh)
self.assertEqual(list(isnull(s)), list(isnull(l)))
self.assertEqual(list(isnull(s)), list(isnull(u)))

def test_valid(self):
ts = self.ts.copy()
ts[::2] = np.NaN
Expand Down