我必须检查股票期权缺少的日期,并且我想通过for循环进行操作。但是
“'(slice(0,155,None),slice(None,None,None))'是无效的键”
显示为错误,我不知道为什么。
首先,它是datetime64[ns]
类型,我已经尝试将其转换为字符串。但是,它似乎没有帮助。这是我的代码:
def days(data, alldays):
days = np.zeros((len(data),1))
companies = ["AAPL", "AXP", "BA", "CAT", "CSCO", "CVX", "DIS", "DJX", "GS", "HD", "IBM", "INTC", "JNJ", \
"JPM", "KO", "MCD", "MMM", "MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V", "VZ", "WBA", "WMT", "XOM"]
d = {}
for i, firm in enumerate(data):
firms = data[i]
days[i] = len(firms.date.unique())
firmdays = firms.date.unique()
counter = 0
if days[i] < 3730:
print(firms.ticker.unique(), len(firms), days[i])
elif days[i] < len(alldays) and days[i]>=3730:
d[companies[i]] = np.zeros((582348,6))
breakpoint()
for j in range(3733):
breakpoint()
if alldays[j] not in firmdays:
breakpoint()
print(firms.ticker.unique(), alldays[j])
for k, data in enumerate(np.zeros((582348,6))):
d[companies[i]][k:k+155,:] = firms[k-156 - counter*156:k-1- 156*counter,:]
counter = counter +1
else:
for k, data in enumerate(np.zeros((582348,6))):
d[companies[i]][k:k+155,:] = firms[k- counter*156:k+155 - 156*counter,:]
print(firms.ticker.unique(), len(firms), days[i])
return days
它在第"if alldays[j] not in firmdays:"
行给出了错误,即
'(slice(0, 155, None), slice(None, None, None))' is an invalid key
答案 0 :(得分:0)
看着错误,问题似乎更多在于在该行上使用了切片符号(=符号之前的部分):
d[companies[i]][k:k+155,:] = firms[k- counter*156,:]
不确定您要达到的目标,但请看一下这篇文章:https://stackoverflow.com/a/509295/6203472