我想循环我的数据帧并选择每个月
然后我想看看一个指标是高还是低,如果是这样,只选择某些变量
我的代码如下所示:
#Sorting date range and taking top 10 values and append these to account
start_date = date(2003, 1, 1)
end_date = date(2019, 12, 1)
highest = []
lowest = []
while start_date <= end_date:
end_of_month = start_date + relativedelta(months=1, days=-1) #Calculate end of month
port = preds_vix.loc[start_date:end_of_month] #Select date range
#Apply if condition to filter for high and low Signals
if preds_vix['Signal_1'] == 'High':
port = port[vix_high] #Select factors that perform well/badly during high vix
for i in list(vix_high_short):
port_l = port.sort_values(i)
low = port_l.nlargest(1,['bh1m_prediction'])
for i in list(vix_high_long):
port_h = port.sort_values(i)
high = port_h.nlargest(1,['bh1m_prediction'])
elif preds_vix['Signal_1'] == 'Low':
port = port[vix_low] #Select factors that perform well/badly during high vix
for i in list(vix_low_short):
port_l = port.sort_values(i)
low = port_l.nlargest(1,['bh1m_prediction'])
for i in list(vix_low_long):
port_h = port.sort_values(i)
high = port_h.nlargest(1,['bh1m_prediction'])
else:
port = port.sort_values(by = 'bh1m_prediction') #sort data conditional on target value
top = port.nlargest(5,['preds']) #take 10 highest values
low = port.nsmallest(5, ['preds']) #take 10 lowest values
ret_h = np.mean(top['bh1m_prediction']) #calculate mean return of each month of long stocks
ret_l = np.mean(low['bh1m_prediction']) #calculate mean return of each month of short stocks
highest.append(ret_h) #append all values to a list
lowest.append(ret_l)
start_date = start_date + relativedelta(months=+1) #move one month forward and repeat the process
但是,我总是收到一条错误消息:
ValueError Traceback (most recent call last)
<ipython-input-91-4bb929bb98fb> in <module>
12
13
---> 14 if preds_vix['Signal_1'] == 'High':
15 port = port[vix_high] #Select factors that perform well/badly during high vix
16
~\anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1477 def __nonzero__(self):
1478 raise ValueError(
-> 1479 f"The truth value of a {type(self).__name__} is ambiguous. "
1480 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1481 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
是否可以在数据帧上应用if条件来选择以我的信号_1为条件的值
因此,如果您从错误中看到,问题的关键在于您试图验证整个列是否为true/false。这是不可能的。我们要做的是逐个检查每一行,看看它们是高还是低。我所做的是创建另一列来跟踪信号_1是高还是低,然后逐行进行
相关问题 更多 >
编程相关推荐