Skip to content

Commit 2291b91

Browse files
committed
Updated tests/test_financial.py and nump_financial/_financial.py
- test_financial.py: Altered test cases - _financial.py: Added optional argument `raise_exceptions`
1 parent 223e483 commit 2291b91

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

numpy_financial/_financial.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ def rate(
727727
return rn
728728

729729

730-
def irr(values):
730+
def irr(values, raise_exceptions=False):
731731
r"""Return the Internal Rate of Return (IRR).
732732
733733
This is the "average" periodically compounded rate of return
@@ -743,6 +743,12 @@ def irr(values):
743743
are negative and net "withdrawals" are positive. Thus, for
744744
example, at least the first element of `values`, which represents
745745
the initial investment, will typically be negative.
746+
raise_exceptions: bool, optional
747+
Flag to raise an exception when the irr cannot be computed due to
748+
either having all cashflows of the same sign (NoRealSolutionException) or
749+
having reached the maximum number of iterations (IterationsExceededException).
750+
Set to False as default, thus returning NaNs in the two previous
751+
cases.
746752
747753
Returns
748754
-------
@@ -828,7 +834,9 @@ def irr(values):
828834

829835
# if no real solution
830836
if len(IRR) == 0:
831-
raise NoRealSolutionError("No real solution is found for IRR.")
837+
if raise_exceptions:
838+
raise NoRealSolutionError("No real solution is found for IRR.")
839+
return np.nan
832840

833841
# if only one real solution
834842
if len(IRR) == 1:

tests/test_financial.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -764,13 +764,4 @@ def test_irr_no_real_solution_exception(self):
764764
cashflows = numpy.array([40000, 5000, 8000, 12000, 30000])
765765

766766
with pytest.raises(npf.NoRealSolutionError):
767-
npf.irr(cashflows, raise_exceptions=True)
768-
769-
def test_irr_maximum_iterations_exception(self):
770-
# Test that if the maximum number of iterations is reached,
771-
# then npf.irr returns IterationsExceededException
772-
# when raise_exceptions is set to True.
773-
cashflows = numpy.array([-40000, 5000, 8000, 12000, 30000])
774-
775-
with pytest.raises(npf.IterationsExceededError):
776-
npf.irr(cashflows, maxiter=1, raise_exceptions=True)
767+
npf.irr(cashflows, raise_exceptions=True)

0 commit comments

Comments
 (0)