1

I have an issue understanding the signatures of Frame.map (and eventually others too) when I could figure out easily Series.map.

Here a quick example:

let ts1 = 
  series
   [ ("A", "A1", "p1") => 0.5 
     ("A", "A1", "p2") => 2.
     ("A", "A2", "p3") => 2. 
     ("A", "A2", "p4") => 0.5 ]

let ts2 = 
  series
   [ ("A", "A1", "p1") => "A"
     ("A", "A1", "p2") => "B"
     ("A", "A2", "p3") => "C"
     ("A", "A2", "p4") => "D" ]

let F = ["ts1" =?> ts1 ; "ts2" =?> ts2] |> Frame.ofColumns

Let's say that I want to apply a function on the Series ts1, there no issue:

ts1 |> Series.map (fun _ v -> v * 2.)

Works as expected...

But if I want to use, let's say for the example:

let fun1 (a: float) (b: string) =
    match b with
    |"B" -> a ** 2.
    |"C" -> a ** 3.
    |_   -> 0.

On the Frame F, I just cannoy figure it out. I have tried calling columns.

I would not mind some help, as I cannot figure it out with previous posts/Deedle doc/etc... I am just stucked at the signature...

let F2 = F |> Frame.mapRows (fun k a -> (fun1 a.GetAs<float>("ts1") a.GetAs<string>("ts2")))

1 Answer 1

2

This kind of operation is very intuitive in MATLAB/R/Python using vectorized indexing.

In Deedle, Frame.mapRows will get every row as an ObjectSeries. My ugly solution would be like this

F
|> Frame.mapRows(fun _ v -> 
  match string v.["ts2"] with
  | "B" -> v |> Series.map(fun k v -> if k = "ts1" then box((unbox v) * 2.) else v)
  | "C" -> v |> Series.map(fun k v -> if k = "ts1" then box((unbox v) * 3.) else v)
  | _ -> v |> Series.map(fun k v -> if k = "ts1" then box(0.) else v)
)
|> Frame.ofRows
2
  • thanks zyzhu but I am indeed curious if there is something nicer
    – Jeff_hk
    Feb 27, 2019 at 1:31
  • Just for information, what I do not like with this solution is the fact that I woukld like to use more complex functions and/or pass it in parameter.
    – Jeff_hk
    Feb 28, 2019 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.