Skip to content

Commit

Permalink
TST/MAINT: Use np for importing in tests
Browse files Browse the repository at this point in the history
The financial functions have been deprecated
for years now and we are using a version
of NumPy that no longer has them.
  • Loading branch information
Kai-Striega committed Mar 26, 2024
1 parent 9559066 commit 9875f10
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions tests/test_financial.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import math
from decimal import Decimal

# Don't use 'import numpy as np', to avoid accidentally testing
# the versions in numpy instead of numpy_financial.
import numpy
import numpy as np
import pytest
from hypothesis import given, settings
Expand Down Expand Up @@ -190,7 +187,7 @@ def test_rate_with_infeasible_solution(self, number_type, when):
number_type(5000.0),
when=when,
)
is_nan = Decimal.is_nan if number_type == Decimal else numpy.isnan
is_nan = Decimal.is_nan if number_type == Decimal else np.isnan
assert is_nan(result)

def test_rate_decimal(self):
Expand All @@ -202,7 +199,7 @@ def test_gh48(self):
Test the correct result is returned with only infeasible solutions
converted to nan.
"""
des = [-0.39920185, -0.02305873, -0.41818459, 0.26513414, numpy.nan]
des = [-0.39920185, -0.02305873, -0.41818459, 0.26513414, np.nan]
nper = 2
pmt = 0
pv = [-593.06, -4725.38, -662.05, -428.78, -13.65]
Expand Down Expand Up @@ -256,22 +253,22 @@ def test_npv(self):
def test_fuzz(self, rates, values):
npf.npv(rates, values)

@pytest.mark.parametrize("rates", ([[1, 2, 3]], numpy.empty(shape=(1, 1, 1))))
@pytest.mark.parametrize("rates", ([[1, 2, 3]], np.empty(shape=(1, 1, 1))))
def test_invalid_rates_shape(self, rates):
cashflows = [1, 2, 3]
with pytest.raises(ValueError):
npf.npv(rates, cashflows)

@pytest.mark.parametrize("cf", ([[[1, 2, 3]]], numpy.empty(shape=(1, 1, 1))))
@pytest.mark.parametrize("cf", ([[[1, 2, 3]]], np.empty(shape=(1, 1, 1))))
def test_invalid_cashflows_shape(self, cf):
rates = [1, 2, 3]
with pytest.raises(ValueError):
npf.npv(rates, cf)

@pytest.mark.parametrize("rate", (-1, -1.0))
def test_rate_of_negative_one_returns_nan(self, rate):
cashflow = numpy.arange(5)
assert numpy.isnan(npf.npv(rate, cashflow))
cashflow = np.arange(5)
assert np.isnan(npf.npv(rate, cashflow))


class TestPmt:
Expand All @@ -290,7 +287,7 @@ def test_pmt_broadcast(self):
# Test the case where we use broadcast and
# the arguments passed in are arrays.
res = npf.pmt([[0.0, 0.8], [0.3, 0.8]], [12, 3], [2000, 20000])
tgt = numpy.array([[-166.66667, -19311.258], [-626.90814, -19311.258]])
tgt = np.array([[-166.66667, -19311.258], [-626.90814, -19311.258]])
assert_allclose(res, tgt)

def test_pmt_decimal_simple(self):
Expand All @@ -312,7 +309,7 @@ def test_pmt_decimal_broadcast(self):
[Decimal("12"), Decimal("3")],
[Decimal("2000"), Decimal("20000")],
)
tgt = numpy.array(
tgt = np.array(
[
[
Decimal("-166.6666666666666666666666667"),
Expand Down Expand Up @@ -357,7 +354,7 @@ def test_mirr(self, values, finance_rate, reinvest_rate, expected):
difference = 10**-decimal_part_len
assert_allclose(result, expected, atol=difference)
else:
assert_(numpy.isnan(result))
assert_(np.isnan(result))

def test_mirr_no_real_solution_exception(self):
# Test that if there is no solution because all the cashflows
Expand All @@ -378,16 +375,16 @@ def test_basic_values(self):
)

def test_gh_18(self):
with numpy.errstate(divide="raise"):
with np.errstate(divide="raise"):
assert_allclose(
npf.nper(0.1, 0, -500, 1500),
11.52670461, # Computed using Google Sheet's NPER
)

def test_infinite_payments(self):
with numpy.errstate(divide="raise"):
with np.errstate(divide="raise"):
result = npf.nper(0, -0.0, 1000)
assert_(result == numpy.inf)
assert_(result == np.inf)

def test_no_interest(self):
assert_(npf.nper(0, -100, 1000) == 10)
Expand Down Expand Up @@ -516,7 +513,7 @@ def test_invalid_per(self, args):
],
)
def test_broadcast(self, when, desired):
args = (0.1 / 12, numpy.arange(1, 5), 24, 2000, 0)
args = (0.1 / 12, np.arange(1, 5), 24, 2000, 0)
result = npf.ppmt(*args) if when is None else npf.ppmt(*args, when)
assert_allclose(result, desired, rtol=1e-5)

Expand Down Expand Up @@ -546,7 +543,7 @@ def test_broadcast(self, when, desired):
def test_broadcast_decimal(self, when, desired):
args = (
Decimal("0.1") / Decimal("12"),
numpy.arange(1, 5),
np.arange(1, 5),
Decimal("24"),
Decimal("2000"),
Decimal("0"),
Expand Down Expand Up @@ -608,7 +605,7 @@ def test_when_is_end_decimal(self, when):
@pytest.mark.parametrize(
"per, desired",
[
(0, numpy.nan),
(0, np.nan),
(1, 0),
(2, -594.107158),
(3, -592.971592),
Expand All @@ -618,15 +615,15 @@ def test_gh_17(self, per, desired):
# All desired results computed using Google Sheet's IPMT
rate = 0.001988079518355057
result = npf.ipmt(rate, per, 360, 300000, when="begin")
if numpy.isnan(desired):
assert numpy.isnan(result)
if np.isnan(desired):
assert np.isnan(result)
else:
assert_allclose(result, desired, rtol=1e-6)

def test_broadcasting(self):
desired = [numpy.nan, -16.66666667, -16.03647345, -15.40102862, -14.76028842]
desired = [np.nan, -16.66666667, -16.03647345, -15.40102862, -14.76028842]
assert_allclose(
npf.ipmt(0.1 / 12, numpy.arange(5), 24, 2000),
npf.ipmt(0.1 / 12, np.arange(5), 24, 2000),
desired,
rtol=1e-6,
)
Expand All @@ -649,10 +646,10 @@ def test_decimal_broadcasting(self):
def test_0d_inputs(self):
args = (0.1 / 12, 1, 24, 2000)
# Scalar inputs should return a scalar.
assert numpy.isscalar(npf.ipmt(*args))
args = (numpy.array(args[0]),) + args[1:]
assert np.isscalar(npf.ipmt(*args))
args = (np.array(args[0]),) + args[1:]
# 0d array inputs should return a scalar.
assert numpy.isscalar(npf.ipmt(*args))
assert np.isscalar(npf.ipmt(*args))


class TestFv:
Expand Down Expand Up @@ -731,7 +728,7 @@ def test_npv_irr_congruence(self):
# a series of cashflows to be zero, so we should have
#
# NPV(IRR(x), x) = 0.
cashflows = numpy.array([-40000, 5000, 8000, 12000, 30000])
cashflows = np.array([-40000, 5000, 8000, 12000, 30000])
assert_allclose(
npf.npv(npf.irr(cashflows), cashflows),
0,
Expand Down Expand Up @@ -769,7 +766,7 @@ def test_trailing_zeros(self):
)
def test_numpy_gh_6744(self, v):
# Test that if there is no solution then npf.irr returns nan.
assert numpy.isnan(npf.irr(v))
assert np.isnan(npf.irr(v))

def test_gh_15(self):
v = [
Expand Down Expand Up @@ -806,13 +803,13 @@ def test_gh_15(self):
1.3133070599585015e-313,
]
result = npf.irr(v)
assert numpy.isfinite(result)
assert np.isfinite(result)
# Very rough approximation taken from the issue.
desired = -0.9999999990596069
assert_allclose(result, desired, rtol=1e-9)

def test_gh_39(self):
cashflows = numpy.array(
cashflows = np.array(
[
-217500.0,
-217500.0,
Expand Down Expand Up @@ -854,7 +851,7 @@ def test_irr_no_real_solution_exception(self):
# Test that if there is no solution because all the cashflows
# have the same sign, then npf.irr returns NoRealSolutionException
# when raise_exceptions is set to True.
cashflows = numpy.array([40000, 5000, 8000, 12000, 30000])
cashflows = np.array([40000, 5000, 8000, 12000, 30000])

with pytest.raises(npf.NoRealSolutionError):
npf.irr(cashflows, raise_exceptions=True)

0 comments on commit 9875f10

Please sign in to comment.