numpy genfromtxt日期转换错误

时间:2015-11-19 01:29:30

标签: python-3.x numpy

我正在使用numpy.genfromtxt加载文件并且某些字段采用日期格式,但是,当我设置转换器来处理这些项目时,我收到错误,我不知道如何解决(请参阅下文):

strptime() argument 0 must be str, not <class 'bytes'>

此时,我的目标是找到一种简单的方法来加载文件(使用genfromtxt)并将所选列从日期格式转换为整数(例如,epoch) - 但是,我正在寻找任何方法工作(不一定是我所说的)。

感谢任何帮助。

(详情如下)

  • 在virtualenv中运行
  • 使用MacOS
  • 使用Python v3.5

文件(缩写)是一种简单的CSV格式:

SomeField,SomeDate
1,2013-08-16

代码:

import numpy as np
import time

def main():
    pathRaw = 'data/homesite-quote-conversion/temp.csv'
    str2epoch = lambda x: time.mktime(time.strptime(x, '%Y-%m-%d'))
    converters = {1:str2epoch}
    dataset = np.genfromtxt(open(pathRaw,'rb'), converters=converters, dtype=None, delimiter=',', skip_header=1)
    return

if __name__ == '__main__':
    main()

错误:

Traceback (most recent call last):
File "test.py", line 12, in <module>
  main()
File "test.py", line 8, in main
  dataset = np.genfromtxt(open(pathRaw,'rb'), converters=converters, dtype=None, delimiter=',', skip_header=1)
File "{user-directory}/.virtualenvs/demo/lib/python3.5/site-packages/numpy/lib/npyio.py", line 1731, in genfromtxt
  converter.iterupgrade(current_column)
File "{user-directory}/.virtualenvs/demo/lib/python3.5/site-packages/numpy/lib/_iotools.py", line 766, in iterupgrade
_ strict_call(_m)
File "/{user-directory}/.virtualenvs/demo/lib/python3.5/site-packages/numpy/lib/_iotools.py", line 688, in _strict_call
  new_value = self.func(value)
File "test.py", line 6, in <lambda>
  str2epoch = lambda x: time.mktime(time.strptime(x, '%Y-%m-%d'))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/_strptime.py", line 494, in _strptime_time
  tt = _strptime(data_string, format)[0]
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/_strptime.py", line 306, in _strptime
  raise TypeError(msg.format(index, type(arg)))
TypeError: strptime() argument 0 must be str, not <class 'bytes'>

1 个答案:

答案 0 :(得分:0)

错误是time.strptime期望第一个参数为str, not bytes

>>> x = b'2013-08-16'    # x is bytes
>>> import time
>>> time.strptime(x, '%Y-%m-%d')
TypeError: strptime() argument 0 must be str, not <class 'bytes'>
>>> time.strptime(x.decode('ascii'), '%Y-%m-%d')   # x.decode('ascii') is a str
time.struct_time(tm_year=2013, tm_mon=8, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=228, tm_isdst=-1)

因此,您可以使用

来避免错误
def str2epoch(x):
    return time.mktime(time.strptime(x.decode('ascii'), '%Y-%m-%d'))