Skip to content

Commit 03e194b

Browse files
committed
Merge branch 'fix_rate_nan'
2 parents 2d7b5ed + d1ee9d4 commit 03e194b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

numpy_financial/_financial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ def rate(nper, pmt, pv, fv, when='end', guess=None, tol=None, maxiter=100):
666666
rn = rnp1
667667
if not close:
668668
# Return nan's in array of the same shape as rn
669-
return np.nan + rn
669+
return default_type(np.nan) + rn
670670
else:
671671
return rn
672672

numpy_financial/tests/test_financial.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ class TestFinancial(object):
1616
def test_rate(self):
1717
assert_almost_equal(npf.rate(10, 0, -3500, 10000), 0.1107, 4)
1818

19+
@pytest.mark.parametrize('number_type', [Decimal, float])
20+
@pytest.mark.parametrize('when', [0, 1, 'end', 'begin'])
21+
def test_rate_with_infeasible_solution(self, number_type, when):
22+
"""
23+
Test when no feasible rate can be found.
24+
25+
Rate will return NaN, if the Newton Raphson method cannot find a
26+
feasible rate within the required tolerance or number of iterations.
27+
This can occur if both `pmt` and `pv` have the same sign, as it is
28+
impossible to repay a loan by making further withdrawls.
29+
"""
30+
result = npf.rate(number_type(12.0),
31+
number_type(400.0),
32+
number_type(10000.0),
33+
number_type(5000.0),
34+
when=when)
35+
is_nan = Decimal.is_nan if number_type == Decimal else numpy.isnan
36+
assert is_nan(result)
37+
1938
def test_rate_decimal(self):
2039
rate = npf.rate(Decimal('10'), Decimal('0'), Decimal('-3500'),
2140
Decimal('10000'))

0 commit comments

Comments
 (0)