Skip to content

Commit 8704272

Browse files
committed
Unify ValueError message and correct cython limits
1 parent 6f041e6 commit 8704272

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

pandas/core/internals.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,7 @@ def fillna(self, value, limit=None, inplace=False, downcast=None,
373373
mask = isnull(self.values)
374374
if limit is not None:
375375
if is_integer(limit) and limit < 1:
376-
raise ValueError('`limit` keyword argument must be greater '
377-
'than 0.')
376+
raise ValueError('Limit must be greater than 0')
378377
if self.ndim > 2:
379378
raise NotImplementedError("number of dimensions for 'fillna' "
380379
"is currently limited to 2")

pandas/core/missing.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ def _interp_limit(invalid, fw_limit, bw_limit):
171171

172172
if limit is not None:
173173
if is_integer(limit) and limit < 1:
174-
raise ValueError('`limit` keyword argument must be greater '
175-
'than 0.')
174+
raise ValueError('Limit must be greater than 0')
176175
if limit_direction == 'forward':
177176
violate_limit = sorted(start_nans | set(_interp_limit(invalid,
178177
limit, 0)))

pandas/src/algos_common_helper.pxi.in

+12-12
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def pad_{{name}}(ndarray[{{c_type}}] old, ndarray[{{c_type}}] new,
8383
if limit is None:
8484
lim = nright
8585
else:
86-
if limit < 0:
87-
raise ValueError('Limit must be non-negative')
86+
if limit < 1:
87+
raise ValueError('Limit must be greater than 0')
8888
lim = limit
8989

9090
if nleft == 0 or nright == 0 or new[nright - 1] < old[0]:
@@ -146,8 +146,8 @@ def pad_inplace_{{name}}(ndarray[{{c_type}}] values,
146146
if limit is None:
147147
lim = N
148148
else:
149-
if limit < 0:
150-
raise ValueError('Limit must be non-negative')
149+
if limit < 1:
150+
raise ValueError('Limit must be greater than 0')
151151
lim = limit
152152

153153
val = values[0]
@@ -180,8 +180,8 @@ def pad_2d_inplace_{{name}}(ndarray[{{c_type}}, ndim=2] values,
180180
if limit is None:
181181
lim = N
182182
else:
183-
if limit < 0:
184-
raise ValueError('Limit must be non-negative')
183+
if limit < 1:
184+
raise ValueError('Limit must be greater than 0')
185185
lim = limit
186186

187187
for j in range(K):
@@ -240,8 +240,8 @@ def backfill_{{name}}(ndarray[{{c_type}}] old, ndarray[{{c_type}}] new,
240240
if limit is None:
241241
lim = nright
242242
else:
243-
if limit < 0:
244-
raise ValueError('Limit must be non-negative')
243+
if limit < 1:
244+
raise ValueError('Limit must be greater than 0')
245245
lim = limit
246246

247247
if nleft == 0 or nright == 0 or new[0] > old[nleft - 1]:
@@ -304,8 +304,8 @@ def backfill_inplace_{{name}}(ndarray[{{c_type}}] values,
304304
if limit is None:
305305
lim = N
306306
else:
307-
if limit < 0:
308-
raise ValueError('Limit must be non-negative')
307+
if limit < 1:
308+
raise ValueError('Limit must be greater than 0')
309309
lim = limit
310310

311311
val = values[N - 1]
@@ -338,8 +338,8 @@ def backfill_2d_inplace_{{name}}(ndarray[{{c_type}}, ndim=2] values,
338338
if limit is None:
339339
lim = N
340340
else:
341-
if limit < 0:
342-
raise ValueError('Limit must be non-negative')
341+
if limit < 1:
342+
raise ValueError('Limit must be greater than 0')
343343
lim = limit
344344

345345
for j in range(K):

pandas/tests/series/test_missing.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,11 @@ def test_fillna_raise(self):
296296
self.assertRaises(TypeError, s.fillna, (1, 2))
297297

298298
# related GH 9217, make sure limit is greater than 0
299+
s = Series([1, 2, 3, None])
299300
for limit in [-1, 0]:
300-
s = Series([1, 2, 3, None])
301-
tm.assertRaises(ValueError, lambda: s.fillna(1, limit=limit))
301+
for method in ['backfill', 'bfill', 'pad', 'ffill', None]:
302+
with tm.assertRaises(ValueError):
303+
s.fillna(1, limit=limit, method=method)
302304

303305
def test_fillna_nat(self):
304306
series = Series([0, 1, 2, tslib.iNaT], dtype='M8[ns]')
@@ -871,9 +873,15 @@ def test_interp_limit(self):
871873
assert_series_equal(result, expected)
872874

873875
# GH 9217
876+
methods = ['linear', 'time', 'index', 'values', 'nearest', 'zero',
877+
'slinear', 'quadratic', 'cubic', 'barycentric', 'krogh',
878+
'polynomial', 'spline', 'piecewise_polynomial', None,
879+
'from_derivatives', 'pchip', 'akima']
880+
s = pd.Series([1, 2, np.nan, np.nan, 5])
874881
for limit in [-1, 0]:
875-
s = pd.Series([1, 2, np.nan, np.nan, 5])
876-
tm.assertRaises(ValueError, lambda: s.interpolate(limit=limit))
882+
for method in methods:
883+
with tm.assertRaises(ValueError):
884+
s.interpolate(limit=limit, method=method)
877885

878886
def test_interp_limit_forward(self):
879887
s = Series([1, 3, np.nan, np.nan, np.nan, 11])

0 commit comments

Comments
 (0)