Skip to content

Commit e902096

Browse files
jbrockmendelMateusz Górski
authored and
Mateusz Górski
committed
CLN: requested follow-ups (pandas-dev#29360)
* follow-up to pandas-dev#29314 * followup to pandas-dev#28289
1 parent 90b4b69 commit e902096

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

pandas/_libs/algos.pyx

+12
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,18 @@ ctypedef fused algos_t:
380380

381381

382382
def _validate_limit(nobs: int, limit=None) -> int:
383+
"""
384+
Check that the `limit` argument is a positive integer.
385+
386+
Parameters
387+
----------
388+
nobs : int
389+
limit : object
390+
391+
Returns
392+
-------
393+
int
394+
"""
383395
if limit is None:
384396
lim = nobs
385397
else:

pandas/core/internals/blocks.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import numpy as np
99

10-
from pandas._libs import NaT, lib, tslib, writers
10+
from pandas._libs import NaT, algos as libalgos, lib, tslib, writers
1111
from pandas._libs.index import convert_scalar
1212
import pandas._libs.internals as libinternals
1313
from pandas._libs.tslibs import Timedelta, conversion
@@ -393,10 +393,7 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
393393

394394
mask = isna(self.values)
395395
if limit is not None:
396-
if not is_integer(limit):
397-
raise ValueError("Limit must be an integer")
398-
if limit < 1:
399-
raise ValueError("Limit must be greater than 0")
396+
limit = libalgos._validate_limit(None, limit=limit)
400397
mask[mask.cumsum(self.ndim - 1) > limit] = False
401398

402399
if not self._can_hold_na:

pandas/core/missing.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
ensure_float64,
1212
is_datetime64_dtype,
1313
is_datetime64tz_dtype,
14-
is_integer,
1514
is_integer_dtype,
1615
is_numeric_v_string_like,
1716
is_scalar,
@@ -191,13 +190,7 @@ def interpolate_1d(
191190
)
192191

193192
# default limit is unlimited GH #16282
194-
if limit is None:
195-
# limit = len(xvalues)
196-
pass
197-
elif not is_integer(limit):
198-
raise ValueError("Limit must be an integer")
199-
elif limit < 1:
200-
raise ValueError("Limit must be greater than 0")
193+
limit = algos._validate_limit(nobs=None, limit=limit)
201194

202195
from pandas import Series
203196

pandas/tests/reductions/test_reductions.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,6 @@ def test_timedelta_ops(self):
299299
result = td.to_frame().std()
300300
assert result[0] == expected
301301

302-
# invalid ops
303-
for op in ["skew", "kurt", "sem", "prod", "var"]:
304-
msg = "reduction operation '{}' not allowed for this dtype"
305-
with pytest.raises(TypeError, match=msg.format(op)):
306-
getattr(td, op)()
307-
308-
with pytest.raises(TypeError, match=msg.format(op)):
309-
getattr(td.to_frame(), op)(numeric_only=False)
310-
311302
# GH#10040
312303
# make sure NaT is properly handled by median()
313304
s = Series([Timestamp("2015-02-03"), Timestamp("2015-02-07")])
@@ -318,6 +309,22 @@ def test_timedelta_ops(self):
318309
)
319310
assert s.diff().median() == timedelta(days=6)
320311

312+
@pytest.mark.parametrize("opname", ["skew", "kurt", "sem", "prod", "var"])
313+
def test_invalid_td64_reductions(self, opname):
314+
s = Series(
315+
[Timestamp("20130101") + timedelta(seconds=i * i) for i in range(10)]
316+
)
317+
td = s.diff()
318+
319+
msg = "reduction operation '{op}' not allowed for this dtype"
320+
msg = msg.format(op=opname)
321+
322+
with pytest.raises(TypeError, match=msg):
323+
getattr(td, opname)()
324+
325+
with pytest.raises(TypeError, match=msg):
326+
getattr(td.to_frame(), opname)(numeric_only=False)
327+
321328
def test_minmax_tz(self, tz_naive_fixture):
322329
tz = tz_naive_fixture
323330
# monotonic

0 commit comments

Comments
 (0)