Home > Software design >  multiply column based values with column based on condition, python
multiply column based values with column based on condition, python

Time:11-07

I need to write a function that allows me to convert timeseries data (wind speed as the argument) to power output for a turbine. The output is based on several intervals.

So i have tried to create an additional dataframe to store the conversion coefficients.

The Problem is, that I get always the error 'float' object is not subscriptable. I know that it must has something to do with that Floating-point numbers are not subscriptable or cannot be accessed at an index but I can't figure out how to change the argument.

import pandas as pd
time = pd.date_range(start='2019-01-06 20:00:00', end='2019-01-07 03:00:00', freq='H')

df_ws = pd.DataFrame({"wind_speed_hh": 
                   [3.359367, 2.695838, 3.036351, 6.64743,
                    9.93, 13.13, 15.574893, 17.3432]}, index = time)

df_ws['power_production_Vestas'] = 0.0

weight_vestas are the conversion coefficients

weight_vestas = pd.DataFrame(
    {"wv1": [0.0],
     "wv2": [0.2],
     "wv3": [0.5],
     "wv4": [1.4],
     "wv5": [2.6],
     "wv6": [3.0],
     "wv7": [3.0],
     "wv8": [3.0]})
weight_vestas.head()

The functions is:

def conv_test(x):
    if 4 <= x['wind_speed_hh']:
        return (weight_vestas['wv1'] * x['wind_speed_hh'])
    elif 4 < x['df_ws.wind_speed_hh'] < 6:
        return (weight_vestas['wv2'] * x['df_ws.wind_speed_hh'])
    elif 6 <= x['df_ws.wind_speed_hh'] < 8:
        return (weight_vestas['wv3'] * x['df_ws.wind_speed_hh'])
    elif 8 <= x['df_ws.wind_speed_hh'] < 10:
        return (weight_vestas['wv4'] * x['df_ws.wind_speed_hh'])
    elif 10 <= x['df_ws.wind_speed_hh'] < 12:
        return (weight_vestas['wv5'] * x['df_ws.wind_speed_hh'])
    elif 12 <= x['df_ws.wind_speed_hh'] < 14:
        return (weight_vestas['wv6'] * x['df_ws.wind_speed_hh'])
    elif 14 <= x['df_ws.wind_speed_hh'] < 16:
        return (weight_vestas['wv7'] * x['df_ws.wind_speed_hh'])
    elif 16 <= x['df_ws.wind_speed_hh']:
        return (weight_vestas['wv8'] * x['df_ws.wind_speed_hh'])
    return ''

Running the function and adding the production output:

df_ws = df_ws.power_production_Vestas.apply(conv_test)
df_test.head()

CodePudding user response:

You are using .apply method in the power_production_Vestas column, which means that the function will be applied to each element of that column. That's why you are getting the "float is not subscriptable" error, that x argument of conv_test is a float.

Try using the same method but on the dataframe, than x will be a Series.

df_ws.apply(conv_test, axis=1)

axis=1 makes sure you'll go thought the lines, not the columns.

CodePudding user response:

use pd.cut instead conv_test

s1 = pd.cut(
    df_ws['wind_speed_hh'], 
    bins=[0, 4, 6, 8, 10, 12], 
    labels=[0.00, 0.20, 0.50, 1.40, 2.60], 
    right=False).astype('float').fillna(3.00)
s1 * df_ws['wind_speed_hh']

output:

2019-01-06 20:00:00    0.00
2019-01-06 21:00:00    0.00
2019-01-06 22:00:00    0.00
2019-01-06 23:00:00    3.32
2019-01-07 00:00:00   13.90
2019-01-07 01:00:00   39.39
2019-01-07 02:00:00   46.72
2019-01-07 03:00:00   52.03
Freq: H, Name: wind_speed_hh, dtype: float64
  • Related