Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas handling of numpy timedelta64[ms]

>>> import pandas as pd
>>> pd.__version__
'0.11.0'
>>> import numpy as np
>>> np.__version__
'1.7.1'
>>> d={'a':np.array([68614867, 72200835], dtype=np.dtype('timedelta64[ms]'))}
>>> d['a'][0]
numpy.timedelta64(68614867,'ms')
>>> df = pd.DataFrame.from_dict(d)
>>> print df
            a
0 00:00:00.068615
1 00:00:00.072201

It looks like it is interpreting the values in the underlying int64 as ns not ms. Is this a bug in pandas' handling of timedelta64[ms] types?

like image 719
Andy Johnson Avatar asked Nov 03 '22 22:11

Andy Johnson


1 Answers

timedelta handling is still a work-in-progress, see this issue: https://github.com/pydata/pandas/issues/3009

main issue is that timedeltas are broken in numpy 1.6.2.

passing of arbitrary timedeltas dtypes in creation is not supported yet, as a workaround, you can do this, as the ONLY dtype supported at the moment is the internal timedelta64[ns] (this is exactly how datetime64[ns]) works btw. Pandas converts to an internal repr and then you do want you want.

(this solution is ONLY good for numpy >= 1.7).

In [22]: d['a'].astype('timedelta64[ns]')
Out[22]: array([68614867000000, 72200835000000], dtype='timedelta64[ns]')

In [23]: DataFrame(dict(a = d['a'].astype('timedelta64[ns]')))
Out[23]: 
                a
0 19:03:34.867000
1 20:03:20.835000

In [24]: DataFrame(dict(a = d['a'].astype('timedelta64[ns]'))).dtypes
Out[24]: 
a    timedelta64[ns]
dtype: object

what is the final goal you are trying to accomplish?

like image 73
Jeff Avatar answered Nov 09 '22 15:11

Jeff