nik
nik

Reputation: 2294

Use a list of filters within polars

Is there a way to filter a polars DataFrame by multiple conditions?

This is my use case and how I currently solve it, but I wonder how to solve it, if my list of dates would be longer:

dates = ["2018-03-25", "2019-03-31", "2020-03-29"]
timechange_forward = [(datetime.strptime(x+"T02:00", '%Y-%m-%dT%H:%M'), datetime.strptime(x+"T03:01", '%Y-%m-%dT%H:%M')) for x in dates]

df.filter(
    pl.col("time").is_between(*timechange_forward[0]) | 
    pl.col("time").is_between(*timechange_forward[1]) | 
    pl.col("time").is_between(*timechange_forward[2])
) 

Upvotes: 6

Views: 4212

Answers (3)

okayama-taro
okayama-taro

Reputation: 55

I did it, by using duckdb with polars

import duckdb
sqlstrings = """
  ****
  ****
"""
print(duckdb.sql(sqlstrings).pl())

Upvotes: -2

jqurious
jqurious

Reputation: 21164

You can pass multiple conditions to .any_horizontal()

df.filter(
   pl.any_horizontal(
      pl.col("time").is_between(*time)
      for time in timechange_forward
   )
)

Upvotes: 10

ignoring_gravity
ignoring_gravity

Reputation: 10476

You haven't made your example reproducible, so it's hard to test this, but how about

import functools
import operator

conditions = [
    pl.col("time").is_between(*val)
    for val in timechange_forward
]

df.filter(functools.reduce(operator.or_, conditions))

?

Upvotes: 4

Related Questions