Skip to content

Commit 8d38b4c

Browse files
committed
smallest_difference() for DataFrames
1 parent a6b33e7 commit 8d38b4c

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

windpowerlib/tools.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,42 @@
1111
__license__ = "GPLv3"
1212

1313

14-
def smallest_difference(value_1, value_2, comp_value, corresp_1, corresp_2):
14+
def smallest_difference(data_frame, comp_value, column_name):
1515
r"""
16-
Selects the value with the smaller difference to a comparative value.
16+
Selects a value with the smallest difference to a comparative value.
1717
1818
Additionally returns a corresponding value. This function is for example
1919
used in :py:func:`~.modelchain.v_wind_hub` of the
2020
:class:`~.modelchain.ModelChain` to choose the wind speed data that is
21-
close to the hub height of the examined wind turbine. In this case
22-
`value_1` and `value_2` are the heights of the corresponding wind speed
23-
data sets `corresp_1` and `corresp_2`.
21+
close to the hub height of the examined wind turbine. In this case the
22+
column of the data frame contains wind speed time series and the indices
23+
are the corresponding heights for which these time series apply.
2424
2525
Parameters
2626
----------
27-
value_1 : float
28-
First value of which the difference to `comp_value` will be
29-
compared with the difference to `comp_value` of `value_2`.
30-
value_2 : float
31-
Second value for comparison.
27+
data_frame : DataFrame
28+
Indices are the values of which the smallest difference to `comp_value`
29+
will be found, the corresponding values are in the column
30+
specified by `column_name` and they can be floats, pd.Series or
31+
np.arrays.
3232
comp_value : float
3333
Comparative value.
34-
corresp_1 : pd.Series or np.array or float
35-
Corresponding value to `value_1`.
36-
corresp_2 : pd.Series or np.array or float
37-
Corresponding value to `value_2`.
34+
column_name : string
35+
Name of the column in the `data_frame` that contains the
36+
correponding values.
3837
3938
Returns
4039
-------
4140
Tuple(float, pd.Series or np.array or float)
42-
Value closer to comparing value as float and its corresponding value as
43-
float.
41+
Closest value to comparative value as float and its corresponding value
42+
as float.
4443
4544
"""
46-
if (value_2 is not None and corresp_2 is not None):
47-
if abs(value_1 - comp_value) <= abs(value_2 - comp_value):
48-
closest_value = value_1
49-
else:
50-
closest_value = value_2
51-
else:
52-
closest_value = value_1
53-
54-
# Select correponding value
55-
if closest_value == value_1:
56-
corresp_value = corresp_1
57-
else:
58-
corresp_value = corresp_2
45+
diff = []
46+
for index in sorted(data_frame.index):
47+
diff.append(abs(comp_value - index))
48+
closest_value = sorted(data_frame.index)[diff.index(min(diff))]
49+
corresp_value = data_frame[column_name][closest_value]
5950
return (closest_value, corresp_value)
6051

6152

0 commit comments

Comments
 (0)