Search code examples
pythonpandaspandasql

date compare in where clause of pandasql sqldf


I am using pandasql sqldf to query over a dataframe , but unable to compare the date in where clause , getting invalid syntax error.

from pandasql import sqldf
import pandas as pd

df = pd.DataFrame([['2015-05-07','2021-05-07'], ['2020-03-06','2018-06-08']],columns= ['date1','date2'])

start = '2020-01-01'
dt= '2015-05-07'
testdf = sqldf('select * from df where strftime('%Y-%m-%d', start) > strftime('%Y-%m-%d',dt))

Solution

  • You are facing formatting issues

    testdf = sqldf('select * from df where strftime("%Y-%m-%d", {}) > strftime("%Y-%m-%d",{})'.format(start,dt))
    

    Output:

           date1       date2
    0  2015-05-07  2021-05-07
    1  2020-03-06  2018-06-08