Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/sphinx/source/whatsnew/v0.16.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ Documentation

Testing
~~~~~~~
* Remove the ``assert_series_equal`` and ``assert_frame_equal`` wrappers
in ``tests/conftest.py`` and use ``pandas.testing`` functions directly,
with explicit tolerances at every call site. Simplify
``assert_index_equal`` to a thin compatibility shim that passes
``check_freq=False`` only on pandas >= 3.1.0, where the keyword exists.
(:issue:`2263`)


Benchmarking
Expand Down Expand Up @@ -84,3 +90,4 @@ Contributors
* :ghuser:`xiajiahang`
* Jakub Krasuski (:ghuser:`Blizzeq`)
* Dylan Pulver (:ghuser:`dylanpulver`)
* Dresden Goehner (:ghuser:`DresdenGman`)
24 changes: 18 additions & 6 deletions tests/bifacial/test_infinite_sheds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import numpy as np
import pandas as pd
from pvlib.bifacial import infinite_sheds
from tests.conftest import assert_series_equal

import pytest

Expand Down Expand Up @@ -156,8 +155,16 @@ def test_get_irradiance_poa():
gcr, height, pitch, ghi, dhi, dni,
albedo, iam=iam, npoints=npoints)
assert isinstance(res, pd.DataFrame)
assert_series_equal(res['poa_global'], expected_global)
assert_series_equal(res['shaded_fraction'], expected_shaded_fraction)
pd.testing.assert_series_equal(
res['poa_global'],
expected_global,
atol=1e-5,
rtol=1e-5)
pd.testing.assert_series_equal(
res['shaded_fraction'],
expected_shaded_fraction,
atol=1e-5,
rtol=1e-5)
assert all(k in res.columns for k in [
'poa_global', 'poa_diffuse', 'poa_direct', 'poa_ground_diffuse',
'poa_sky_diffuse', 'shaded_fraction'])
Expand Down Expand Up @@ -232,9 +239,14 @@ def test_get_irradiance(vectorize):
expected_shaded_fraction = pd.Series(
result_front['shaded_fraction'], index=ghi.index,
name='shaded_fraction_front')
assert_series_equal(result['poa_global'], expected_poa_global)
assert_series_equal(result['shaded_fraction_front'],
expected_shaded_fraction)
pd.testing.assert_series_equal(
result['poa_global'],
expected_poa_global,
atol=1e-5,
rtol=1e-5)
pd.testing.assert_series_equal(result['shaded_fraction_front'],
expected_shaded_fraction, atol=1e-5,
rtol=1e-5)


def test_get_irradiance_limiting_gcr():
Expand Down
38 changes: 31 additions & 7 deletions tests/bifacial/test_pvfactors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pandas as pd
from datetime import datetime
from pvlib.bifacial.pvfactors import pvfactors_timeseries
from tests.conftest import requires_pvfactors, assert_series_equal
from tests.conftest import requires_pvfactors
import pytest


Expand Down Expand Up @@ -47,8 +47,16 @@ def test_pvfactors_timeseries_list(example_values):
"""Test basic pvfactors functionality with list inputs"""
inputs, outputs = example_values
ipoa_inc_front, ipoa_inc_back, _, _ = pvfactors_timeseries(**inputs)
assert_series_equal(ipoa_inc_front, outputs['expected_ipoa_front'])
assert_series_equal(ipoa_inc_back, outputs['expected_ipoa_back'])
pd.testing.assert_series_equal(
ipoa_inc_front,
outputs['expected_ipoa_front'],
atol=1e-5,
rtol=1e-5)
pd.testing.assert_series_equal(
ipoa_inc_back,
outputs['expected_ipoa_back'],
atol=1e-5,
rtol=1e-5)


@requires_pvfactors
Expand All @@ -61,8 +69,16 @@ def test_pvfactors_timeseries_pandas(example_values):
inputs[key] = pd.Series(inputs[key], index=inputs['timestamps'])

ipoa_inc_front, ipoa_inc_back, _, _ = pvfactors_timeseries(**inputs)
assert_series_equal(ipoa_inc_front, outputs['expected_ipoa_front'])
assert_series_equal(ipoa_inc_back, outputs['expected_ipoa_back'])
pd.testing.assert_series_equal(
ipoa_inc_front,
outputs['expected_ipoa_front'],
atol=1e-5,
rtol=1e-5)
pd.testing.assert_series_equal(
ipoa_inc_back,
outputs['expected_ipoa_back'],
atol=1e-5,
rtol=1e-5)


@requires_pvfactors
Expand All @@ -78,5 +94,13 @@ def test_pvfactors_scalar_orientation(example_values):
outputs['expected_ipoa_back'].iloc[1] = 81.72135884745822

ipoa_inc_front, ipoa_inc_back, _, _ = pvfactors_timeseries(**inputs)
assert_series_equal(ipoa_inc_front, outputs['expected_ipoa_front'])
assert_series_equal(ipoa_inc_back, outputs['expected_ipoa_back'])
pd.testing.assert_series_equal(
ipoa_inc_front,
outputs['expected_ipoa_front'],
atol=1e-5,
rtol=1e-5)
pd.testing.assert_series_equal(
ipoa_inc_back,
outputs['expected_ipoa_back'],
atol=1e-5,
rtol=1e-5)
38 changes: 7 additions & 31 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,41 +38,17 @@ def inner(*args, **kwargs):
return wrapper


def _check_pandas_assert_kwargs(kwargs):
# handles the change in API related to default
# tolerances in pandas 1.1.0. See pvlib GH #1018
if Version(pd.__version__) >= Version('1.1.0'):
if kwargs.pop('check_less_precise', False):
kwargs['atol'] = 1e-3
kwargs['rtol'] = 1e-3
else:
kwargs['atol'] = 1e-5
kwargs['rtol'] = 1e-5
else:
kwargs.pop('rtol', None)
kwargs.pop('atol', None)
return kwargs


def assert_index_equal(left, right, **kwargs):
kwargs = _check_pandas_assert_kwargs(kwargs)
if Version(pd.__version__) >= Version('3.1.0'):
# check_freq was added in pandas 3.1.0
# tests written prior to it, may have not taken it into account
kwargs['check_freq'] = False
# check_freq was added to pandas.testing.assert_index_equal in pandas
# 3.1.0; on older pandas the kwarg does not exist. Only pass it where
# the installed pandas supports it. This wrapper can be removed (and
# call sites switched to pd.testing.assert_index_equal) once pvlib
# requires pandas >= 3.1.0. See #2263 and #2924.
if Version(pd.__version__) >= Version("3.1.0"):
kwargs["check_freq"] = False
pd.testing.assert_index_equal(left, right, **kwargs)


def assert_series_equal(left, right, **kwargs):
kwargs = _check_pandas_assert_kwargs(kwargs)
pd.testing.assert_series_equal(left, right, **kwargs)


def assert_frame_equal(left, right, **kwargs):
kwargs = _check_pandas_assert_kwargs(kwargs)
pd.testing.assert_frame_equal(left, right, **kwargs)


# commonly used directories in the tests
TEST_DIR = Path(__file__).parent
PVLIB_DATA_DIR = Path(pvlib.__path__[0]) / 'data'
Expand Down
18 changes: 9 additions & 9 deletions tests/iotools/test_acis.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
get_acis_prism, get_acis_nrcc, get_acis_mpe,
get_acis_station_data, get_acis_available_stations
)
from tests.conftest import RERUNS, RERUNS_DELAY, assert_frame_equal
from tests.conftest import RERUNS, RERUNS_DELAY
from requests import HTTPError


Expand All @@ -28,15 +28,15 @@ def test_get_acis_prism():
'heating_degree_days', 'growing_degree_days'],
index=pd.to_datetime(['2020-01-01', '2020-01-02']),
)
assert_frame_equal(df, expected)
pd.testing.assert_frame_equal(df, expected, atol=1e-5, rtol=1e-5)
expected_meta = {'latitude': 40, 'longitude': -80, 'altitude': 298.0944}
assert meta == expected_meta

# map_variables=False
df, meta = get_acis_prism(40.001, -80.001, '2020-01-01', '2020-01-02',
map_variables=False)
expected.columns = ['pcpn', 'maxt', 'mint', 'avgt', 'cdd', 'hdd', 'gdd']
assert_frame_equal(df, expected)
pd.testing.assert_frame_equal(df, expected, atol=1e-5, rtol=1e-5)
expected_meta = {'lat': 40, 'lon': -80, 'elev': 298.0944}
assert meta == expected_meta

Expand All @@ -57,15 +57,15 @@ def test_get_acis_nrcc(grid, expected):
'heating_degree_days', 'growing_degree_days'],
index=pd.to_datetime(['2020-01-01']),
)
assert_frame_equal(df, expected)
pd.testing.assert_frame_equal(df, expected, atol=1e-5, rtol=1e-5)
expected_meta = {'latitude': 40., 'longitude': -80., 'altitude': 356.9208}
assert meta == pytest.approx(expected_meta)

# map_variables=False
df, meta = get_acis_nrcc(40.001, -80.001, '2020-01-01', '2020-01-01', grid,
map_variables=False)
expected.columns = ['pcpn', 'maxt', 'mint', 'avgt', 'cdd', 'hdd', 'gdd']
assert_frame_equal(df, expected)
pd.testing.assert_frame_equal(df, expected, atol=1e-5, rtol=1e-5)
expected_meta = {'lat': 40., 'lon': -80., 'elev': 356.9208}
assert meta == pytest.approx(expected_meta)

Expand All @@ -87,15 +87,15 @@ def test_get_acis_mpe():
{'precipitation': [0.4, 0.0]},
index=pd.to_datetime(['2020-01-01', '2020-01-02']),
)
assert_frame_equal(df, expected)
pd.testing.assert_frame_equal(df, expected, atol=1e-5, rtol=1e-5)
expected_meta = {'latitude': 40.0083, 'longitude': -79.9653}
assert meta == expected_meta

# map_variables=False
df, meta = get_acis_mpe(40.001, -80.001, '2020-01-01', '2020-01-02',
map_variables=False)
expected.columns = ['pcpn']
assert_frame_equal(df, expected)
pd.testing.assert_frame_equal(df, expected, atol=1e-5, rtol=1e-5)
expected_meta = {'lat': 40.0083, 'lon': -79.9653}
assert meta == expected_meta

Expand All @@ -116,7 +116,7 @@ def test_get_acis_station_data():
'heating_degree_days', 'growing_degree_days'],
index=pd.to_datetime(['2020-01-10', '2020-01-11', '2020-01-12']),
)
assert_frame_equal(df, expected)
pd.testing.assert_frame_equal(df, expected, atol=1e-5, rtol=1e-5)
expected_meta = {
'uid': 48,
'state': 'IL',
Expand Down Expand Up @@ -177,7 +177,7 @@ def test_get_acis_station_data():
trace_val=-99, map_variables=False)
expected.columns = ['maxt', 'mint', 'avgt', 'obst', 'pcpn', 'snow',
'snwd', 'cdd', 'hdd', 'gdd']
assert_frame_equal(df, expected)
pd.testing.assert_frame_equal(df, expected, atol=1e-5, rtol=1e-5)
expected_meta['lat'] = expected_meta.pop('latitude')
expected_meta['lon'] = expected_meta.pop('longitude')
expected_meta['elev'] = expected_meta.pop('altitude')
Expand Down
33 changes: 26 additions & 7 deletions tests/iotools/test_bsrn.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
RERUNS,
RERUNS_DELAY,
assert_index_equal,
requires_bsrn_credentials,
)
requires_bsrn_credentials)


@pytest.fixture(scope="module")
Expand All @@ -39,7 +38,11 @@ def expected_index():
])
def test_read_bsrn(testfile, expected_index):
data, metadata = read_bsrn(TESTS_DATA_DIR / testfile)
assert_index_equal(expected_index, data.index)
assert_index_equal(
expected_index,
data.index,
atol=1e-5,
rtol=1e-5)
assert 'ghi' in data.columns
assert 'dni_std' in data.columns
assert 'dhi_min' in data.columns
Expand All @@ -50,7 +53,11 @@ def test_read_bsrn(testfile, expected_index):
def test_read_bsrn_buffer(expected_index):
with open(TESTS_DATA_DIR / 'bsrn-lr0100-pay0616.dat') as fbuf:
data, metadata = read_bsrn(fbuf)
assert_index_equal(expected_index, data.index)
assert_index_equal(
expected_index,
data.index,
atol=1e-5,
rtol=1e-5)
assert 'ghi' in data.columns
assert 'dni_std' in data.columns
assert 'dhi_min' in data.columns
Expand All @@ -63,7 +70,11 @@ def test_read_bsrn_logical_records(expected_index):
# and that 0100 is not passed when not specified
data, metadata = read_bsrn(TESTS_DATA_DIR / 'bsrn-pay0616.dat.gz',
logical_records=['0300', '0500'])
assert_index_equal(expected_index, data.index)
assert_index_equal(
expected_index,
data.index,
atol=1e-5,
rtol=1e-5)
assert 'lwu' in data.columns
assert 'uva_global' in data.columns
assert 'uvb_reflected_std' in data.columns
Expand Down Expand Up @@ -107,15 +118,23 @@ def test_get_bsrn(expected_index, bsrn_credentials):
username=username,
password=password,
save_path=temp_dir.name)
assert_index_equal(expected_index, data.index)
assert_index_equal(
expected_index,
data.index,
atol=1e-5,
rtol=1e-5)
assert 'ghi' in data.columns
assert 'dni_std' in data.columns
assert 'dhi_min' in data.columns
assert 'lwd_max' in data.columns
assert 'relative_humidity' in data.columns
# test that a local file was saved and is read correctly
data2, metadata2 = read_bsrn(os.path.join(temp_dir.name, 'tam0616.dat.gz'))
assert_index_equal(expected_index, data2.index)
assert_index_equal(
expected_index,
data2.index,
atol=1e-5,
rtol=1e-5)
assert 'ghi' in data2.columns
temp_dir.cleanup() # explicitly remove temporary directory

Expand Down
11 changes: 7 additions & 4 deletions tests/iotools/test_crn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pvlib.iotools import crn
from tests.conftest import (
TESTS_DATA_DIR,
assert_frame_equal,
assert_index_equal,
)

Expand Down Expand Up @@ -77,13 +76,17 @@ def test_read_crn(testfile, columns_mapped, dtypes):
expected[col] = expected[col].astype(_dtype)

out = crn.read_crn(testfile)
assert_frame_equal(out, expected)
pd.testing.assert_frame_equal(out, expected, atol=1e-5, rtol=1e-5)


# Test map_variables=False returns correct column names
def test_read_crn_map_variables(testfile, columns_unmapped, dtypes):
out = crn.read_crn(testfile, map_variables=False)
assert_index_equal(out.columns, pd.Index(columns_unmapped))
assert_index_equal(
out.columns,
pd.Index(columns_unmapped),
atol=1e-5,
rtol=1e-5)


def test_read_crn_problems(testfile_problems, columns_mapped, dtypes):
Expand All @@ -103,4 +106,4 @@ def test_read_crn_problems(testfile_problems, columns_mapped, dtypes):
if _dtype is not None:
expected[col] = expected[col].astype(_dtype)
out = crn.read_crn(testfile_problems)
assert_frame_equal(out, expected)
pd.testing.assert_frame_equal(out, expected, atol=1e-5, rtol=1e-5)
9 changes: 6 additions & 3 deletions tests/iotools/test_era5.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
RERUNS,
RERUNS_DELAY,
assert_index_equal,
requires_ecmwf_credentials,
)
requires_ecmwf_credentials)


@pytest.fixture
Expand Down Expand Up @@ -71,7 +70,11 @@ def test_get_era5_land(params, expected):
df, meta = pvlib.iotools.get_era5(**params)
assert np.isclose(meta['longitude'], -80.0)
assert np.isclose(meta['latitude'], 40.0)
assert_index_equal(df.index, expected.index)
assert_index_equal(
df.index,
expected.index,
atol=1e-5,
rtol=1e-5)
assert set(df.columns) == {'ghi', 'temp_air'}
assert df.notna().all().all()

Expand Down
Loading