Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String formatting of timedeltas in Pandas

Tags:

python

pandas

I noticed that Pandas knows how to smartly format a timedelta object into a string.

In [1]: df[column][rows].max()
Out[1]: 
0   2 days, 02:08:07
dtype: timedelta64[ns]

When I try to do this manually I keep getting the string in nanoseconds.

In [2]: df[column][rows].max()[0]
Out[2]: numpy.timedelta64(180487000000000,'ns')

In [2]: str(df[column][rows].max()[0])
Out[2]: '180487000000000 nanoseconds'

I would rather not reinvent the wheel, so is there any way to access the string formatting method (or the string itself) that Pandas uses to show a timedelta object in x days, hh:mm:ss ?

like image 530
Amelio Vazquez-Reina Avatar asked Aug 25 '13 20:08

Amelio Vazquez-Reina


1 Answers

The function is located here:

pd.tslib.repr_timedelta64

In action:

In [11]: pd.tslib.repr_timedelta64(np.timedelta64(180487000000000,'ns'))
Out[11]: '2 days, 02:08:07'
like image 194
Andy Hayden Avatar answered Sep 30 '22 17:09

Andy Hayden