Home > Software engineering >  Setting a date 8 days inside a value in a pandas column
Setting a date 8 days inside a value in a pandas column

Time:10-25

I want to create a new column in pandas in which I get a comment that says: Opened today() need update in today 8 days. This is what I've got, but have not been able to fix this error.

This is my code:

import pandas as pd
import datetime
from datetime import timedelta

Today = datetime.date.today() 

def add_days_to_date(date, days):
    subtracted_date = pd.to_datetime(date)   timedelta(days=days)
    subtracted_date = subtracted_date.strftime("%m-%d")

    return(subtracted_date)

RepliedSent_date = ("Opened", Today, "need update", add_days_to_date(Today, 8))

df.loc[(df['col1'] != ' ') & (df['col2'] != ' '), "Replied/sent date"] = RepliedSent_date 

Any help would be greatly appreciated! Thanks.

CodePudding user response:

df['Replied/sent date']=(df.loc[(df['col1'] != ' ') & (df['col2'] != ' ')]['date']
                         .apply(lambda x: "Opened "   str(x.strftime('%F') )   " , need update, "   str((x  pd.offsets.Day(8)).strftime('%F')))
)
df
col1    col2    date    Replied/sent date
0           2021-01-04  Opened 2021-01-04 , need update, 2021-01-12
1           2021-01-04  Opened 2021-01-04 , need update, 2021-01-12
2           2021-01-04  Opened 2021-01-04 , need update, 2021-01-12
3           2021-01-04  Opened 2021-01-04 , need update, 2021-01-12
4           2021-01-04  Opened 2021-01-04 , need update, 2021-01-12
5           2021-01-04  Opened 2021-01-04 , need update, 2021-01-12
6           2021-01-04  Opened 2021-01-04 , need update, 2021-01-12
7           2021-01-04  Opened 2021-01-04 , need update, 2021-01-12
8           2021-01-04  Opened 2021-01-04 , need update, 2021-01-12

CodePudding user response:

You can't assing = RepliedSent_date to get result.

You have to create function RepliedSent_date(date)

def RepliedSent_date(date):
    return f"Opened {date} need update {add_days_to_date(date, 8)}"

or

RepliedSent_date = lambda date: f"Opened {date} need update {add_days_to_date(date, 8)}"

and use .apply() to execute this function on every row.

mask = ...
df.loc[mask] = df.loc[mask].apply(RepliedSent_date)

Minimal working code with example data:

import pandas as pd
from datetime import timedelta

# --- functions ---

def add_days_to_date(date, days):
    subtracted_date = pd.to_datetime(date)   timedelta(days=days)
    subtracted_date = subtracted_date.strftime("%m-%d")
    return subtracted_date

def replied_sent_date(date):
    return f"Opened {date} need update {add_days_to_date(date, 8)}"

#replied_sent_date = lambda date: f"Opened {date} need update {add_days_to_date(date, 8)}"

# --- main ---

df = pd.DataFrame({
    'col1': ['A', 'B', 'C', ' '],
    'col2': ['X', 'Y', 'Z', ' '],
    'Replied/sent date': ['2022.10.24', '2022.10.23', '2022.10.25', '2000.01.01'],
})

mask = (df['col1'] != ' ') & (df['col2'] != ' ')
df.loc[mask, "Replied/sent date"] = df.loc[mask, "Replied/sent date"].apply(replied_sent_date)

print(df)

Result:

  col1 col2                    Replied/sent date
0    A    X  Opened 2022.10.24 need update 11-01
1    B    Y  Opened 2022.10.23 need update 10-31
2    C    Z  Opened 2022.10.25 need update 11-02
3                                     2000.01.01
  • Related