diff --git a/docs/sphinx/source/whatsnew/v0.15.2.rst b/docs/sphinx/source/whatsnew/v0.15.2.rst index 37f8692280..ac56307b23 100644 --- a/docs/sphinx/source/whatsnew/v0.15.2.rst +++ b/docs/sphinx/source/whatsnew/v0.15.2.rst @@ -14,6 +14,9 @@ Deprecations Bug fixes ~~~~~~~~~ +* Added test coverage for :py:func:`pvlib.irradiance.dirint` with + ``np.array`` and ``pd.Series`` inputs. + (:issue:`2751`, :pull:`2752`) * Corrects a bug in :py:func:`pvlib.temperature.fuentes`. If inputs were data type integer, users can expect modeled cell temperature values to increase slightly. diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 5a4a76df25..46b3485afe 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1964,9 +1964,8 @@ def dirint(ghi, solar_zenith, times, pressure=101325., use_delta_kt_prime=True, Returns ------- - dni : array-like - The modeled direct normal irradiance, as provided by the - DIRINT model. [Wm⁻²] + dni : pd.Series + Estimated direct normal irradiance. [Wm⁻²] Notes ----- @@ -1984,6 +1983,9 @@ def dirint(ghi, solar_zenith, times, pressure=101325., use_delta_kt_prime=True, SERI/TR-215-3087, Golden, CO: Solar Energy Research Institute, 1987. """ + if use_delta_kt_prime and len(times) < 2: + raise ValueError('The DIRINT model requires at least two times') + disc_out = disc(ghi, solar_zenith, times, pressure=pressure, min_cos_zenith=min_cos_zenith, max_zenith=max_zenith) airmass = disc_out['airmass'] @@ -2109,6 +2111,7 @@ def _dirint_bins(times, kt_prime, zenith, w, delta_kt_prime): ------- tuple of kt_prime_bin, zenith_bin, w_bin, delta_kt_prime_bin """ + # @wholmgren: the following bin assignments use MATLAB's 1-indexing. # Later, we'll subtract 1 to conform to Python's 0-indexing. diff --git a/tests/test_irradiance.py b/tests/test_irradiance.py index a416636ae9..52a6f623f4 100644 --- a/tests/test_irradiance.py +++ b/tests/test_irradiance.py @@ -1140,6 +1140,50 @@ def test_dirindex_min_cos_zenith_max_zenith(): assert_series_equal(out, expected) +def test_dirint_array_inputs(): + """np.array and pd.Series inputs work correctly. GH #2751""" + times = pd.date_range('2023-06-21 10:00', periods=2, freq='h', tz='UTC') + + # np.array input → should return pd.Series + result = irradiance.dirint( + ghi=np.array([500.0, 400.0]), + solar_zenith=np.array([45.0, 50.0]), + times=times, + use_delta_kt_prime=False + ) + assert isinstance(result, pd.Series) + assert result.iloc[0] > 0 + + # pd.Series input → should return pd.Series + times2 = pd.date_range('2023-06-21 10:00', periods=3, freq='h', tz='UTC') + result2 = irradiance.dirint( + ghi=pd.Series([400, 500, 300], index=times2), + solar_zenith=pd.Series([50, 40, 60], index=times2), + times=times2 + ) + assert isinstance(result2, pd.Series) + assert (result2 >= 0).all() + + # single time point + use_delta_kt_prime=True → ValueError + times_single = pd.DatetimeIndex(['2023-06-21 12:00'], tz='UTC') + with pytest.raises(ValueError, match="requires at least two times"): + irradiance.dirint( + ghi=np.array([500.0]), + solar_zenith=np.array([45.0]), + times=times_single, + use_delta_kt_prime=True + ) + + # single time point + use_delta_kt_prime=False → no error + result3 = irradiance.dirint( + ghi=np.array([500.0]), + solar_zenith=np.array([45.0]), + times=times_single, + use_delta_kt_prime=False + ) + assert isinstance(result3, pd.Series) + + def test_dni(): ghi = pd.Series([90, 100, 100, 100, 100]) dhi = pd.Series([100, 90, 50, 50, 50])