Skip to content

Commit c1790ee

Browse files
committed
Unify ValueError message and correct cython limits
Add stricter integer check with tests
1 parent 6f041e6 commit c1790ee

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

pandas/core/internals.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,10 @@ def fillna(self, value, limit=None, inplace=False, downcast=None,
372372
original_value = value
373373
mask = isnull(self.values)
374374
if limit is not None:
375-
if is_integer(limit) and limit < 1:
376-
raise ValueError('`limit` keyword argument must be greater '
377-
'than 0.')
375+
if not is_integer(limit):
376+
raise ValueError('Limit must be an integer')
377+
if limit < 1:
378+
raise ValueError('Limit must be greater than 0')
378379
if self.ndim > 2:
379380
raise NotImplementedError("number of dimensions for 'fillna' "
380381
"is currently limited to 2")

pandas/core/missing.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,10 @@ def _interp_limit(invalid, fw_limit, bw_limit):
170170
violate_limit = sorted(start_nans)
171171

172172
if limit is not None:
173-
if is_integer(limit) and limit < 1:
174-
raise ValueError('`limit` keyword argument must be greater '
175-
'than 0.')
173+
if not is_integer(limit):
174+
raise ValueError('Limit must be an integer')
175+
if limit < 1:
176+
raise ValueError('Limit must be greater than 0')
176177
if limit_direction == 'forward':
177178
violate_limit = sorted(start_nans | set(_interp_limit(invalid,
178179
limit, 0)))

pandas/src/algos_common_helper.pxi.in

+24-12
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ 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 not util.is_integer_object(limit):
87+
raise ValueError('Limit must be an integer')
88+
if limit < 1:
89+
raise ValueError('Limit must be greater than 0')
8890
lim = limit
8991

9092
if nleft == 0 or nright == 0 or new[nright - 1] < old[0]:
@@ -146,8 +148,10 @@ def pad_inplace_{{name}}(ndarray[{{c_type}}] values,
146148
if limit is None:
147149
lim = N
148150
else:
149-
if limit < 0:
150-
raise ValueError('Limit must be non-negative')
151+
if not util.is_integer_object(limit):
152+
raise ValueError('Limit must be an integer')
153+
if limit < 1:
154+
raise ValueError('Limit must be greater than 0')
151155
lim = limit
152156

153157
val = values[0]
@@ -180,8 +184,10 @@ def pad_2d_inplace_{{name}}(ndarray[{{c_type}}, ndim=2] values,
180184
if limit is None:
181185
lim = N
182186
else:
183-
if limit < 0:
184-
raise ValueError('Limit must be non-negative')
187+
if not util.is_integer_object(limit):
188+
raise ValueError('Limit must be an integer')
189+
if limit < 1:
190+
raise ValueError('Limit must be greater than 0')
185191
lim = limit
186192

187193
for j in range(K):
@@ -240,8 +246,10 @@ def backfill_{{name}}(ndarray[{{c_type}}] old, ndarray[{{c_type}}] new,
240246
if limit is None:
241247
lim = nright
242248
else:
243-
if limit < 0:
244-
raise ValueError('Limit must be non-negative')
249+
if not util.is_integer_object(limit):
250+
raise ValueError('Limit must be an integer')
251+
if limit < 1:
252+
raise ValueError('Limit must be greater than 0')
245253
lim = limit
246254

247255
if nleft == 0 or nright == 0 or new[0] > old[nleft - 1]:
@@ -304,8 +312,10 @@ def backfill_inplace_{{name}}(ndarray[{{c_type}}] values,
304312
if limit is None:
305313
lim = N
306314
else:
307-
if limit < 0:
308-
raise ValueError('Limit must be non-negative')
315+
if not util.is_integer_object(limit):
316+
raise ValueError('Limit must be an integer')
317+
if limit < 1:
318+
raise ValueError('Limit must be greater than 0')
309319
lim = limit
310320

311321
val = values[N - 1]
@@ -338,8 +348,10 @@ def backfill_2d_inplace_{{name}}(ndarray[{{c_type}}, ndim=2] values,
338348
if limit is None:
339349
lim = N
340350
else:
341-
if limit < 0:
342-
raise ValueError('Limit must be non-negative')
351+
if not util.is_integer_object(limit):
352+
raise ValueError('Limit must be an integer')
353+
if limit < 1:
354+
raise ValueError('Limit must be greater than 0')
343355
lim = limit
344356

345357
for j in range(K):

pandas/tests/series/test_missing.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,12 @@ def test_fillna_raise(self):
295295
self.assertRaises(TypeError, s.fillna, [1, 2])
296296
self.assertRaises(TypeError, s.fillna, (1, 2))
297297

298-
# related GH 9217, make sure limit is greater than 0
299-
for limit in [-1, 0]:
300-
s = Series([1, 2, 3, None])
301-
tm.assertRaises(ValueError, lambda: s.fillna(1, limit=limit))
298+
# related GH 9217, make sure limit is an int and greater than 0
299+
s = Series([1, 2, 3, None])
300+
for limit in [-1, 0, 1., 2.]:
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]')
@@ -870,10 +872,16 @@ def test_interp_limit(self):
870872
result = s.interpolate(method='linear', limit=2)
871873
assert_series_equal(result, expected)
872874

873-
# GH 9217
874-
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))
875+
# GH 9217, make sure limit is an int and greater than 0
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])
881+
for limit in [-1, 0, 1., 2.]:
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)