numpy timedelta64 [ms]的 pandas 处理 [英] pandas handling of numpy timedelta64[ms]

查看:122
本文介绍了numpy timedelta64 [ms]的 pandas 处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>>> 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

似乎正在将基础int64中的值解释为ns而不是ms.这是熊猫处理timedelta64[ms]类型的错误吗?

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?

推荐答案

timedelta处理仍在进行中,请参见以下问题:

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

主要问题是在numpy 1.6.2中打破了时间增量.

main issue is that timedeltas are broken in numpy 1.6.2.

在创建过程中传递任意timedeltas dtypes,因为 一种解决方法,您可以执行此操作,因为当前支持的唯一dtype是 内部timedelta64 [ns](这正是datetime64 [ns]的工作方式)顺便说一句.大熊猫 转换为内部代表,然后您确实想要.

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.

(此解决方案仅适用于numpy> = 1.7).

(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?

这篇关于numpy timedelta64 [ms]的 pandas 处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆