docs/user-guide/transformations/time-series/filter.md
Filtering date columns works in the same way as with other types of columns using the .filter method.
Polars uses Python's native datetime, date and timedelta for equality comparisons between the datatypes pl.Datetime, pl.Date and pl.Duration.
In the following example we use a time series of Apple stock prices.
{{code_block('user-guide/transformations/time-series/filter','df',['read_csv'])}}
--8<-- "python/user-guide/transformations/time-series/filter.py:df"
We can filter by a single date by casting the desired date string to a Date object
in a filter expression:
{{code_block('user-guide/transformations/time-series/filter','filter',['filter'])}}
--8<-- "python/user-guide/transformations/time-series/filter.py:filter"
Note we are using the lowercase datetime method rather than the uppercase Datetime data type.
We can filter by a range of dates using the is_between method in a filter expression with the start and end dates:
{{code_block('user-guide/transformations/time-series/filter','range',['filter','is_between'])}}
--8<-- "python/user-guide/transformations/time-series/filter.py:range"
Say you are working with an archeologist and are dealing in negative dates.
Polars can parse and store them just fine, but the Python datetime library
does not. So for filtering, you should use attributes in the .dt namespace:
{{code_block('user-guide/transformations/time-series/filter','negative',['strptime'])}}
--8<-- "python/user-guide/transformations/time-series/filter.py:negative"