How to filter Date data column in a DataFrame

Hi everyone,

I have a DataFrame with Dates type in one column named “Date” (the rest of the columns are with regular data). I want to filter the dataframe to have data only between years 2015 and 2019.
I tried:

df[@. 2015 <= Dates.year(df.Date) <= 2019, :]

However, I get the following error:

MethodError: no method matching getindex(::DataFrames.DataFrame, ::Tuple{BitVector, Colon})

The data type dump(df.Date) is:

Array{Dates.Date}((120,))
  1: Dates.Date
    instant: Dates.UTInstant{Dates.Day}
      periods: Dates.Day
        value: Int64 735264
...

Any ideas on how to do that efficiently?

Have you tried writing all the dots instead of using the macro?

df[2015 .<= Dates.year.(df.Date) .<= 2019, :]
1 Like

Manually adding dots works as does

df[(@. 2015 <= Dates.year(df.Date) <= 2019), :]

Otherwise the @. grabs the whole tuple and that gets passed as one argument rather than as two separate arguments.

2 Likes

You guys helped a-lot! I couldn’t figure out what’s wrong