diff --git a/Project.toml b/Project.toml new file mode 100644 index 0000000..7ffdf35 --- /dev/null +++ b/Project.toml @@ -0,0 +1,15 @@ +name = "Impute" +uuid = "f7bf1975-0170-51b9-8c5f-a992d46b9575" +authors = ["Invenia Technical Computing"] +version = "0.2.0" + +[deps] +DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" +Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" + +[extras] +RDatasets = "ce6b1742-4840-55fa-b093-852dadbb1d8b" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[targets] +test = ["RDatasets", "Test"] diff --git a/src/imputors/interp.jl b/src/imputors/interp.jl index 7a4b0af..a2a8c92 100644 --- a/src/imputors/interp.jl +++ b/src/imputors/interp.jl @@ -27,16 +27,14 @@ function impute!(imp::Interpolate, ctx::Context, data::AbstractVector{<:Union{T, diff = data[next_idx] - data[prev_idx] incr = diff / T(gap_sz + 1) - start_val = data[prev_idx] - stop_val = data[next_idx] + val = data[prev_idx] + incr - values = Real(start_val):Real(incr):Real(stop_val) + # Iteratively fill in the values + for j in i:(next_idx - 1) + data[j] = val + val += incr + end - idx_range = prev_idx:(prev_idx + length(values) - 1) - # println(collect(idx_range)) - # println(values) - - data[idx_range] = values i = next_idx else break diff --git a/test/runtests.jl b/test/runtests.jl index 3d8754d..1a268e5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -21,6 +21,18 @@ using Statistics result = impute(a, :interp; limit=0.2) @test result == collect(1.0:1.0:20) @test result == interp(a) + + # Test interpolation between identical points + b = ones(Union{Float64, Missing}, 20) + b[[2, 3, 7]] .= missing + @test interp(b) == ones(Union{Float64, Missing}, 20) + + # Test interpolation at endpoints + b = ones(Union{Float64, Missing}, 20) + b[[1, 3, 20]] .= missing + result = interp(b) + @test ismissing(result[1]) + @test ismissing(result[20]) end @testset "Fill" begin