SBad
SBad

Reputation: 1345

How can I remove "days" when computing the difference between two dates?

When computing the difference between two dates using the following:

df_test['Difference'] = (df_test['First_Date'] - df_test['Second Date'])

I get a third column with "x Days".

How can I convert "x Days" into int "x". e.g., "50 days" into "50"?

Someone in a previous thread suggested to use:

df_test['Difference'] = (df_test['First_Date'] - df_test['Second Date']).dt.days

but using that I get an error of type:

"'Series' object has no attribute 'dt"

How I can fix the issue please?

Upvotes: 2

Views: 9865

Answers (4)

Japleen Gulati
Japleen Gulati

Reputation: 11

By converting each of the subtraction variables to datetime, you can resolve the error you're getting. The following should work:

df_test['Difference'] = (pd.to_datetime(df_test['First_Date']).dt.date - pd.to_datetime(df_test['Second Date']).dt.date).dt.days

Upvotes: 1

Manoj
Manoj

Reputation: 83

Function

def duration(diff):
    tsec = diff.total_seconds()
    hh = int(tsec//3600)
    mm = int((tsec%3600)//60)
    ss = int((tsec%3600)%60)
    return('{:02d}:{:02d}:{:02d}'.format(hh,mm,ss))

Test Results:

import datetime as dt

d1 = dt.datetime(2020, 10, 21, 10, 15, 12) - dt.datetime(2020, 10, 20,  8, 5, 12)
d2 = dt.datetime(2020, 10, 21, 10, 15, 12) - dt.datetime(2020, 10, 21,  8, 0,  0)
d3 = dt.datetime(2020, 10, 21, 10, 15, 12) - dt.datetime(2020, 10, 22, 10, 5,  0)
print(duration(d1))   #    26:10:00
print(duration(d2))   #    02:15:12
print(duration(d3))   #   -24:10:12

Output

 26:10:00
 02:15:12
-24:10:12

Upvotes: 0

Rao Sahab
Rao Sahab

Reputation: 1281

If it is a string variable. then you can do the following and strip of the unwanted part. (say your variable is xDays)

df["xDays"] = df["xDays"].map(lambda x: x[:-5])

if it is a timedelta value you can do following

df["xDays"].dt.days

Upvotes: 9

Zsolt Diveki
Zsolt Diveki

Reputation: 159

If I stick to your code but fill in with made up data, this modification works for me. Basically you reassign the df_test['Difference'].dt.days to df_test['Difference'] :

df_test=pd.DataFrame(dict(zip(['First_Date', 'Second_date'], [pd.date_range("2018-01-01",periods=10), pd.date_range("2017-01-01",periods=10)])))
df_test['Difference'] = df_test['First_Date'].sub(df_test['Second_date'], axis=0)
df_test['Difference']=df_test['Difference'].dt.days

Upvotes: 0

Related Questions