Python: Convert timedelta to int in a dataframe

Asaf Hanish picture Asaf Hanish · Sep 3, 2014 · Viewed 212.5k times · Source

I would like to create a column in a pandas data frame that is an integer representation of the number of days in a timedelta column. Is it possible to use 'datetime.days' or do I need to do something more manual?

timedelta column

7 days, 23:29:00

day integer column

7

Answer

abeboparebop picture abeboparebop · Feb 15, 2017

Use the dt.days attribute. Access this attribute via:

timedelta_series.dt.days

You can also get the seconds and microseconds attributes in the same way.