Skip to content

Commit 2b51d4a

Browse files
authored
change irradiance.perez(return_components=True) to return dict/DataFrame (#565)
* change irradiance.perez(return_components=True) to return dict/DataFrame * assign series name in test * fix py27min test attempt 2
1 parent ec329e2 commit 2b51d4a

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ API Changes
5555
instead of only the function handle (:issue:`417`)
5656
* Add DC model methods desoto and pvsyst to ModelChain, and deprecates DC model method singlediode
5757
(singlediode defaults to desoto until v0.7.0) (:issue:`487`)
58+
* The behavior of irradiance.perez(return_components=True) has changed.
59+
The function previously returned a tuple of total sky diffuse and
60+
an OrderedDict/DataFrame of components. The function now returns
61+
an OrderedDict/DataFrame with total sky diffuse and each component.
62+
The behavior for return_components=False remains unchanged. (:issue:`434`)
5863

5964

6065
Enhancements

pvlib/irradiance.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -1077,9 +1077,22 @@ def perez(surface_tilt, surface_azimuth, dhi, dni, dni_extra,
10771077
10781078
Returns
10791079
--------
1080+
numeric, OrderedDict, or DataFrame
1081+
Return type controlled by `return_components` argument.
1082+
If ``return_components=False``, `sky_diffuse` is returned.
1083+
If ``return_components=True``, `diffuse_components` is returned.
1084+
10801085
sky_diffuse : numeric
10811086
The sky diffuse component of the solar radiation on a tilted
1082-
surface. Array input is currently converted to Series output.
1087+
surface.
1088+
1089+
diffuse_components : OrderedDict (array input) or DataFrame (Series input)
1090+
Keys/columns are:
1091+
* sky_diffuse: Total sky diffuse
1092+
* isotropic
1093+
* circumsolar
1094+
* horizon
1095+
10831096
10841097
References
10851098
----------
@@ -1161,6 +1174,7 @@ def perez(surface_tilt, surface_azimuth, dhi, dni, dni_extra,
11611174

11621175
if return_components:
11631176
diffuse_components = OrderedDict()
1177+
diffuse_components['sky_diffuse'] = sky_diffuse
11641178

11651179
# Calculate the different components
11661180
diffuse_components['isotropic'] = dhi * term1
@@ -1175,9 +1189,7 @@ def perez(surface_tilt, surface_azimuth, dhi, dni, dni_extra,
11751189
else:
11761190
diffuse_components = {k: np.where(mask, 0, v) for k, v in
11771191
diffuse_components.items()}
1178-
1179-
return sky_diffuse, diffuse_components
1180-
1192+
return diffuse_components
11811193
else:
11821194
return sky_diffuse
11831195

pvlib/test/test_irradiance.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -232,29 +232,27 @@ def test_perez(irrad_data, ephem_data, dni_et, relative_airmass):
232232
def test_perez_components(irrad_data, ephem_data, dni_et, relative_airmass):
233233
dni = irrad_data['dni'].copy()
234234
dni.iloc[2] = np.nan
235-
out, df_components = irradiance.perez(40, 180, irrad_data['dhi'], dni,
236-
dni_et, ephem_data['apparent_zenith'],
237-
ephem_data['azimuth'], relative_airmass,
238-
return_components=True)
239-
expected = pd.Series(np.array(
240-
[ 0. , 31.46046871, np.nan, 45.45539877]),
241-
index=irrad_data.index)
242-
expected_components = pd.DataFrame(
243-
np.array([[ 0. , 26.84138589, np.nan, 31.72696071],
244-
[ 0. , 0. , np.nan, 4.47966439],
245-
[ 0. , 4.62212181, np.nan, 9.25316454]]).T,
246-
columns=['isotropic', 'circumsolar', 'horizon'],
235+
out = irradiance.perez(40, 180, irrad_data['dhi'], dni,
236+
dni_et, ephem_data['apparent_zenith'],
237+
ephem_data['azimuth'], relative_airmass,
238+
return_components=True)
239+
expected = pd.DataFrame(np.array(
240+
[[ 0. , 31.46046871, np.nan, 45.45539877],
241+
[ 0. , 26.84138589, np.nan, 31.72696071],
242+
[ 0. , 0. , np.nan, 4.47966439],
243+
[ 0. , 4.62212181, np.nan, 9.25316454]]).T,
244+
columns=['sky_diffuse', 'isotropic', 'circumsolar', 'horizon'],
247245
index=irrad_data.index
248246
)
249247
if pandas_0_22():
250-
expected_for_sum = expected.copy()
248+
expected_for_sum = expected['sky_diffuse'].copy()
251249
expected_for_sum.iloc[2] = 0
252250
else:
253-
expected_for_sum = expected
254-
sum_components = df_components.sum(axis=1)
251+
expected_for_sum = expected['sky_diffuse']
252+
sum_components = out.iloc[:, 1:].sum(axis=1)
253+
sum_components.name = 'sky_diffuse'
255254

256-
assert_series_equal(out, expected, check_less_precise=2)
257-
assert_frame_equal(df_components, expected_components)
255+
assert_frame_equal(out, expected, check_less_precise=2)
258256
assert_series_equal(sum_components, expected_for_sum, check_less_precise=2)
259257

260258

@@ -268,6 +266,15 @@ def test_perez_arrays(irrad_data, ephem_data, dni_et, relative_airmass):
268266
expected = np.array(
269267
[ 0. , 31.46046871, np.nan, 45.45539877])
270268
assert_allclose(out, expected, atol=1e-2)
269+
assert isinstance(out, np.ndarray)
270+
271+
272+
def test_perez_scalar():
273+
# copied values from fixtures
274+
out = irradiance.perez(40, 180, 118.45831879, 939.95469881,
275+
1321.1655834833093, 10.56413562, 144.76567754,
276+
1.01688136)
277+
assert_allclose(out, 109.084332)
271278

272279

273280
@pytest.mark.parametrize('model', ['isotropic', 'klucher', 'haydavies',

0 commit comments

Comments
 (0)