1

How can I filter a dataframe by the hour of a date column?

let df = df.lazy().select([
    col("value").filter(
      col("date").dt().hour().gt(7).lt(20)

  ).alias("hours").sum(),
]).collect()?;

Whith that I get an Error

Error: Invalid operation operation not supported on dtype Date

Brucie Alpha
  • 1,135
  • 2
  • 12
  • 31
  • 2
    `Date`s don't store the hours. Therefore you can't use `.hour()` on them. Your column has to be of type `DateTime` or `NativeDateTime` or something along the lines. If you provide a [mre] maybe we can help further. – cafce25 Dec 09 '22 at 12:59
  • Ahhh thanks i forgot that I manually parsed the date column and there I used `DataType::Date` – Brucie Alpha Dec 09 '22 at 13:11

1 Answers1

1

Thanks to cafce25 I found the Error. It was in how I parsed the date column

let options = StrpTimeOptions {
        fmt: Some("%Y-%m-%d %H:%M:%S".into()),
        date_dtype: DataType::Date,
        ..Default::default()
    };

vs

let options = StrpTimeOptions {
        fmt: Some("%Y-%m-%d %H:%M:%S".into()),
        date_dtype: DataType::Datetime(TimeUnit::Milliseconds, Option::None),
        ..Default::default()
    };
Brucie Alpha
  • 1,135
  • 2
  • 12
  • 31