pythonrdata.tablepy-datatable

What is the Python equivalent to R data.table column creation?


I'm in the process of converting my R scripts to python. Is there a similar process in creating new columns that r data.table does in the J step? Below is my example code in R:

dat[,Returned_on_time:= ifelse(Contract_End_Date==Estimated_In_Date,'Yes','No')]

Thanks


Solution

  • Your R example illustrates data.table's fast in-place generation of your new column.

    A python equivalent of in-place creation in the j slot is shown below using python datatable

    from datatable import dt,f,update
    
    dat=dt.Frame({"Contract_End_Date":["2022-01-02", "2022-04-15", "2022-12-19"],
                  "Estimated_In_Date":["2022-04-26", "2022-04-15", "2022-11-30"]
                 })
    
    dat[:, dt.update(Returned_on_time = dt.ifelse(f.Contract_End_Date == f.Estimated_In_Date, "Yes", "No"))]
    

    Output:

       | Contract_End_Date  Estimated_In_Date  Returned_on_time
       | str32              str32              str32           
    -- + -----------------  -----------------  ----------------
     0 | 2022-01-02         2022-04-26         No              
     1 | 2022-04-15         2022-04-15         Yes             
     2 | 2022-12-19         2022-11-30         No              
    [3 rows x 3 columns]