Python: Convert timedelta to int in a dataframe
PythonPandasTimedeltaPython Problem Overview
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
Python Solutions
Solution 1 - Python
The Series class has a pandas.Series.dt
accessor object with several
useful datetime attributes, including dt.days
. Access this attribute via:
timedelta_series.dt.days
You can also get the seconds
and microseconds
attributes in the same way.
Solution 2 - Python
You could do this, where td
is your series of timedeltas. The division converts the nanosecond deltas into day deltas, and the conversion to int drops to whole days.
import numpy as np
(td / np.timedelta64(1, 'D')).astype(int)
Solution 3 - Python
Timedelta objects have read-only instance attributes .days
, .seconds
, and .microseconds
.
Solution 4 - Python
If the question isn't just "how to access an integer form of the timedelta?" but "how to convert the timedelta column in the dataframe to an int?" the answer might be a little different. In addition to the .dt.days
accessor you need either df.astype
or pd.to_numeric
Either of these options should help:
df['tdColumn'] = pd.to_numeric(df['tdColumn'].dt.days, downcast='integer')
or
df['tdColumn'] = df['tdColumn'].dt.days.astype('int16')
Solution 5 - Python
I think it's much easier way to it with this (where dif
is the difference between dates):
dif_In_Days = dif.days
Solution 6 - Python
The simplest way to do this is by
df["DateColumn"] = (df["DateColumn"]).dt.days