From 66bbe0c84f200937643289a339deef104393d681 Mon Sep 17 00:00:00 2001 From: SabineH Date: Wed, 8 Jan 2020 10:56:55 +0100 Subject: [PATCH 1/2] Convert str to numeric in TurbineClusterModelChain incl. test --- tests/test_turbine_cluster_modelchain.py | 18 ++++++++++++++++++ windpowerlib/turbine_cluster_modelchain.py | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/tests/test_turbine_cluster_modelchain.py b/tests/test_turbine_cluster_modelchain.py index 6e9356be..073e88a3 100644 --- a/tests/test_turbine_cluster_modelchain.py +++ b/tests/test_turbine_cluster_modelchain.py @@ -272,3 +272,21 @@ def test_tc_modelchain_with_power_curve_as_dict(self): power_plant=wtc.WindTurbineCluster(**my_cluster)) test_tc_mc.run_model(self.weather_df) assert_series_equal(test_tc_mc.power_output, power_output_exp) + + def test_heigths_as_string(self): + """Test run_model if data heights are of type string.""" + + # Convert data heights to str + string_weather = self.weather_df.copy() + string_weather.columns = pd.MultiIndex.from_arrays([ + string_weather.columns.get_level_values(0), + string_weather.columns.get_level_values(1).astype(str)]) + + # Heights in the original DataFrame are of type np.int64 + assert isinstance(self.weather_df.columns.get_level_values(1)[0], + np.int64) + assert isinstance(string_weather.columns.get_level_values(1)[0], str) + + test_mc = tc_mc.TurbineClusterModelChain( + power_plant=wtc.WindTurbineCluster(**self.test_cluster)) + test_mc.run_model(string_weather) diff --git a/windpowerlib/turbine_cluster_modelchain.py b/windpowerlib/turbine_cluster_modelchain.py index 39c87480..d69493d1 100644 --- a/windpowerlib/turbine_cluster_modelchain.py +++ b/windpowerlib/turbine_cluster_modelchain.py @@ -8,6 +8,7 @@ SPDX-License-Identifier: MIT """ import logging +import pandas as pd from windpowerlib import wake_losses from windpowerlib.modelchain import ModelChain @@ -266,6 +267,10 @@ def run_model(self, weather_df): 'wind_speed' """ + # Convert data heights to integer. In some case they are strings. + weather_df.columns = pd.MultiIndex.from_arrays([ + weather_df.columns.get_level_values(0), + pd.to_numeric(weather_df.columns.get_level_values(1))]) self.assign_power_curve(weather_df) self.power_plant.mean_hub_height() From 6ff4368d46f31e9eab3cd14c391a11ad1508a03f Mon Sep 17 00:00:00 2001 From: SabineH Date: Wed, 8 Jan 2020 10:57:13 +0100 Subject: [PATCH 2/2] Delete conversion from example --- example/modelchain_example.ipynb | 5 ----- example/modelchain_example.py | 5 ----- 2 files changed, 10 deletions(-) diff --git a/example/modelchain_example.ipynb b/example/modelchain_example.ipynb index 14714a2f..f4b1f748 100644 --- a/example/modelchain_example.ipynb +++ b/example/modelchain_example.ipynb @@ -139,11 +139,6 @@ " weather_df.index = pd.to_datetime(weather_df.index).tz_convert(\n", " 'Europe/Berlin')\n", " \n", - " # change type of height from str to int by resetting columns\n", - " l0 = [_[0] for _ in weather_df.columns]\n", - " l1 = [int(_[1]) for _ in weather_df.columns]\n", - " weather_df.columns = [l0, l1]\n", - " \n", " return weather_df\n", "\n", "\n", diff --git a/example/modelchain_example.py b/example/modelchain_example.py index 6aec70f2..badef625 100644 --- a/example/modelchain_example.py +++ b/example/modelchain_example.py @@ -78,11 +78,6 @@ def get_weather_data(filename='weather.csv', **kwargs): weather_df.index = pd.to_datetime(weather_df.index).tz_convert( 'Europe/Berlin') - # change type of height from str to int by resetting columns - l0 = [_[0] for _ in weather_df.columns] - l1 = [int(_[1]) for _ in weather_df.columns] - weather_df.columns = [l0, l1] - return weather_df