On this page:
1.4.2.1 Filtering by single dates
1.4.2.2 Filtering by a date range
1.4.2.3 Filtering with negative dates

1.4.2 Filtering🔗ℹ

A date column filters like any other. Its comparisons take gregor values, as Python’s take date, datetime and timedelta:

> (define stock (read-csv "apple_stock.csv" #:try-parse-dates #t))
> stock

shape: (100, 2)

┌────────────┬────────┐

│ Date       ┆ Close  │

│ ---        ┆ ---    │

│ date       ┆ f64    │

╞════════════╪════════╡

│ 1981-02-23 ┆ 24.62  │

│ 1981-05-06 ┆ 27.38  │

│ 1981-05-18 ┆ 28.0   │

│ 1981-09-25 ┆ 14.25  │

│ 1982-07-08 ┆ 11.0   │

│ …          ┆ …      │

│ 2012-05-16 ┆ 546.08 │

│ 2012-12-04 ┆ 575.85 │

│ 2013-07-05 ┆ 417.42 │

│ 2013-11-07 ┆ 512.49 │

│ 2014-02-25 ┆ 522.06 │

└────────────┴────────┘

1.4.2.1 Filtering by single dates🔗ℹ
> (filter stock (= (col "Date") (datetime 1995 10 16)))

shape: (1, 2)

┌────────────┬───────┐

│ Date       ┆ Close │

│ ---        ┆ ---   │

│ date       ┆ f64   │

╞════════════╪═══════╡

│ 1995-10-16 ┆ 36.13 │

└────────────┴───────┘

A gregor datetime compares with a date column as midnight of that day, and a gregor date works as well.

1.4.2.2 Filtering by a date range🔗ℹ
> (filter stock (is-between "Date" (datetime 1995 7 1) (datetime 1995 11 1)))

shape: (2, 2)

┌────────────┬───────┐

│ Date       ┆ Close │

│ ---        ┆ ---   │

│ date       ┆ f64   │

╞════════════╪═══════╡

│ 1995-07-06 ┆ 47.0  │

│ 1995-10-16 ┆ 36.13 │

└────────────┴───────┘

1.4.2.3 Filtering with negative dates🔗ℹ

Polars parses and stores dates before year 1. Python’s datetime cannot hold them, so upstream filters on the year; gregor can, so a Racket date compares directly too:

> (define negative-dates
    (~> (dataframe (list (series '("-1300-05-23" "-1400-03-02") #:name "ts")
                         (series '(3 4) #:name "values")))
        (with-columns (str->date "ts"))))
> (filter negative-dates (< (dt-year "ts") -1300))

shape: (1, 2)

┌─────────────┬────────┐

│ ts          ┆ values │

│ ---         ┆ ---    │

│ date        ┆ i64    │

╞═════════════╪════════╡

│ -1400-03-02 ┆ 4      │

└─────────────┴────────┘

> (filter negative-dates (= (col "ts") (date -1300 5 23)))

shape: (1, 2)

┌─────────────┬────────┐

│ ts          ┆ values │

│ ---         ┆ ---    │

│ date        ┆ i64    │

╞═════════════╪════════╡

│ -1300-05-23 ┆ 3      │

└─────────────┴────────┘