Skip to content

Commit

Permalink
CLN: requested follow-ups (pandas-dev#29360)
Browse files Browse the repository at this point in the history
* follow-up to pandas-dev#29314

* followup to pandas-dev#28289
  • Loading branch information
jbrockmendel authored and jreback committed Nov 2, 2019
1 parent d48ccb8 commit 276f16d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
12 changes: 12 additions & 0 deletions pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,18 @@ ctypedef fused algos_t:


def _validate_limit(nobs: int, limit=None) -> int:
"""
Check that the `limit` argument is a positive integer.

Parameters
----------
nobs : int
limit : object

Returns
-------
int
"""
if limit is None:
lim = nobs
else:
Expand Down
7 changes: 2 additions & 5 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import numpy as np

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

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

if not self._can_hold_na:
Expand Down
9 changes: 1 addition & 8 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
ensure_float64,
is_datetime64_dtype,
is_datetime64tz_dtype,
is_integer,
is_integer_dtype,
is_numeric_v_string_like,
is_scalar,
Expand Down Expand Up @@ -191,13 +190,7 @@ def interpolate_1d(
)

# default limit is unlimited GH #16282
if limit is None:
# limit = len(xvalues)
pass
elif not is_integer(limit):
raise ValueError("Limit must be an integer")
elif limit < 1:
raise ValueError("Limit must be greater than 0")
limit = algos._validate_limit(nobs=None, limit=limit)

from pandas import Series

Expand Down
25 changes: 16 additions & 9 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,6 @@ def test_timedelta_ops(self):
result = td.to_frame().std()
assert result[0] == expected

# invalid ops
for op in ["skew", "kurt", "sem", "prod", "var"]:
msg = "reduction operation '{}' not allowed for this dtype"
with pytest.raises(TypeError, match=msg.format(op)):
getattr(td, op)()

with pytest.raises(TypeError, match=msg.format(op)):
getattr(td.to_frame(), op)(numeric_only=False)

# GH#10040
# make sure NaT is properly handled by median()
s = Series([Timestamp("2015-02-03"), Timestamp("2015-02-07")])
Expand All @@ -318,6 +309,22 @@ def test_timedelta_ops(self):
)
assert s.diff().median() == timedelta(days=6)

@pytest.mark.parametrize("opname", ["skew", "kurt", "sem", "prod", "var"])
def test_invalid_td64_reductions(self, opname):
s = Series(
[Timestamp("20130101") + timedelta(seconds=i * i) for i in range(10)]
)
td = s.diff()

msg = "reduction operation '{op}' not allowed for this dtype"
msg = msg.format(op=opname)

with pytest.raises(TypeError, match=msg):
getattr(td, opname)()

with pytest.raises(TypeError, match=msg):
getattr(td.to_frame(), opname)(numeric_only=False)

def test_minmax_tz(self, tz_naive_fixture):
tz = tz_naive_fixture
# monotonic
Expand Down

0 comments on commit 276f16d

Please sign in to comment.