Skip to content

Commit 9f56660

Browse files
authored
Use pandas wrapper functions for the test suite (#1021)
* add wrapper functions for pandas testing asserts * update tests to use assert wrapper instead of importing from pandas.testing * update wrappers * refactor asserts * update basic_chain test values * whatsnew * add test
1 parent d216ebe commit 9f56660

19 files changed

+100
-28
lines changed

docs/sphinx/source/whatsnew/v0.8.0.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ Testing
5353
(:pull:`1006`)
5454
* Update the `data/test_psm3_tmy-2017.csv` datafile to match the updated
5555
NSRDB data. (:issue:`1005`, :pull:`1007`)
56+
* Add wrappers around the pandas assert_X_equal functions to accommodate the
57+
changed API and default precision thresholds in pandas 1.1.0
58+
(:issue:`1018`, :pull:`1021`)
5659

5760
Documentation
5861
~~~~~~~~~~~~~

pvlib/tests/conftest.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,38 @@ def inner(*args, **kwargs):
3535
return inner
3636
return wrapper
3737

38+
39+
def _check_pandas_assert_kwargs(kwargs):
40+
# handles the change in API related to default
41+
# tolerances in pandas 1.1.0. See pvlib GH #1018
42+
if parse_version(pd.__version__) >= parse_version('1.1.0'):
43+
if kwargs.pop('check_less_precise', False):
44+
kwargs['atol'] = 1e-3
45+
kwargs['rtol'] = 1e-3
46+
else:
47+
kwargs['atol'] = 1e-5
48+
kwargs['rtol'] = 1e-5
49+
else:
50+
kwargs.pop('rtol', None)
51+
kwargs.pop('atol', None)
52+
return kwargs
53+
54+
55+
def assert_index_equal(left, right, **kwargs):
56+
kwargs = _check_pandas_assert_kwargs(kwargs)
57+
pd.testing.assert_index_equal(left, right, **kwargs)
58+
59+
60+
def assert_series_equal(left, right, **kwargs):
61+
kwargs = _check_pandas_assert_kwargs(kwargs)
62+
pd.testing.assert_series_equal(left, right, **kwargs)
63+
64+
65+
def assert_frame_equal(left, right, **kwargs):
66+
kwargs = _check_pandas_assert_kwargs(kwargs)
67+
pd.testing.assert_frame_equal(left, right, **kwargs)
68+
69+
3870
# commonly used directories in the tests
3971
TEST_DIR = Path(__file__).parent
4072
DATA_DIR = TEST_DIR.parent / 'data'

pvlib/tests/iotools/test_crn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pandas as pd
2-
from pandas.testing import assert_frame_equal
2+
from conftest import assert_frame_equal
33
import numpy as np
44
from numpy import dtype, nan
55
import pytest

pvlib/tests/iotools/test_solrad.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pandas as pd
2-
from pandas.testing import assert_frame_equal
2+
from conftest import assert_frame_equal
33
import numpy as np
44
from numpy import nan
55

pvlib/tests/test_atmosphere.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from numpy import nan
55
from numpy.testing import assert_allclose
66
import pandas as pd
7-
from pandas.testing import assert_series_equal
7+
from conftest import assert_series_equal
88
import pytest
99

1010
from pvlib import atmosphere

pvlib/tests/test_bifacial.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pandas as pd
22
from datetime import datetime
33
from pvlib.bifacial import pvfactors_timeseries
4-
from conftest import requires_pvfactors
4+
from conftest import requires_pvfactors, assert_series_equal
55
import pytest
66

77

@@ -49,8 +49,8 @@ def test_pvfactors_timeseries():
4949
rho_front_pvrow=rho_front_pvrow, rho_back_pvrow=rho_back_pvrow,
5050
horizon_band_angle=horizon_band_angle)
5151

52-
pd.testing.assert_series_equal(ipoa_inc_front, expected_ipoa_front)
53-
pd.testing.assert_series_equal(ipoa_inc_back, expected_ipoa_back)
52+
assert_series_equal(ipoa_inc_front, expected_ipoa_front)
53+
assert_series_equal(ipoa_inc_back, expected_ipoa_back)
5454

5555

5656
@requires_pvfactors
@@ -97,5 +97,5 @@ def test_pvfactors_timeseries_pandas_inputs():
9797
rho_front_pvrow=rho_front_pvrow, rho_back_pvrow=rho_back_pvrow,
9898
horizon_band_angle=horizon_band_angle)
9999

100-
pd.testing.assert_series_equal(ipoa_inc_front, expected_ipoa_front)
101-
pd.testing.assert_series_equal(ipoa_inc_back, expected_ipoa_back)
100+
assert_series_equal(ipoa_inc_front, expected_ipoa_front)
101+
assert_series_equal(ipoa_inc_back, expected_ipoa_back)

pvlib/tests/test_clearsky.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pytest
99
from numpy.testing import assert_allclose
10-
from pandas.testing import assert_frame_equal, assert_series_equal
10+
from conftest import assert_frame_equal, assert_series_equal
1111

1212
from pvlib.location import Location
1313
from pvlib import clearsky

pvlib/tests/test_conftest.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
2+
import pandas
23

4+
import conftest
35
from conftest import fail_on_pvlib_version
46

57
from pvlib._deprecation import pvlibDeprecationWarning, deprecated
@@ -43,3 +45,38 @@ def test_use_fixture_with_decorator(some_data):
4345
assert some_data == "some data"
4446
with pytest.warns(pvlibDeprecationWarning): # test for deprecation warning
4547
deprec_func(some_data)
48+
49+
50+
@pytest.mark.parametrize('function_name', ['assert_index_equal',
51+
'assert_series_equal',
52+
'assert_frame_equal'])
53+
@pytest.mark.parametrize('pd_version', ['1.0.0', '1.1.0'])
54+
@pytest.mark.parametrize('check_less_precise', [True, False])
55+
def test__check_pandas_assert_kwargs(mocker, monkeypatch,
56+
function_name, pd_version,
57+
check_less_precise):
58+
# test that conftest._check_pandas_assert_kwargs returns appropriate
59+
# kwargs for the assert_x_equal functions
60+
61+
# patch the pandas assert; not interested in actually calling them:
62+
def patched_assert(*args, **kwargs):
63+
pass
64+
65+
monkeypatch.setattr(pandas.testing, function_name, patched_assert)
66+
# then attach a spy to it so we can see what args it is called with:
67+
mocked_function = mocker.spy(pandas.testing, function_name)
68+
# patch pd.__version__ to exercise the two branches in
69+
# conftest._check_pandas_assert_kwargs
70+
monkeypatch.setattr(pandas, '__version__', pd_version)
71+
72+
# finally, run the function and check what args got passed to pandas:
73+
assert_function = getattr(conftest, function_name)
74+
args = [None, None]
75+
assert_function(*args, check_less_precise=check_less_precise)
76+
if pd_version == '1.1.0':
77+
tol = 1e-3 if check_less_precise else 1e-5
78+
expected_kwargs = {'atol': tol, 'rtol': tol}
79+
else:
80+
expected_kwargs = {'check_less_precise': check_less_precise}
81+
82+
mocked_function.assert_called_with(*args, **expected_kwargs)

pvlib/tests/test_iam.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pandas as pd
1010

1111
import pytest
12-
from pandas.testing import assert_series_equal
12+
from conftest import assert_series_equal
1313
from numpy.testing import assert_allclose
1414

1515
from pvlib import iam as _iam

pvlib/tests/test_inverter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import pandas as pd
55

6-
from pandas.testing import assert_series_equal
6+
from conftest import assert_series_equal
77
from numpy.testing import assert_allclose
88

99
from pvlib import inverter

0 commit comments

Comments
 (0)