[Tutor] Datetime Integer Array

Jeremy Traurig jeremy.traurig at gmail.com
Mon May 21 22:04:26 CEST 2012


Hello,

I am reading a data file with a string time stamp as the first column,
example below:

'03/10/2010 02:00:00'
'03/10/2010 02:10:00'
'03/10/2010 02:20:00'
'03/10/2010 02:30:00'
etc to n number of rows.

I'm using the numpy function genfromtxt to read this data:

import numpy as np
datetime_IN = np.genfromtxt('SIL633_original.txt', delimiter='\t',
skip_header=141, dtype='|S19',
		usecols=0)

Now I have a variable called datetime_IN which is an array of datetime
strings to the nth row. I'd like to convert this strings to a numpy
array of integers where each column represents a value of time.
For example, using the same values above, i want an array of integers
to look like this:

3,10,2010,2,0,0
3,10,2010,2,10,0
3,10,2010,2,20,0
3,10,2010,2,30,0
etc to n number of rows.

I have already tried creating a numpy array of integers using this code:

import time
time_format = %m/%d/%Y %H:%M:%S
for x in range(len(datetime_IN)):
    junk = time.strptime(datetime[x],time_format)
    junk2 = [y for y in junk]

The above code works in general but it doesn't create an array with
the same number of rows as datetime_IN, and I understand it doesn't
because the previous data in junk2 is lost. I'd like to build the
junk2 array but I'm not sure how. In other languages, I'm able to
iterate over an array to build it, so in each iteration of a loop a
new row is created for the arrray I'm building. I'm not quite sure how
to do that in python.

thanks -- jt


More information about the Tutor mailing list