Skip to content

Commit 18d23f9

Browse files
committed
MAINT: Changed IRR result from list to np.array
Altered structure of IRR function to utilise a np.array to store IRR calculated, rather than a list. Also made small fixes in indentation.
1 parent a753576 commit 18d23f9

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

numpy_financial/_financial.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -807,16 +807,16 @@ def irr(values, *, raise_exceptions=False, selection_logic=_irr_default_selectio
807807
if values.ndim not in [1, 2]:
808808
raise ValueError("Cashflows must be a 2D array")
809809

810-
irr_results = []
811-
for row in values:
810+
irr_results = np.empty(values.shape[0])
811+
for i, row in enumerate(values):
812812
# If all values are of the same sign, no solution exists
813813
# We don't perform any further calculations and exit early
814814
same_sign = np.all(row > 0) if row[0] > 0 else np.all(row < 0)
815815
if same_sign:
816816
if raise_exceptions:
817817
raise NoRealSolutionError('No real solution exists for IRR since all '
818818
'cashflows are of the same sign.')
819-
irr_results.append(np.nan)
819+
irr_results[i] = np.nan
820820

821821
# We aim to solve eirr such that NPV is exactly zero. This can be framed as
822822
# simply finding the closest root of a polynomial to a given initial guess
@@ -844,17 +844,16 @@ def irr(values, *, raise_exceptions=False, selection_logic=_irr_default_selectio
844844

845845
# If no real solution
846846
if len(eirr) == 0:
847-
if raise_exceptions:
848-
raise NoRealSolutionError("No real solution is found for IRR.")
849-
irr_results.append(np.nan)
847+
if raise_exceptions:
848+
raise NoRealSolutionError("No real solution is found for IRR.")
849+
irr_results[i] = np.nan
850850
# If only one real solution
851851
elif len(eirr) == 1:
852-
irr_results.append(eirr[0])
852+
irr_results[i] = eirr[0]
853853
else:
854-
eirr = selection_logic(eirr)
855-
irr_results.append(eirr)
856-
857-
return _ufunc_like(np.array(irr_results))
854+
irr_results[i] = selection_logic(eirr)
855+
856+
return _ufunc_like(irr_results)
858857

859858

860859
def npv(rate, values):

0 commit comments

Comments
 (0)