Search code examples
f#deedle

Remove index from Deedle Frame


Suppose a dataframe is loaded like so:

let fr = Frame.ReadCsv("somedata.txt", hasHeaders=true, separators="\t") 
             |> Frame.indexRowsDate "Datetime"

The output of which looks something like this:

                        Open     High     Low      Close    
5/14/2015 7:02:00 AM -> 21.00000 21.01000 21.00000 21.01000 
5/14/2015 7:03:00 AM -> 22.01000 22.01000 22.93000 22.9500  

How do you transform the dataframe into the following:

     Datetime             Open     High     Low      Close    
0 -> 5/14/2015 7:02:00 AM 21.00000 21.01000 21.00000 21.01000 
1 -> 5/14/2015 7:03:00 AM 22.01000 22.01000 22.93000 22.9500  

Basically, how are indexes removed from a deedle dataframe and turned into a column of the dataframe.


Solution

  • Using the F#-friendly Deedle API, you can use addCol to add keys as a new column and then you can use indexRowsOrdinarilly to replace the original keys with plain numerical index:

    df 
    |> Frame.addCol "Keys" (df |> Frame.mapRows (fun k _ -> k))
    |> Frame.indexRowsOrdinally
    

    Alternatively, the first could also be done imperatively, which is a bit shorter:

    f.AddColumn("Keys", f.RowKeys)
    f |> Frame.indexRowsOrdinally