Domanda

In order to get comfortable with Deedle I made up a CSV file that represents a log of video rentals.

   RentedOn,Shop,Title
   12/dec/2013 00:00:00,East,Rambo
   12/dec/2013 00:00:00,West,Rocky
   12/dec/2013 00:00:00,West,Rambo
   12/dec/2013 00:00:00,East,Rambo
   13/dec/2013 00:00:00,East,Rocky
   13/dec/2013 00:00:00,East,Rocky
   13/dec/2013 00:00:00,East,Rocky
   14/dec/2013 00:00:00,West,Rocky 2

I have the following function, that groups the rentals by Shop (East or West):

let overview =
    __SOURCE_DIRECTORY__ + "/rentallog.csv"
    |> Frame.ReadCsv
    |> Frame.groupRowsByString "Shop"
    |> Frame.nest
    |> Series.map (fun dtc df -> 
        df.GetSeries<string>("Title") |> Series.groupBy (fun k v -> v)
        |> Frame.ofColumns |> Frame.countValues )
    |> Frame.ofRows

I'd like to be able to filter the rows by the date in the RentedOn col, however, I'm not sure how to do this. I know its probably using the Frame.filterRowValues function but I'm unsure the best way to use this. Any guidance on how to filter would be appreciated.

Update based on @jeremyh advice

let overview rentedOnDate =
    let addRentedDate (f:Frame<_,_>) = 
        f.AddSeries ("RentedOnDate", f.GetSeries<DateTime>("RentedOn"))
        f
    __SOURCE_DIRECTORY__ + "/rentallog.csv"
    |> Frame.ReadCsv
    |> addRentedDate
    |> Frame.filterRowValues (fun row -> row.GetAs<DateTime>("RentedOnDate") = rentedOnDate)
    |> Frame.groupRowsByString "Shop"
    |> Frame.nest
    |> Series.map (fun dtc df -> 
        df.GetSeries<string>("Title") |> Series.groupBy (fun k v -> v)
        |> Frame.ofColumns |> Frame.countValues )
    |> Frame.ofRows

Thanks, Rob

È stato utile?

Soluzione

Hey I think that you might get a faster answer if you add an f# tag to your question too.

I used the following link to answer your question which has some helpful examples.

This is the solution I came up with. Please note that I added a new column RentedOnDate that actually has a DateTime type that I do the filtering on.

let overview rentedOnDate =
    let rentalLog = 
        __SOURCE_DIRECTORY__ + "/rentallog.csv"
        |> Frame.ReadCsv
    rentalLog
    |> Frame.addSeries "RentedOnDate" (rentalLog.GetSeries<DateTime>("RentedOn"))
    |> Frame.filterRowValues (fun row -> row.GetAs<DateTime>("RentedOnDate") = rentedOnDate)
    |> Frame.groupRowsByString "Shop"
    |> Frame.nest
    |> Series.map (fun dtc df -> 
        df.GetSeries<string>("Title") |> Series.groupBy (fun k v -> v)
        |> Frame.ofColumns |> Frame.countValues )
    |> Frame.ofRows

// Testing
overview (DateTime.Parse "12/dec/2013 00:00:00")
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top