7

When I compute the difference between two pandas datetime64 dates I get np.timedelta64. Is there any easy way to convert these deltas into representations like hours, days, weeks, etc.?

I could not find any methods in np.timedelta64 that facilitate conversions between different units, but it looks like Pandas seems to know how to convert these units to days when printing timedeltas (e.g. I get: 29 days, 23:20:00 in the string representation dataframes). Any way to access this functionality ?

Update:

Strangely, none of the following work:

> df['column_with_times'].days
> df['column_with_times'].apply(lambda x: x.days)

but this one does:

df['column_with_times'][0].days
6
  • 1
    As a side note: a timedelta64 doesn't have hours, days, or weeks if its units are months, years, or generic, only if they're weeks or smaller. I assume you've already got timedelta64 values in some relevant unit (usually days, seconds, or milliseconds, or nanoseconds), so this isn't an issue for you, but it's something to be aware of.
    – abarnert
    Sep 4, 2014 at 18:12
  • 3
    Read the extensive documentation here on how to do exactly that: pandas.pydata.org/pandas-docs/stable/…
    – Jeff
    Sep 4, 2014 at 18:14
  • @Jeff: I thought he was asking how to do this directly with numpy, not with pandas… but now that I reread it, I think you may be right…
    – abarnert
    Sep 4, 2014 at 18:15
  • Thanks. Sorry I missed that @Jeff. You are totally right. Sep 4, 2014 at 18:18
  • 1
    it IS a big set of documentation :)
    – Jeff
    Sep 4, 2014 at 18:18

1 Answer 1

3

pandas stores timedelta data in the numpy timedelta64[ns] type, but also provides the Timedelta type to wrap this for more convenience (eg to provide such accessors of the 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 different components of a full column (series), you have to use the .dt accessor. 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 (days to nanoseconds) as different columns.
When accessing one value of the column above, this gives back a Timedelta, and on this you don't need to use the dt accessor, but you can access directly the components:

In [45]: timedelta_col[0]
Out[45]: Timedelta('1 days 00:00:00')

In [46]: timedelta_col[0].days
Out[46]: 1L

So the .dt accessor provides access to the attributes of the Timedelta scalar, but on the full column. That is the reason you see that df['column_with_times'][0].days works but df['column_with_times'].days not.
The reason that df['column_with_times'].apply(lambda x: x.days) does not work is that apply is given the timedelta64 values (and not the Timedelta pandas type), and these don't have such attributes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.