Python numpy loadtxt失败,并带有日期时间 [英] Python numpy loadtxt fails with date time

查看:353
本文介绍了Python numpy loadtxt失败,并带有日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用numpy loadtxt将csv文件加载到数组中。但是看来我无法正确加载日期时间。

I am trying to use numpy loadtxt to load a csv file into an array. But it seem i can't get the date time correctly loaded.

下面的示例演示了发生了什么。我做错了吗?

Below demonstrates what is happening. Did I do something wrong?

>>> s = StringIO("05/21/2007,03:27")
>>> np.loadtxt(s, delimiter=",", dtype={'names':('date','time'), 'formats':('datetime64[D]', 'datetime64[m]')})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/npyio.py", line 796, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/npyio.py", line 573, in <lambda>
  return lambda x: int(float(x))
ValueError: invalid literal for float(): 05/21/2007


推荐答案

还需要添加转换器,例如:

You need to also add converters, like:

from matplotlib.dates import strpdate2num
...
np.loadtxt(s, delimiter=",", converters={0:strpdate2num('%m/%d/%Y'), 1:...}, dtype= ...

当numpy看到您的dtype格式时of datetime [64],它准备输出类型为numpy.datetime64的列。numpy.datetim64是numpy.integer的子类,并且loadtxt准备将该列作为整数处理,并带有以下内容:

When numpy sees your dtype format of datetime[64], it prepares to output a column of type numpy.datetime64. numpy.datetim64 is a subclass of numpy.integer, and loadtxt prepares to deal with that column as an integer with the following:

def _getconv(dtype):
    typ = dtype.type
    if issubclass(typ, np.bool_):
        return lambda x: bool(int(x))
    if issubclass(typ, np.uint64):
        return np.uint64
    if issubclass(typ, np.int64):
        return np.int64
    if issubclass(typ, np.integer):
        return lambda x: int(float(x))

    ...

何时到尝试在numpyio的第796行进行转换的点:

When it gets to the point of attempting conversion at line 796 in numpyio:

items = [conv(val) for (conv, val) in zip(converters, vals)]

它尝试使用 lambda x:int (float(x))处理输入。这样做时,它会尝试将您的日期(05/27/2007)转换为浮动日期,然后逐渐减少。上面的转换函数strpdate2num会将日期转换为数字表示形式。

it tries to uselambda x: int(float(x)) to handle the input. When it does that, it tries to cast your date (05/27/2007) to a float and peters out. The conversion function strpdate2num above will convert the date to a number representation.

这篇关于Python numpy loadtxt失败,并带有日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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