I am trying to efficiently read some legacy DBs contents into a numpy (rec-)array. I was following this What's the most efficient way to convert a MySQL result set to a NumPy array? and this MySQLdb query to Numpy array posts. Now it happens that some entries in the DB contains NULL, which are returned as None.
So np.fromiter will react like this e.g.
TypeError: long() argument must be a string or a number, not 'NoneType'
I would like to kind of, tell it how it should behave in case it encounters None. Is that even possible?
Here is (something like) my code:
cur = db.cursor()
query = ("SELECT a, b, c from Table;")
cur.execute(query)
dt = np.dtype([
('a', int),
('b', int),
('c', float),
])
r = np.fromiter(cur.fetchall(), count=-1, dtype=dt)
And I would like to be able to specify, that the resulting array should contain np.nan in case None is encountered in column 'c', while it should contain the number 9999 when None is found for column 'a' or 'b'. Is something like that possible?
Or is there another (beautiful) method to get MySQL DB contents into numpy arrays, in case some values are unknown?