4

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')')
2
  • Why are you using a timedelta(days=6) (six days ago) in Python and comparing against grabbing yesterday (one day ago) in R? Commented Apr 1, 2020 at 22:17
  • Sorry, that's confusing and a typo. So for test purposes the data set I built meant the date variable ended 6 days ago. Sorry it seems a bit confusing. Commented Apr 2, 2020 at 8:49

3 Answers 3

4

Try: df.query('date >= @yesterday'). You need @ so pandas recognizes it's a variable.

Sign up to request clarification or add additional context in comments.

Comments

3

IIUC, you want to create an outside varible to use inside your query?

from the docs

You can refer to variables in the environment by prefixing them with an ‘@’ character like @a + b.

using pandas only

import pandas as pd

df = pd.DataFrame({'date' : pd.date_range('01-02-2020','01-03-2021',freq='D')})
df = df.set_index('date')

delta = (pd.Timestamp('today') - pd.DateOffset(days=1)).strftime('%d-%m-%y')

df.query(f"date <= @delta")

  date
  2020-01-02
  2020-01-03
  2020-01-04
  2020-01-05
  2020-01-06

Comments

2

you can do it with string formatting:

df.query(f'date>= "{pd.Timestamp.today() - pd.Timedelta(days=6)}"')

Note: I tried with pd.Timestamp and pd.Timedelta but I'm sure it will work with date and timedelta as you used

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.