I am trying to filter a Pandas df by dates (today and yesterday). For automation purposes I wish to filter using a timestamp function. This is pretty seamless in R:
df %>%
filter(date >= today() - 1)
However, my attempts to replicate in Pandas are not reaching any success so far: Yesterday comes out fine, but .query() doesnt recognise it?
yesterday = (date.today() - timedelta(days=6)).strftime('%Y-%m-%d')
df.\
query('date >= yesterday')
Ideally I am seeking something all encompassing like:
df.\
query('date >= (date.today() - timedelta(days=6)).strftime('%Y-%m-%d')')
Try: df.query('date >= @yesterday')
. You need @
so pandas recognizes it's a variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With