Reputation: 2069
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
Upvotes: 205
Views: 492215
Reputation: 181
A great way to do this is
dif_in_days = dif.days
(where dif
is the difference between dates)
Upvotes: 5
Reputation: 41
The simplest way to do this is by
df["DateColumn"] = (df["DateColumn"]).dt.days
Upvotes: 3
Reputation: 7775
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.
Upvotes: 279
Reputation: 437
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')
Upvotes: 22
Reputation: 589
Timedelta objects have read-only instance attributes .days
, .seconds
, and .microseconds
.
Upvotes: 45
Reputation: 52286
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)
Upvotes: 78