8000 BUG: pmt() method of financial.py will yield error because the two pa… · numpy/numpy@586620c · GitHub
[go: up one dir, main page]

Skip to content

Commit 586620c

Browse files
committed
BUG: pmt() method of financial.py will yield error because the two parameters passed in to np.where() is evaluated first before going into the function however, the denominator can be zero at that time. I allocated the intermediate result based on different formulas for rate == 0.0 and not equal to and fixes #5046. Also, a easy fix for asocial doesn’t take integer because it assumes passing in an np.array. I convert everything to np.array first and then retrieve the item. Fixed #4701
1 parent cfa095a commit 586620c

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

numpy/lib/financial.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,11 @@ def pmt(rate, nper, pv, fv=0, when='end'):
211211
temp = (1+rate)**nper
212212
miter = np.broadcast(rate, nper, pv, fv, when)
213213
zer = np.zeros(miter.shape)
214-
fact = np.where(rate == zer, nper + zer,
215-
(1 + rate*when)*(temp - 1)/rate + zer)
214+
fact = np.zeros(miter.shape)
215+
numerator = (1 + rate * when) * ( temp - 1)
216+
np.divide(numerator, rate, where = ( rate!= 0), out= fact)
217+
factforZeroRate = nper + zer
218+
np.copyto(fact, factforZeroRate, where = (rate==0))
216219
return -(fv + pv*temp) / fact
217220

218221
def nper(rate, pmt, pv, fv=0, when='end'):

numpy/lib/tests/test_financial.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ def test_fv(self):
4242
def test_pmt(self):
4343
assert_almost_equal(np.pmt(0.08/12, 5*12, 15000),
4444
-304.146, 3)
45+
# This is to test the edge case where rate == 0.0
46+
# it would fail on this case if the fix for checking rate == 0.0 was not there
47+
assert_almost_equal(np.pmt(0.0, 5*12, 15000), -250.0, 3)
48+
# This one tests the case where we use broadcast and arguments passed in are arrays.
49+
assert_almost_equal(np.pmt([[0.0, 0.8],[0.3, 0.8]],[12, 3],[2000, 20000]), np.array([[-166.666, -19311.258],[-626.908, -19311.258]]), 3)
4550

4651
def test_ppmt(self):
4752
np.round(np.ppmt(0.1/12, 1, 60, 55000), 2) == 710.25

numpy/lib/tests/test_type_check.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ def test_basic(self):
9090
assert_(np.isscalar(4.0))
9191

9292

93+
class TestAsscalar(TestCase):
94+
95+
def test_basic(self):
96+
assert_(np.asscalar(4))
97+
assert_(np.asscalar(np.array([3])))
98+
99+
93100
class TestReal(TestCase):
94101

95102
def test_real(self):

numpy/lib/type_check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def asscalar(a):
460460
24
461461
462462
"""
463-
return a.item()
463+
return array(a).item()
464464

465465
#-----------------------------------------------------------------------------
466466

0 commit comments

Comments
 (0)
0