Skip to content

Commit e12d6f7

Browse files
committed
Merge pull request #3998 from mtkni/series_clip_timestamp
BUG: Use Series.where rather than np.where in clip
2 parents 2f56a09 + b96c2c2 commit e12d6f7

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ pandas 0.11.1
279279
- Fix index name not propogating when using ``shift``
280280
- Fixed dropna=False being ignored with multi-index stack (:issue:`3997`)
281281
- Fixed flattening of columns when renaming MultiIndex columns DataFrame (:issue:`4004`)
282+
- Fix ``Series.clip`` for datetime series. NA/NaN threshold values will now throw ValueError (:issue:`3996`)
282283

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

pandas/core/series.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,10 @@ def clip_upper(self, threshold):
19841984
-------
19851985
clipped : Series
19861986
"""
1987-
return pa.where(self > threshold, threshold, self)
1987+
if isnull(threshold):
1988+
raise ValueError("Cannot use an NA value as a clip threshold")
1989+
1990+
return self.where((self <= threshold) | isnull(self), threshold)
19881991

19891992
def clip_lower(self, threshold):
19901993
"""
@@ -1998,7 +2001,10 @@ def clip_lower(self, threshold):
19982001
-------
19992002
clipped : Series
20002003
"""
2001-
return pa.where(self < threshold, threshold, self)
2004+
if isnull(threshold):
2005+
raise ValueError("Cannot use an NA value as a clip threshold")
2006+
2007+
return self.where((self >= threshold) | isnull(self), threshold)
20022008

20032009
def dot(self, other):
20042010
"""

pandas/tests/test_series.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,6 +2840,21 @@ def test_clip(self):
28402840
assert_series_equal(result, expected)
28412841
self.assert_(isinstance(expected, Series))
28422842

2843+
def test_clip_types_and_nulls(self):
2844+
2845+
sers = [Series([np.nan, 1.0, 2.0, 3.0]),
2846+
Series([None, 'a', 'b', 'c']),
2847+
Series(pd.to_datetime([np.nan, 1, 2, 3], unit='D'))]
2848+
2849+
for s in sers:
2850+
thresh = s[2]
2851+
l = s.clip_lower(thresh)
2852+
u = s.clip_upper(thresh)
2853+
self.assertEqual(l[notnull(l)].min(), thresh)
2854+
self.assertEqual(u[notnull(u)].max(), thresh)
2855+
self.assertEqual(list(isnull(s)), list(isnull(l)))
2856+
self.assertEqual(list(isnull(s)), list(isnull(u)))
2857+
28432858
def test_valid(self):
28442859
ts = self.ts.copy()
28452860
ts[::2] = np.NaN

0 commit comments

Comments
 (0)