How to transform the subtraction field between dates in python into int?

Asked

Viewed 51 times

1

Hello I have the following code that turns a "Brazilian" date into a standard date:

df2['Start_Date'] = df2['Start_Date'].apply(lambda x: datetime.strptime(x, '%d/%m/%Y'))
df2['End_Date'] = df2['End_Date'].apply(lambda x: datetime.strptime(x, '%d/%m/%Y'))
df2['Days_Considered'] = df2['End_Date'] -  df2['Start_Date']

The result would be:

Days_conidered

x days

How can I turn this df column ('Days_considered') into an integer? Since it will appear for example: 9 days. I’ve tried using int(), but it goes wrong.

1 answer

3


The column Days_Considered is from guy dtype: timedelta64[ns]. To access the value of days as an integer, use .dt.days:

df2['Days_Considered'] = (df2['End_Date'] -  df2['Start_Date']).dt.days
  • Thank you so much!!! It worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.