Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinearInterpolation not working with transform in DataFrames.jl

I have defined the following linear interpolator:

julia> using DataFrames, Interpolations

julia> xs = 1:0.2:5;

julia> ys = log.(xs);

julia> li = LinearInterpolation(xs, ys);

and have a data frame:

julia> df = DataFrame(x=2:0.1:3)
11×1 DataFrame
 Row │ x
     │ Float64
─────┼─────────
   1 │     2.0
   2 │     2.1
   3 │     2.2
   4 │     2.3
   5 │     2.4
   6 │     2.5
   7 │     2.6
   8 │     2.7
   9 │     2.8
  10 │     2.9
  11 │     3.0

I can pass the :x column of a data frame to li like this:

julia> li(df.x)
11-element Vector{Float64}:
 0.6931471805599453
 0.7408022704621078
 0.7884573603642704
 0.831963048859085
 0.8754687373538997
 0.915490091190668
 0.9555114450274363
 0.9925654311042973
 1.0296194171811581
 1.0641158529246337
 1.0986122886681098

However, when I try using the transform function it fails:

julia> transform(df, :x => li => :y)
ERROR: ArgumentError: Unrecognized column selector: :x => (21-element extrapolate(scale(interpolate(::Vector{Float64}, BSpline(Linear())), (1.0:0.2:5.0,)), Throw()) with element type Float64:

and throws a strange error that I do not understand. How to resolve this issue?

like image 690
Bogumił Kamiński Avatar asked Nov 11 '21 09:11

Bogumił Kamiński


1 Answers

The problem you encounter is that li is not defined by LinearInterpolations.jl to have a type that is a subtype of Function:

julia> li isa Function
false

Objects such as li are called functors in Julia and sometimes their authors do not opt-in for making them a subtype of Function. In such a case the simplest solution is to use an anonymous function as a work-around:

julia> transform(df, :x => (x -> li(x)) => :y)
11×2 DataFrame
 Row │ x        y
     │ Float64  Float64
─────┼───────────────────
   1 │     2.0  0.693147
   2 │     2.1  0.740802
   3 │     2.2  0.788457
   4 │     2.3  0.831963
   5 │     2.4  0.875469
   6 │     2.5  0.91549
   7 │     2.6  0.955511
   8 │     2.7  0.992565
   9 │     2.8  1.02962
  10 │     2.9  1.06412
  11 │     3.0  1.09861
like image 86
Bogumił Kamiński Avatar answered Nov 15 '22 07:11

Bogumił Kamiński