Julia (Julia-lang) How to get a subset of a dataframe and count the different activities by group?

Viewed 299

I am trying to get a subset of a dataframe. The starting point have to be the first day of the current month. The end point have to be the current day. After that, I want to count the different activities by group.

The dataframe :

date        | activity

06-07-2020  | walking

06-07-2020  | rucking

07-07-2020  | tennis
...

I tried that :

DfName[Dates.firstdayofmonth(Dates.today()):Dates.today(), :]

Result :

ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Dates.Day

I also tried that :

combine(groupby(DfName, :activity), nrow => :count)    

@where(DfName, :date .>= Dates.firstdayofmonth(Dates.today()), :date .<= Dates.today())`

Result :

@where(DfName, :date .>= Dates.firstdayofmonth(Dates.today()), :date .<= Dates.today())
ERROR: MethodError: no method matching isless(::Dates.Date, ::String)
Closest candidates are:
  isless(::DataValues.DataValue{Union{}}, ::Any) at /home/...
  isless(::PyObject, ::Any) at /home/...

How can I do that in Julia ?

1 Answers

Use the following:

filter(:date => x -> Dates.firstdayofmonth(Dates.today()) <= x <= Dates.today(), DfName)

or

filter(row -> Dates.firstdayofmonth(Dates.today()) <= row.x <= Dates.today(), DfName)

or

DfName[Dates.firstdayofmonth(Dates.today()) .<= DfName.date .<= Dates.today(), :]

or (soon in DataFrames.jl 1.0 release):

subset(DfName,
       :date => ByRow(>=(Dates.firstdayofmonth(Dates.today())),
       :date => ByRow(<=(Dates.today())
      )
Related