I have a dataframe like below.
Id | arr_date | dep_date |
---|---|---|
1 | 2022-01-12 | 2022-02-01 |
2 | 2022-01-20 | 2022-03-01 |
I want to do the comparison like,
arr_date
<= dep_date
days(7).
How can I do this comparison using df.query() method in pandas? Can anyone please help me?
PS: I know how to do the comparison without df.query(). I only want to know how to do it with df.query()
CodePudding user response:
You can do it all in query
and without extra imports like so:
>>> df.query('arr_date <= dep_date.add(@pd.to_timedelta("7d"))')
Id arr_date dep_date
0 1 2022-01-12 2022-02-01
1 2 2022-01-20 2022-03-01
CodePudding user response:
Try the following code.
import pandas as pd
from datetime import date, timedelta
# Test data
raw_data = {
"arr_date" : [date(2022, 1, 12), date(2022, 1, 20), date(2022, 5, 2)],
"dep_date" : [date(2022, 2, 1) , date(2022, 3, 1) , date(2022, 3, 1)],
}
df = pd.DataFrame(data=raw_data)
#print(df.info())
print("org-data:")
print(df)
df2 = df[ df["arr_date"]<=df["dep_date"] timedelta(days=7) ]
print("filtering-data:")
print(df2)
output
org-data:
arr_date dep_date
0 2022-01-12 2022-02-01
1 2022-01-20 2022-03-01
2 2022-05-02 2022-03-01
filtering-data:
arr_date dep_date
0 2022-01-12 2022-02-01
1 2022-01-20 2022-03-01
Adds the date of use of the 'timedelta' class.
df2 = df[ df["arr_date"]<=df["dep_date"] timedelta(days=7) ]