Merging dataframe based on different conditions

43 Views Asked by At

I'm doing the transition from SAS to python.

In SAS

proc SQL;
    create table ABC as
    select a.*, b.*
    from table_1 as a inner join
    table_2 as b
    on a.ID = on b.ID and
    a. week date \>= b. start date and a. week date \<= b. end date;
quit;

When I tried the above code in SAS, the observations between table a and table ABC are matched. But when I tried in python, I'm getting less number of observations compared to SAS. The week date, start date, end date are date variables in the format '2019-05-21'. whenever I used >=/<= in the date variables it shows an error like this.

TypeError: '>=' not supported between instances of 'Timestamp' and 'str'.

ABC =a. merge (b, left_on='ID', right_on='ID', how='left')

ABC [(ABC ['week date']>= (ABC ['start date '])) & (ABC ['week date'] \<= (ABC ['end date']))]
1

There are 1 best solutions below

1
Maciej On

In python, if a & b are DataFrame compare type of variables (a.dtypes and b.dtypes)