Skip to content

Method equivalent to query/eval in pandas #136

Answered by markfairbanks
mpecovnik asked this question in Q&A
Discussion options

You must be logged in to vote

Two steps to convert this to tidypolars:

  • .query() is the pandas way of filtering a data frame. That is done using .filter() in tidypolars.
  • Your query string needs to use tidypolars col() syntax so you can use it to create an expression
import tidypolars as tp
from tidypolars import col

df = tp.Tibble(x = range(3), y = range(3))

# Query string contained in config
query_string = "(col('x') > 0) & (col('y') < 2)"

# Run eval to turn this into a tidypolars expression
query_expr = eval(query_string)

# Execute the expression inside a tidypolars method
queried = df.filter(query_expr)

queried
┌─────┬─────┐
│ xy   │
│ ------ │
│ i64i64 │
╞═════╪═════╡
│ 11   │
└─────┴─────┘

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mpecovnik
Comment options

Answer selected by markfairbanks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants