Skip to content

Commit 1fa50b5

Browse files
committed
Adapt interpolation function to multiindex dataframe
1 parent e2ce7d7 commit 1fa50b5

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

windpowerlib/tools.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def smallest_difference(data_frame, comp_value, column_name):
5050
return (closest_value, corresp_value)
5151

5252

53-
def linear_extra_interpolation(data_frame, requested_height, column_name):
53+
def linear_extra_interpolation(data_frame, requested_height):
5454
r"""
5555
Inter- or extrapolates between the values of a data frame.
5656
@@ -94,14 +94,11 @@ def linear_extra_interpolation(data_frame, requested_height, column_name):
9494
place
9595
9696
"""
97-
# Get closest index of data_frame to requested_height
98-
height_2, value_2 = smallest_difference(data_frame, requested_height,
99-
column_name)
100-
# Drop row with closest index to requested_height and get second closest
101-
data_frame_2 = data_frame.drop(height_2)
102-
height_1, value_1 = smallest_difference(data_frame_2, requested_height,
103-
column_name)
104-
# Interpolation
105-
interpolant = ((value_2 - value_1) / (height_2 - height_1) *
106-
(requested_height - height_1) + value_1)
107-
return interpolant
97+
# find closest heights
98+
heights_sorted = data_frame.columns[
99+
sorted(range(len(data_frame.columns)),
100+
key=lambda i: abs(data_frame.columns[i] - requested_height))]
101+
return ((data_frame[heights_sorted[1]] - data_frame[heights_sorted[0]]) /
102+
(heights_sorted[1] - heights_sorted[0]) *
103+
(requested_height - heights_sorted[0]) +
104+
data_frame[heights_sorted[0]])

0 commit comments

Comments
 (0)