I'm trying to import a simple CSV file with Numpy genfromtxt but can't manage to convert the data of first column to dates.
Here is my code:
import numpy as np
from datetime import datetime
str2date = lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
data = np.genfromtxt('C:\\\\data.csv',dtype=None,names=True, delimiter=',', converters = {0: str2date})
I get the following error in str2date:
TypeError: must be str, not bytes
The problem is there are many columns, so I'd prefer avoiding the specification of all the column types (which are basically numerical).
The problem is that the argument passed to str2date
is of this form b'%Y-%m-%d %H:%M:%S'
. These are bytes, which rightfully cannot be parsed to a datetime object. The solution to that problem is quite simple though, as you should decode your byte string to a UTF-8
string:
str2date = lambda x: datetime.strptime(x.decode("utf-8"), '%Y-%m-%d %H:%M:%S')