diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2016-02-29 10:52:17 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2016-02-29 10:52:17 -0700 |
commit | 682a6910ad8be25afc2e449f12b87c1c291dd1d1 (patch) | |
tree | a6a7bf8de4ad3291be6048a9377226cdbe7bb273 | |
parent | 1bd18cd7ea1ed83243777c1f52f369dfb9048e6c (diff) | |
parent | 3fdf3270e6f47e65d8a8b4d995fc21f9b64650ee (diff) | |
download | numpy-682a6910ad8be25afc2e449f12b87c1c291dd1d1.tar.gz |
Merge pull request #7332 from simongibbons/irr_empty_check
Check for no solution in np.irr Fixes #6744
-rw-r--r-- | numpy/lib/financial.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_financial.py | 7 |
2 files changed, 7 insertions, 2 deletions
diff --git a/numpy/lib/financial.py b/numpy/lib/financial.py index c42424da1..f1a1a4287 100644 --- a/numpy/lib/financial.py +++ b/numpy/lib/financial.py @@ -651,7 +651,7 @@ def irr(values): """ res = np.roots(values[::-1]) mask = (res.imag == 0) & (res.real > 0) - if res.size == 0: + if not mask.any(): return np.nan res = res[mask].real # NPV(rate) = 0 can have more than one solution so we return diff --git a/numpy/lib/tests/test_financial.py b/numpy/lib/tests/test_financial.py index baa785424..cc8ba55e5 100644 --- a/numpy/lib/tests/test_financial.py +++ b/numpy/lib/tests/test_financial.py @@ -3,7 +3,7 @@ from __future__ import division, absolute_import, print_function import numpy as np from numpy.testing import ( run_module_suite, TestCase, assert_, assert_almost_equal, - assert_allclose + assert_allclose, assert_equal ) @@ -26,6 +26,11 @@ class TestFinancial(TestCase): v = [-5, 10.5, 1, -8, 1] assert_almost_equal(np.irr(v), 0.0886, 2) + # Test that if there is no solution then np.irr returns nan + # Fixes gh-6744 + v = [-1, -2, -3] + assert_equal(np.irr(v), np.nan) + def test_pv(self): assert_almost_equal(np.pv(0.07, 20, 12000, 0), -127128.17, 2) |