Skip to content

Commit 343fbe9

Browse files
committed
BUG: fix dirint KeyError for scalar inputs on pandas >= 2.0
1 parent 33f8c49 commit 343fbe9

3 files changed

Lines changed: 40 additions & 7 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Deprecations
1515
Bug fixes
1616
~~~~~~~~~
1717
* Fixed :py:func:`pvlib.irradiance.dirint` raising ``KeyError`` with
18-
scalar inputs on pandas >= 2.0. (:pull:`XXXX`, :issue:`2751`)
18+
scalar inputs on pandas >= 2.0. (:pull:`2752`, :issue:`2751`)
1919
By :ghuser:`Omesh37`.
2020
* Corrects a bug in :py:func:`pvlib.temperature.fuentes`. If inputs were
2121
data type integer, users can expect modeled cell temperature values to

pvlib/irradiance.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,10 +1927,10 @@ def dirint(ghi, solar_zenith, times, pressure=101325., use_delta_kt_prime=True,
19271927
19281928
Parameters
19291929
----------
1930-
ghi : array-like
1930+
ghi : numeric
19311931
Global horizontal irradiance. See :term:`ghi`. [Wm⁻²]
19321932
1933-
solar_zenith : array-like
1933+
solar_zenith : numeric
19341934
True (not refraction-corrected) solar zenith angles. See
19351935
:term:`solar_zenith`. [°]
19361936
@@ -1964,9 +1964,9 @@ def dirint(ghi, solar_zenith, times, pressure=101325., use_delta_kt_prime=True,
19641964
19651965
Returns
19661966
-------
1967-
dni : array-like
1968-
The modeled direct normal irradiance, as provided by the
1969-
DIRINT model. [Wm⁻²]
1967+
dni : numeric or pd.Series
1968+
Estimated direct normal irradiance. Returns float if all inputs
1969+
are scalar, pd.Series otherwise. [Wm⁻²]
19701970
19711971
Notes
19721972
-----
@@ -1983,7 +1983,8 @@ def dirint(ghi, solar_zenith, times, pressure=101325., use_delta_kt_prime=True,
19831983
Global Horizontal to Direct Normal Insolation", Technical Report No.
19841984
SERI/TR-215-3087, Golden, CO: Solar Energy Research Institute, 1987.
19851985
"""
1986-
1986+
scalar_input = np.isscalar(solar_zenith)
1987+
19871988
disc_out = disc(ghi, solar_zenith, times, pressure=pressure,
19881989
min_cos_zenith=min_cos_zenith, max_zenith=max_zenith)
19891990
airmass = disc_out['airmass']
@@ -1998,9 +1999,12 @@ def dirint(ghi, solar_zenith, times, pressure=101325., use_delta_kt_prime=True,
19981999
dirint_coeffs = _dirint_coeffs(times, kt_prime, solar_zenith, w,
19992000
delta_kt_prime)
20002001

2002+
20012003
# Perez eqn 5
20022004
dni = disc_out['dni'] * dirint_coeffs
20032005

2006+
if scalar_input:
2007+
return float(dni.iloc[0])
20042008
return dni
20052009

20062010

@@ -2160,6 +2164,7 @@ def _dirint_bins(times, kt_prime, zenith, w, delta_kt_prime):
21602164
return kt_prime_bin, zenith_bin, w_bin, delta_kt_prime_bin
21612165

21622166

2167+
21632168
def dirindex(ghi, ghi_clear, dni_clear, zenith, times, pressure=101325.,
21642169
use_delta_kt_prime=True, temp_dew=None, min_cos_zenith=0.065,
21652170
max_zenith=87):

tests/test_irradiance.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,34 @@ def test_dirindex_min_cos_zenith_max_zenith():
11391139
expected = pd.Series([nan, 5.], index=times)
11401140
assert_series_equal(out, expected)
11411141

1142+
def test_dirint_scalar_inputs():
1143+
"""Scalar numeric inputs return scalar float output. GH #2751"""
1144+
times = pd.DatetimeIndex(['2023-06-21 12:00'], tz='UTC')
1145+
1146+
# scalar int input → should return float (not Series)
1147+
result = irradiance.dirint(
1148+
ghi=500, solar_zenith=45, times=times
1149+
)
1150+
assert np.isscalar(result), "scalar input should return scalar output"
1151+
assert isinstance(result, float)
1152+
1153+
# scalar float with delta_kt_prime disabled → non-NaN value check
1154+
result = irradiance.dirint(
1155+
ghi=500.0, solar_zenith=45.0, times=times,
1156+
use_delta_kt_prime=False
1157+
)
1158+
assert np.isscalar(result)
1159+
assert isinstance(result, float)
1160+
assert result > 0 # should be positive DNI
1161+
1162+
# Series input → should still return Series (regression check)
1163+
times2 = pd.date_range('2023-06-21 10:00', periods=3, freq='h', tz='UTC')
1164+
result_series = irradiance.dirint(
1165+
ghi=pd.Series([400, 500, 300], index=times2),
1166+
solar_zenith=pd.Series([50, 40, 60], index=times2),
1167+
times=times2
1168+
)
1169+
assert isinstance(result_series, pd.Series)
11421170

11431171
def test_dni():
11441172
ghi = pd.Series([90, 100, 100, 100, 100])

0 commit comments

Comments
 (0)