【发布时间】:2020-11-16 14:40:45
【问题描述】:
在我的代码中,我有一个函数如下,它返回一个简单的数据框:
def find_highest_confs(dictOfCurves):
"""
Parameters
----------
dictOfCurves : Function takes in a dictionary containing stocks(key) and a
dataframe per stock containing stocktrend data for that stock
Returns
-------
multipleConfs : A dataframe with per row the stock (ticker symbol), start
date of the highest order trend, the nr of times that trend was confirmed
and the date of last confirmation
"""
multipleConfs = pd.DataFrame(columns = ['symbol', 'max confirmations', \
'Launch date', 'Last confirmation'])
for item in dictOfCurves:
df = dictOfCurves[item]
try:
df.sort_values(by = ['confirmations'], ascending = False, inplace = True)
maxLaunchDate = df[df['confirmations'] == df['confirmations'].max()].index[0]
lastConf = df.loc[maxLaunchDate, 'Last confirmation']
newData = {'symbol': item, 'max confirmations': df['confirmations'].max(), \
'Launch date': maxLaunchDate, 'Last confirmation': lastConf}
except:
newData = {'symbol': item, 'max confirmations': np.nan, 'Launch date': np.nan, \
'Last confirmation': np.nan}
multipleConfs = multipleConfs.append(newData, ignore_index = True)
return multipleConfs
现在这段代码可以正常工作,并返回一个 df,如下所示:
highest = find_highest_confs(curves)
这会产生预期的数据框,没有设置索引。
如果我然后设置这样的索引:
highest.set_index('symbol', inplace = True)
再次,按预期工作。
这是奇怪的事情......
如果我将函数中的最后一行更改为:
return multipleConfs.set_index('symbol', inplace = True)
它返回一个空的NoneType?
我也尝试添加multipleConfs.set_index('symbol', inplace = True)
先声明一行,然后返回它。结果一样?
我真的很困惑为什么我不能将索引设置为函数中代码的一部分?
【问题讨论】: