Skip to content

Commit b23be5e

Browse files
committed
Allow Series input to modelchain._common_keys()
Return the intersection of the indices of the series. This supports the use case shown in introtutorial.rst where module parameters are a column selected from the a data frame.
1 parent 42dde2c commit b23be5e

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

pvlib/modelchain.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,9 +1795,11 @@ def _all_same_index(data):
17951795
def _common_keys(dicts):
17961796
"""Return the intersection of the set of keys for each dictionary
17971797
in `dicts`"""
1798+
def _keys(x):
1799+
return set(x.keys())
17981800
if isinstance(dicts, tuple):
1799-
return set.intersection(*map(set, dicts))
1800-
return set(dicts)
1801+
return set.intersection(*map(_keys, dicts))
1802+
return _keys(dicts)
18011803

18021804

18031805
def _tuple_from_dfs(dfs, name):

pvlib/tests/test_modelchain.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,9 +1204,11 @@ def test_ac_model_not_a_model(pvwatts_dc_pvwatts_ac_system, location, weather):
12041204

12051205

12061206
def test_infer_ac_model_invalid_params(location):
1207+
# only the keys are relevant here, using arbitrary values
1208+
module_parameters = {'pdc0': 1, 'gamma_pdc': 1}
12071209
system = pvsystem.PVSystem(
12081210
arrays=[pvsystem.Array(
1209-
module_parameters=pvsystem._DC_MODEL_PARAMS['pvwatts']
1211+
module_parameters=module_parameters
12101212
)],
12111213
inverter_parameters={'foo': 1, 'bar': 2}
12121214
)
@@ -1676,7 +1678,9 @@ def test_unknown_attribute(sapm_dc_snl_ac_system, location):
16761678
mc.unknown_attribute
16771679

16781680

1679-
def test_inconsistent_array_params(location):
1681+
def test_inconsistent_array_params(location,
1682+
sapm_module_params,
1683+
cec_module_params):
16801684
module_error = ".* selected for the DC model but one or more Arrays are " \
16811685
"missing one or more required parameters"
16821686
temperature_error = "could not infer temperature model from " \
@@ -1687,32 +1691,57 @@ def test_inconsistent_array_params(location):
16871691
different_module_system = pvsystem.PVSystem(
16881692
arrays=[
16891693
pvsystem.Array(
1690-
module_parameters=pvsystem._DC_MODEL_PARAMS['sapm']),
1694+
module_parameters=sapm_module_params),
16911695
pvsystem.Array(
1692-
module_parameters=pvsystem._DC_MODEL_PARAMS['cec']),
1696+
module_parameters=cec_module_params),
16931697
pvsystem.Array(
1694-
module_parameters=pvsystem._DC_MODEL_PARAMS['cec'])]
1698+
module_parameters=cec_module_params)]
16951699
)
16961700
with pytest.raises(ValueError, match=module_error):
16971701
ModelChain(different_module_system, location, dc_model='cec')
16981702
different_temp_system = pvsystem.PVSystem(
16991703
arrays=[
17001704
pvsystem.Array(
1701-
module_parameters=pvsystem._DC_MODEL_PARAMS['cec'],
1705+
module_parameters=cec_module_params,
17021706
temperature_model_parameters={'a': 1,
17031707
'b': 1,
17041708
'deltaT': 1}),
17051709
pvsystem.Array(
1706-
module_parameters=pvsystem._DC_MODEL_PARAMS['cec'],
1710+
module_parameters=cec_module_params,
17071711
temperature_model_parameters={'a': 2,
17081712
'b': 2,
17091713
'deltaT': 2}),
17101714
pvsystem.Array(
1711-
module_parameters=pvsystem._DC_MODEL_PARAMS['cec'],
1715+
module_parameters=cec_module_params,
17121716
temperature_model_parameters={'b': 3, 'deltaT': 3})]
17131717
)
17141718
with pytest.raises(ValueError, match=temperature_error):
17151719
ModelChain(different_temp_system, location,
17161720
ac_model='sandia_multi',
17171721
aoi_model='no_loss', spectral_model='no_loss',
17181722
temperature_model='sapm')
1723+
1724+
1725+
def test_modelchain__common_keys():
1726+
dictionary = {'a': 1, 'b': 1}
1727+
series = pd.Series(dictionary)
1728+
assert {'a', 'b'} == modelchain._common_keys(
1729+
{'a': 1, 'b': 1}
1730+
)
1731+
assert {'a', 'b'} == modelchain._common_keys(
1732+
pd.Series({'a': 1, 'b': 1})
1733+
)
1734+
assert {'a', 'b'} == modelchain._common_keys(
1735+
(dictionary, series)
1736+
)
1737+
no_a = dictionary.copy()
1738+
del no_a['a']
1739+
assert {'b'} == modelchain._common_keys(
1740+
(dictionary, no_a)
1741+
)
1742+
assert {'b'} == modelchain._common_keys(
1743+
(series, pd.Series(no_a))
1744+
)
1745+
assert {'b'} == modelchain._common_keys(
1746+
(series, no_a)
1747+
)

0 commit comments

Comments
 (0)