Python Numpy Loadtxt - convert unix timestamp

I have a text file with many lines of data - the first piece of data on each line is a unix timestamp such as 1436472000

. I am using numpy.loadtxt

, and in the options for the converters, I want to specify for it to convert the timestamp to what numpy understands as date time. I know this should come after 0:

the curly braces, but I can't figure out how to convert it. I know the converter can be used from matplotlib.dates.strpdate2num

for normal dates, but I won't work for unix timestamps.

code:

timestamp, closep, highp, lowp, openp, volume = np.loadtxt(fileName,delimiter=",",unpack=True,converters={ 0: })

      

Thanks for the help in advance, please ask if you would like me to clarify what I mean.

+3


source to share


1 answer


While converters can be handy, they are slow because they are called once for each line of data. Fast data conversion after timestamps are loaded into a NumPy array of integers:

x = np.array([1436472000, 1436472001])
x = np.asarray(x, dtype='datetime64[s]')

      

outputs a NumPy datetime64

s
array :

array(['2015-07-09T16:00:00-0400', '2015-07-09T16:00:01-0400'],
       dtype='datetime64[s]')

      

To get Python datetime.datetime

use tolist()

:



>>> x.tolist()
# [datetime.datetime(2015, 7, 9, 20, 0), 
#  datetime.datetime(2015, 7, 9, 20, 0, 1)]

      


As you know matplotlib data counts the number of days from 0001-01-01 00:00:00 UTC, plus one. These are not timestamps (which count seconds from Epoch time, 1970-01-01 00:00:00 UTC):

>>> matplotlib.dates.date2num(x.tolist())
# array([ 735788.83333333,  735788.83334491])

      

+5


source







All Articles