Angus Angus - 1 year ago 283
Python Question

Calculate difference between 2 dates in python dataframe

I've looked through python advice and worked out how to calculate the difference between two dates, e.g. Difference between two dates?. That works, but ... I'm working with variables in a dataframe. I'm sure I'm following the advice I've read but I'm getting:

TypeError: strptime() argument 1 must be str, not Series

Here's the code:

df['DAYSDIFF'] = (datetime.datetime.strptime(df['SDATE'],"%d/%m/%Y") - datetime.datetime.strptime(df['QDATE'],"%d/%m/%Y"))

Thanks again for help!

Answer Source

Use pandas.to_datetime:

df["SDATE"] = pd.to_datetime(df["SDATE"], format="%d/%m/%Y")
df["QDATE"] = pd.to_datetime(df["QDATE"], format="%d/%m/%Y")

df["DAYSDIFF"] = df["SDATE"] - df["QDATE"]

Because datetime.strptime does not recognize the pandas Series and is expecting a string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download