numpy genfromtxt date conversion error
Question: I am loading a file using `numpy.genfromtxt` and some of the fields are in a
date format, however, when I setup a converter to process these items I get an
error that I am not sure how to address (see below):

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

At this point, my objective is to find a simple way to load files (using
genfromtxt) and convert selected columns from date format to an integer
(epoch, for example) -- however, I am looking for any approach that will work
(not necessarily the one I have illustrated).

Any help is appreciated.

(details below)

  * Running in virtualenv
  * Using MacOS
  * Using Python v3.5

The file (abbreviated) is a simple CSV format:

    
    
    SomeField,SomeDate
    1,2013-08-16
    

The code:

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

The error:

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


Answer: The error is saying that `time.strptime` expects the first argument to be a
[`str`, not `bytes`](http://eli.thegreenplace.net/2012/01/30/the-bytesstr-
dichotomy-in-python-3):

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

So you can avoid the error by using

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

