I need to create a new column from the output of a function. Below the function:
def lane_shoulder_width_factor(lane: float, shoulder: float = 0.0) -> float:
"""Speed reduction factor based on lane and shoulder width.
Args:
lane: float
shoulder: float
Returns:
float
"""
max_lane_shoulder_width = 999
min_lane_width = 2.7
factor = {
(min_lane_width, 3.0): {
(0.0, 0.6): 10.3,
(0.6, 1.2): 7.7,
(1.2, 1.8): 5.6,
(1.8, max_lane_shoulder_width): 3.5,
},
(3.0, 3.3): {
(0.0, 0.6): 8.5,
(0.6, 1.2): 5.9,
(1.2, 1.8): 3.8,
(1.8, max_lane_shoulder_width): 1.7,
},
(3.3, 3.6): {
(0.0, 0.6): 7.5,
(0.6, 1.2): 4.9,
(1.2, 1.8): 2.8,
(1.8, max_lane_shoulder_width): 0.7,
},
(3.6, 999): {
(0.0, 0.6): 6.8,
(0.6, 1.2): 4.2,
(1.2, 1.8): 2.1,
(1.8, max_lane_shoulder_width): 0.0,
},
}
for key, value in factor.items():
lane_width_min = key[0]
lane_width_max = key[1]
if lane_width_min <= lane < lane_width_max:
for key2, value2 in value.items():
shoulder_width_min = key2[0]
shoulder_width_max = key2[1]
if shoulder_width_min <= shoulder < shoulder_width_max:
return value2
Below the columns already into DataFrame:
fid int64
direction int64
flip int64
w_lane float64
w_shoulder float64
maxspeed float64
So, I create the new column using the function above:
df['fls'] = lane_shoulder_width_factor(
lane=df.w_lane,
shoulder=df.w_shoulder
)
The output is this error:
self = 0 False 1 False 2 False 3 False 4 False 5
False 6 False 7 False 8 False Name: w_lane, dtype: bool@final def __nonzero__(self): raise ValueError( f"The truth value of a {type(self).__name__} is ambiguous. " "Use a.empty, a.bool(), a.item(), a.any() or a.all()." ) E ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
/home/max/.cache/pypoetry/virtualenvs/mobilitypath-zlqRuy9I-py3.8/lib/python3.8/site-packages/pandas/core/generic.py:1527: ValueError
It isn't clear for me where is the problem. The values inside the columns w_lane
and w_shoulder
are inside the ranges.