原始问题为如何根据一个dataframe生成另一个dataframe 解决办法请见二楼
请问大家,在Julia中,“Any”是指任何一种type吗?
问题的来源是我在转写一份Julia 0.5 的代码到 Julia 1.3,在运行下列代码时最后一步总是会显示
assetclasses[:returns] = Array{Any,8}
assetclasses[1,:returns]=classreturns
ERROR: MethodError: Cannot `convert` an object of type DataFrame to an object of type DataType
我猜测问题在于上述代码块中第一行可能有些问题。
完整代码如下:
df = DataFrame(FF25_01 = 1:6, FF25_02 = 100:105, ALL_01 = 2:7, ALL_02 = 3:8)
assetnames = ["FF25_01","FF25_02","ALL_01"]
classnames = ["FF25", "US_bonds", "Sov_bonds", "Options", "CDS", "Commod", "FX", "All"]
nclasses = length(classnames)
nassets = length(assetnames)
assetclasses = DataFrame(classnames=classnames)
for c=1:nclasses
classname=classnames[c]
classreturns = DataFrame()
for a=1:nassets
assetname = assetnames[a]
if occursin(classname,assetname)
classreturns[!,Symbol(assetname)] = df[[!,Symbol(assetname)]
end
end
end
assetclasses[:returns] = Array{Any,8}
assetclasses[1,:returns]=classreturns
以下是原始代码 in Julia 0.5
function organizedata(alldata)
returnsstart = first(find(names(alldata).==:rf))+1
returns = alldata[:,returnsstart:end]
rf = alldata[:rf]
assetnames = map(x->string(x),names(returns))
nassets = length(assetnames)
nclasses = length(classnames)
assetclasses = DataFrame(classnames=classnames)
assetclasses[:returns] = DataArray(Any, nclasses)
assetclasses[:excessreturns] = DataArray(Any, nclasses)
assetclasses[:n] = DataArray(Any, nclasses)
assetclasses[:allassetsRange] = DataArray(Any, nclasses)
allassetsCounter = 1;
for c=1:nclasses
classname = classnames[c]
classreturns = DataFrame()
for a=1:nassets
assetname = assetnames[a]
if contains(assetname,classname)
classreturns[Symbol(assetname)] = returns[Symbol(assetname)]
end
end
assetclasses[c,:returns]=classreturns
excessreturns = classreturns
T, n = size(excessreturns)
for i=1:n
excessreturns[:,i] = 100*(excessreturns[:,i] - rf)
end
assetclasses[c,:excessreturns]=excessreturns
assetclasses[c,:n] = n
assetclasses[c,:allassetsRange] = vcat(allassetsCounter:allassetsCounter+n-1)
allassetsCounter = allassetsCounter + n
end
factors = alldata[[:intermediary_capital_risk_factor, :mkt_rf]]
factors[:intermediary_capital_risk_factor]*=100
factors[:mkt_rf]*=100
assetclasses, factors
end
现在在研究怎么把这个代码update到Julia 1.3 也可以用。。。
initial code source: