Np.timedelta64 conversions in days, weeks, months, etc.
When I calculate the difference between two pandas dates datetime64
I get np.timedelta64
. Is there an easy way to convert these deltas to representations like hours, days, weeks, etc.?
I haven't been able to find any methods in np.timedelta64
that make conversions between different units easier, but it looks like pandas seems to know how to convert those units on the days the timedeltas is printed (for example, I get: 29 days, 23:20:00
to string representations of the data). Anyone access to this feature?
Update:
Oddly enough, none of the following works:
> df['column_with_times'].days
> df['column_with_times'].apply(lambda x: x.days)
but this does:
df['column_with_times'][0].days
source to share
pandas stores the timedelta data in a numpy type timedelta64[ns]
, but it also provides a type Timedelta
to wrap this for more convenience (for example, to provide such accessories for days, hours and other components).
In [41]: timedelta_col = pd.Series(pd.timedelta_range('1 days', periods=5, freq='2 h'))
In [42]: timedelta_col
Out[42]:
0 1 days 00:00:00
1 1 days 02:00:00
2 1 days 04:00:00
3 1 days 06:00:00
4 1 days 08:00:00
dtype: timedelta64[ns]
To access the various components of a complete column (series), you must use an accessory .dt
. For example:
In [43]: timedelta_col.dt.hours
Out[43]:
0 0
1 2
2 4
3 6
4 8
dtype: int64
With, timedelta_col.dt.components
you get a frame with all the different components (from days to nanoseconds) in different columns.
When accessing one column value above, this returns Timedelta
and you don't need to use the accessory dt
, but you can access the components directly:
In [45]: timedelta_col[0]
Out[45]: Timedelta('1 days 00:00:00')
In [46]: timedelta_col[0].days
Out[46]: 1L
So, the accessory .dt
provides access to the scanner's attributes Timedelta
, but in a complete column. This is why you see what df['column_with_times'][0].days
works, but df['column_with_times'].days
not.
The reason it df['column_with_times'].apply(lambda x: x.days)
doesn't work is because apply assigns values timedelta64
(not Timedelta
pandas type ) and they don't have such attributes.
source to share