From 2722d596dc7f847de11b9367bc08d704f8b8b7df Mon Sep 17 00:00:00 2001 From: Kevin Anderson Date: Mon, 26 Jan 2026 16:49:22 -0500 Subject: [PATCH 1/3] Update test_solarposition.py --- tests/test_solarposition.py | 48 ++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/tests/test_solarposition.py b/tests/test_solarposition.py index 88093e05f9..363a9b47cc 100644 --- a/tests/test_solarposition.py +++ b/tests/test_solarposition.py @@ -142,20 +142,15 @@ def test_spa_python_numpy_physical_dst(expected_solpos, golden): @pytest.mark.parametrize('delta_t', [65.0, None, np.array([65, 65])]) def test_sun_rise_set_transit_spa(expected_rise_set_spa, golden, delta_t): - # solution from NREL SAP web calculator + # solution from NREL SPA web calculator south = Location(-35.0, 0.0, tz='UTC') - times = pd.DatetimeIndex([datetime.datetime(1996, 7, 5, 0), - datetime.datetime(2004, 12, 4, 0)] - ).tz_localize('UTC') - sunrise = pd.DatetimeIndex([datetime.datetime(1996, 7, 5, 7, 8, 15), - datetime.datetime(2004, 12, 4, 4, 38, 57)] - ).tz_localize('UTC').tolist() - sunset = pd.DatetimeIndex([datetime.datetime(1996, 7, 5, 17, 1, 4), - datetime.datetime(2004, 12, 4, 19, 2, 3)] - ).tz_localize('UTC').tolist() - transit = pd.DatetimeIndex([datetime.datetime(1996, 7, 5, 12, 4, 36), - datetime.datetime(2004, 12, 4, 11, 50, 22)] - ).tz_localize('UTC').tolist() + times = pd.to_datetime(["1996-07-05", "2004-12-04"], utc=True) + sunrise = pd.to_datetime(["1996-07-05 07:08:15", "2004-12-04 04:38:57"], + utc=True) + sunset = pd.to_datetime(["1996-07-05 17:01:04", "2004-12-04 19:02:03"], + utc=True) + transit = pd.to_datetime(["1996-07-05 12:04:36", "2004-12-04 11:50:22"], + utc=True) frame = pd.DataFrame({'sunrise': sunrise, 'sunset': sunset, 'transit': transit}, index=times) @@ -169,7 +164,9 @@ def test_sun_rise_set_transit_spa(expected_rise_set_spa, golden, delta_t): for col, data in result.items(): result_rounded[col] = data.dt.round('1s') - assert_frame_equal(frame, result_rounded) + assert_frame_equal(frame, result_rounded, + check_dtype=False # ignore us/ns dtypes + ) # test for Golden, CO compare to NREL SPA result = solarposition.sun_rise_set_transit_spa( @@ -182,7 +179,9 @@ def test_sun_rise_set_transit_spa(expected_rise_set_spa, golden, delta_t): for col, data in result.items(): result_rounded[col] = data.dt.round('s').tz_convert('MST') - assert_frame_equal(expected_rise_set_spa, result_rounded) + assert_frame_equal(expected_rise_set_spa, result_rounded, + check_dtype=False # ignore us/ns dtypes + ) @requires_ephem @@ -726,7 +725,10 @@ def test_hour_angle_with_tricky_timezones(): '2014-09-07 02:00:00', ]).tz_localize('America/Santiago', nonexistent='shift_forward') - with pytest.raises(pytz.exceptions.NonExistentTimeError): + with pytest.raises(( + pytz.exceptions.NonExistentTimeError, # pandas 1.x, 2.x + ValueError, # pandas 3.x + )): times.normalize() # should not raise `pytz.exceptions.NonExistentTimeError` @@ -740,7 +742,10 @@ def test_hour_angle_with_tricky_timezones(): '2014-11-02 02:00:00', ]).tz_localize('America/Havana', ambiguous=[True, True, False, False]) - with pytest.raises(pytz.exceptions.AmbiguousTimeError): + with pytest.raises(( + pytz.exceptions.AmbiguousTimeError, # pandas 1.x, 2.x + ValueError, # pandas 3.x + )): solarposition.hour_angle(times, longitude, eot) @@ -798,8 +803,13 @@ def test_sun_rise_set_transit_geometric(expected_rise_set_spa, golden_mst): @pytest.mark.parametrize('tz', [None, 'utc', 'US/Eastern']) def test__datetime_to_unixtime(tz): # for pandas < 2.0 where "unit" doesn't exist in pd.date_range. note that - # unit of ns is the only option in pandas<2, and the default in pandas 2.x - times = pd.date_range(start='2019-01-01', freq='h', periods=3, tz=tz) + # unit of ns is the only option in pandas<2, and the default in pandas 2.x, + # but the default is us in pandas 3.x + kwargs = dict(start='2019-01-01', freq='h', periods=3, tz=tz) + try: + times = pd.date_range(**kwargs, unit='ns') # pandas 2.x, 3.x + except: + times = pd.date_range(**kwargs) # pandas 1.x expected = times.view(np.int64)/10**9 actual = solarposition._datetime_to_unixtime(times) np.testing.assert_equal(expected, actual) From 659aa24b30367b3bdcadc425e212efd273eaec34 Mon Sep 17 00:00:00 2001 From: Kevin Anderson Date: Mon, 26 Jan 2026 17:33:44 -0500 Subject: [PATCH 2/3] Update test_spa.py --- tests/test_spa.py | 73 ++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 42 deletions(-) diff --git a/tests/test_spa.py b/tests/test_spa.py index 67cab4cbdb..81ae58a2a6 100644 --- a/tests/test_spa.py +++ b/tests/test_spa.py @@ -3,6 +3,7 @@ import warnings import pytest import pvlib +from pvlib.solarposition import _datetime_to_unixtime try: from importlib import reload @@ -19,9 +20,13 @@ import unittest from .conftest import requires_numba +kwargs = dict(start='2003-10-17 12:30:30', periods=1, freq='D') +try: + times = pd.date_range(**kwargs, unit='ns') # pandas 2.x, 3.x +except TypeError: + times = pd.date_range(**kwargs) # pandas 1.x -times = (pd.date_range('2003-10-17 12:30:30', periods=1, freq='D') - .tz_localize('MST')) +times = times.tz_localize('MST') unixtimes = np.array(times.tz_convert('UTC').view(np.int64)*1.0/10**9) lat = 39.742476 @@ -258,35 +263,29 @@ def test_transit_sunrise_sunset(self): # tests at greenwich times = pd.DatetimeIndex([dt.datetime(1996, 7, 5, 0), dt.datetime(2004, 12, 4, 0)] - ).tz_localize( - 'UTC').view(np.int64)*1.0/10**9 + ).tz_localize('UTC') sunrise = pd.DatetimeIndex([dt.datetime(1996, 7, 5, 7, 8, 15), dt.datetime(2004, 12, 4, 4, 38, 57)] - ).tz_localize( - 'UTC').view(np.int64)*1.0/10**9 + ).tz_localize('UTC') sunset = pd.DatetimeIndex([dt.datetime(1996, 7, 5, 17, 1, 4), dt.datetime(2004, 12, 4, 19, 2, 2)] - ).tz_localize( - 'UTC').view(np.int64)*1.0/10**9 - times = np.array(times) - sunrise = np.array(sunrise) - sunset = np.array(sunset) + ).tz_localize('UTC') + times = _datetime_to_unixtime(times) + sunrise = _datetime_to_unixtime(sunrise) + sunset = _datetime_to_unixtime(sunset) result = self.spa.transit_sunrise_sunset(times, -35.0, 0.0, 64.0, 1) assert_almost_equal(sunrise/1e3, result[1]/1e3, 3) assert_almost_equal(sunset/1e3, result[2]/1e3, 3) times = pd.DatetimeIndex([dt.datetime(1994, 1, 2), ] - ).tz_localize( - 'UTC').view(np.int64)*1.0/10**9 + ).tz_localize('UTC') sunset = pd.DatetimeIndex([dt.datetime(1994, 1, 2, 16, 59, 55), ] - ).tz_localize( - 'UTC').view(np.int64)*1.0/10**9 + ).tz_localize('UTC') sunrise = pd.DatetimeIndex([dt.datetime(1994, 1, 2, 7, 8, 12), ] - ).tz_localize( - 'UTC').view(np.int64)*1.0/10**9 - times = np.array(times) - sunrise = np.array(sunrise) - sunset = np.array(sunset) + ).tz_localize('UTC') + times = _datetime_to_unixtime(times) + sunrise = _datetime_to_unixtime(sunrise) + sunset = _datetime_to_unixtime(sunset) result = self.spa.transit_sunrise_sunset(times, 35.0, 0.0, 64.0, 1) assert_almost_equal(sunrise/1e3, result[1]/1e3, 3) assert_almost_equal(sunset/1e3, result[2]/1e3, 3) @@ -297,23 +296,20 @@ def test_transit_sunrise_sunset(self): dt.datetime(2015, 4, 2), dt.datetime(2015, 8, 2), dt.datetime(2015, 12, 2)], - ).tz_localize( - 'UTC').view(np.int64)*1.0/10**9 + ).tz_localize('UTC') sunrise = pd.DatetimeIndex([dt.datetime(2015, 1, 2, 7, 19), dt.datetime(2015, 4, 2, 5, 43), dt.datetime(2015, 8, 2, 5, 1), dt.datetime(2015, 12, 2, 7, 1)], - ).tz_localize( - 'MST').view(np.int64)*1.0/10**9 + ).tz_localize('MST') sunset = pd.DatetimeIndex([dt.datetime(2015, 1, 2, 16, 49), dt.datetime(2015, 4, 2, 18, 24), dt.datetime(2015, 8, 2, 19, 10), dt.datetime(2015, 12, 2, 16, 38)], - ).tz_localize( - 'MST').view(np.int64)*1.0/10**9 - times = np.array(times) - sunrise = np.array(sunrise) - sunset = np.array(sunset) + ).tz_localize('MST') + times = _datetime_to_unixtime(times) + sunrise = _datetime_to_unixtime(sunrise) + sunset = _datetime_to_unixtime(sunset) result = self.spa.transit_sunrise_sunset(times, 39.0, -105.0, 64.0, 1) assert_almost_equal(sunrise/1e3, result[1]/1e3, 1) assert_almost_equal(sunset/1e3, result[2]/1e3, 1) @@ -323,33 +319,26 @@ def test_transit_sunrise_sunset(self): dt.datetime(2015, 4, 2), dt.datetime(2015, 8, 2), dt.datetime(2015, 12, 2)], - ).tz_localize( - 'UTC').view(np.int64)*1.0/10**9 + ).tz_localize('UTC') sunrise = pd.DatetimeIndex([dt.datetime(2015, 1, 2, 7, 36), dt.datetime(2015, 4, 2, 5, 58), dt.datetime(2015, 8, 2, 5, 13), dt.datetime(2015, 12, 2, 7, 17)], - ).tz_localize('Asia/Shanghai').view( - np.int64)*1.0/10**9 + ).tz_localize('Asia/Shanghai') sunset = pd.DatetimeIndex([dt.datetime(2015, 1, 2, 17, 0), dt.datetime(2015, 4, 2, 18, 39), dt.datetime(2015, 8, 2, 19, 28), dt.datetime(2015, 12, 2, 16, 50)], - ).tz_localize('Asia/Shanghai').view( - np.int64)*1.0/10**9 - times = np.array(times) - sunrise = np.array(sunrise) - sunset = np.array(sunset) + ).tz_localize('Asia/Shanghai') + times = _datetime_to_unixtime(times) + sunrise = _datetime_to_unixtime(sunrise) + sunset = _datetime_to_unixtime(sunset) result = self.spa.transit_sunrise_sunset( times, 39.917, 116.383, 64.0, 1) assert_almost_equal(sunrise/1e3, result[1]/1e3, 1) assert_almost_equal(sunset/1e3, result[2]/1e3, 1) def test_earthsun_distance(self): - times = (pd.date_range('2003-10-17 12:30:30', periods=1, freq='D') - .tz_localize('MST')) - unixtimes = times.tz_convert('UTC').view(np.int64)*1.0/10**9 - unixtimes = np.array(unixtimes) result = self.spa.earthsun_distance(unixtimes, 64.0, 1) assert_almost_equal(R, result, 6) From 12c79942359590a2801d809ac3cb5b9cc266d07b Mon Sep 17 00:00:00 2001 From: Kevin Anderson Date: Mon, 26 Jan 2026 17:33:55 -0500 Subject: [PATCH 3/3] lint --- tests/test_solarposition.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_solarposition.py b/tests/test_solarposition.py index 363a9b47cc..68158ee7d6 100644 --- a/tests/test_solarposition.py +++ b/tests/test_solarposition.py @@ -165,7 +165,7 @@ def test_sun_rise_set_transit_spa(expected_rise_set_spa, golden, delta_t): result_rounded[col] = data.dt.round('1s') assert_frame_equal(frame, result_rounded, - check_dtype=False # ignore us/ns dtypes + check_dtype=False # ignore us/ns dtypes ) # test for Golden, CO compare to NREL SPA @@ -808,7 +808,7 @@ def test__datetime_to_unixtime(tz): kwargs = dict(start='2019-01-01', freq='h', periods=3, tz=tz) try: times = pd.date_range(**kwargs, unit='ns') # pandas 2.x, 3.x - except: + except TypeError: times = pd.date_range(**kwargs) # pandas 1.x expected = times.view(np.int64)/10**9 actual = solarposition._datetime_to_unixtime(times)