Search code examples
pythonarraysnumpyuser-defined-typesrecarray

Specifying a numpy.datype to read GPX trackpoints


I want to represent a GPS track extracted from GPX file as a Numpy array. For that, each element will be of type "trackpoint", containing one datetime and three floats.

I am trying to do this (actually, after parsing the GPX file with some XML library):

import numpy

trkptType = numpy.dtype([('time', 'datetime64'),
                         ('lat', 'float32'),
                         ('lon', 'float32'),
                         ('elev', 'float32')])

a = numpy.array([('2014-08-08T03:03Z', '30', '51', '40'),
                 ('2014-08-08T03:03Z', '30', '51', '40')], dtype=trkptType)

But I get this error:

ValueError: Cannot create a NumPy datetime other than NaT with generic units

What am I doing wrong, and how should I create a much larger array from some list in an efficient manner?


Solution

  • As explained here, you have to use at least datetime64[m] (minutes), instead of datetime64, then your code will work. You could also use a datetime that goes down to seconds or miliseconds, such as datetime64[s] or datetime64[ms].

    trkptType = np.dtype([('time', 'datetime64[m]'),
                          ('lat', 'float32'),
                          ('lon', 'float32'),
                          ('elev', 'float32')])