Problem with calculation od days from date in DataFrame in Python Pandas

I have DataFrame like below:

df = pd.DataFrame({"data" : ["02.01.2020"]})
df["data"] = pd.to_datetime(df["data"])

And list of special dates:

special_date = pd.to_datetime(["04.01.2020", "01.01.2020"], dayfirst=True)

ANd I need to calculate 2 columns in this DataFrame:

col1 = number of days to the next special date
col2 = number of days from the last special date \

So I need result like below:
col1 = 2 because next special date from 02.01.2020 will be for 2 days (04.01.2020)
col2 = 1 because last special date from 02.01.2020 was 1 day ago (01.01.2020)

enter image description here

1 answer

  • answered 2021-01-19 12:07 Celius Stingher

    You can simply use the - sign operator:

    df = pd.DataFrame({"data" : ["02.01.2020"]})
    df["data"] = pd.to_datetime(df["data"],dayfirst=True)
    special_date = pd.to_datetime(["04.01.2020", "01.01.2020"], dayfirst=True)
    df['col1'] = abs(df['data'] - special_date[0])
    df['col2'] = df['data'] - special_date[1]
    

    This outputs:

            data   col1   col2
    0 2020-01-02 2 days 1 days
    

    If you'd like to have only the number, then you can add .dt.days after the calculations (abs(df['data'] - special_data[0]).dt.days which outputs:

            data  col_1  col_2
    0 2020-01-02      2      1