Skip to content

Commit

Permalink
Adapt to DataFrames index change (http://JuliaData/DataFrames.jl#2199)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyun committed Jun 11, 2020
1 parent 50bd92a commit 1bf0a2a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ deunitfy(v, u) = deunitfy(unitfy(v, u))
import DataFrames: DataFrame, DataFrames
unitfy(df::DataFrame, U::Vector) = begin
r = DataFrame()
for (n, c, u) in zip(names(df), eachcol(df), U)
for (n, c, u) in zip(propertynames(df), eachcol(df), U)
r[!, n] = unitfy.(c, u)
end
r
end
unitfy(df::DataFrame) = begin
p = r"(.+)\(([^\(\)]+)\)$"
M = match.(p, String.(names(df)))
M = match.(p, names(df))
u(m::RegexMatch) = eval(:(@u_str($(m.captures[2]))))
u(m) = nothing
U = u.(M)
Expand Down
2 changes: 1 addition & 1 deletion src/util/calibrate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ calibrate(S::Type{<:System}, obs, configs; index="context.clock.tick", target, p
elseif metric == :prmse
metric = (E, O) -> ((E - O) ./ O).^2
end
NT = DataFrames.make_unique([names(obs)..., T...], makeunique=true)
NT = DataFrames.make_unique([propertynames(obs)..., T...], makeunique=true)
T1 = NT[end-n+1:end]
residual(c) = begin
est = simulate(S; config=c, index=index, target=target, verbose=false, kwargs...)
Expand Down
2 changes: 1 addition & 1 deletion src/util/simulate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ format(m::Simulation; nounit=false, long=false) = begin
end
if long
i = collect(keys(m.index))
t = setdiff(names(r), i)
t = setdiff(propertynames(r), i)
r = DataFrames.stack(r, t, i)
end
r
Expand Down
16 changes: 8 additions & 8 deletions test/unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,19 @@ using DataFrames
@testset "dataframe auto" begin
df = DataFrame()
z = [0]
df[!, Symbol("a (g/cm^2)")] = z
df[!, Symbol("c (a)(b)(s)")] = z
df[!, Symbol("b ()")] = z
df[!, Symbol("d")] = z
df."a (g/cm^2)" = z
df."c (a)(b)(s)" = z
df."b ()" = z
df."d" = z
r = Cropbox.unitfy(df)
@test Cropbox.unit(eltype(r[!, 1])) == u"g/cm^2"
@test Cropbox.unit(eltype(r[!, 2])) == u"s"
@test Cropbox.unit(eltype(r[!, 3])) == u"NoUnits"
@test Cropbox.unit(eltype(r[!, 4])) == u"NoUnits"
N = names(r)
@test N[1] == Symbol("a")
@test N[2] == Symbol("c (a)(b)")
@test N[3] == Symbol("b ()")
@test N[4] == Symbol("d")
@test N[1] == "a"
@test N[2] == "c (a)(b)"
@test N[3] == "b ()"
@test N[4] == "d"
end
end
14 changes: 7 additions & 7 deletions test/util/simulate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using Dates
r = simulate(SSimulate, stop=n)
@test r isa DataFrame
@test size(r, 1) == (n+1)
@test names(r) == [:tick, :a, :b]
@test propertynames(r) == [:tick, :a, :b]
@test r[end, :tick] == (n+1)u"hr"
@test r[end, :a] == 1
@test r[end, :b] == n
Expand All @@ -20,10 +20,10 @@ using Dates
@test r[end, :b] == 2n
r = simulate(SSimulate, stop=n, target=[:b])
@test size(r, 2) == 2
@test names(r) == [:tick, :b]
@test propertynames(r) == [:tick, :b]
r = simulate(SSimulate, stop=n, index=:b, target=[:b])
@test size(r, 2) == 1
@test names(r) == [:b]
@test propertynames(r) == [:b]
end

@testset "stop" begin
Expand Down Expand Up @@ -87,9 +87,9 @@ using Dates
]
n = 1
r = simulate(SSimulateLayout, L, stop=n)
@test names(r[1]) == [:tick, :a]
@test names(r[2]) == [:t, :i, :a, :B]
@test names(r[3]) == [:tick, :step]
@test propertynames(r[1]) == [:tick, :a]
@test propertynames(r[2]) == [:t, :i, :a, :B]
@test propertynames(r[3]) == [:tick, :step]
@test r[1][end, :tick] == r[2][end, :t] == r[3][end, :tick] == (n+1)u"hr"
@test r[1][end, :a] == 0
@test r[2][end, :B] == 2
Expand Down Expand Up @@ -183,7 +183,7 @@ using Dates
g => (1, 2) ~ track::Tuple
end
r = simulate(SSimulateExtractable)
N = names(r)
N = propertynames(r)
# default = Union{Number,Symbol,AbstractString,AbstractDateTime}
@test :a N
@test :b N
Expand Down

0 comments on commit 1bf0a2a

Please sign in to comment.