@@ -807,16 +807,16 @@ def irr(values, *, raise_exceptions=False, selection_logic=_irr_default_selectio
807
807
if values .ndim not in [1 , 2 ]:
808
808
raise ValueError ("Cashflows must be a 2D array" )
809
809
810
- irr_results = []
811
- for row in values :
810
+ irr_results = np . empty ( values . shape [ 0 ])
811
+ for i , row in enumerate ( values ) :
812
812
# If all values are of the same sign, no solution exists
813
813
# We don't perform any further calculations and exit early
814
814
same_sign = np .all (row > 0 ) if row [0 ] > 0 else np .all (row < 0 )
815
815
if same_sign :
816
816
if raise_exceptions :
817
817
raise NoRealSolutionError ('No real solution exists for IRR since all '
818
818
'cashflows are of the same sign.' )
819
- irr_results . append ( np .nan )
819
+ irr_results [ i ] = np .nan
820
820
821
821
# We aim to solve eirr such that NPV is exactly zero. This can be framed as
822
822
# 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
844
844
845
845
# If no real solution
846
846
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
850
850
# If only one real solution
851
851
elif len (eirr ) == 1 :
852
- irr_results . append ( eirr [0 ])
852
+ irr_results [ i ] = eirr [0 ]
853
853
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 )
858
857
859
858
860
859
def npv (rate , values ):
0 commit comments