Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Using python, I've just made two strings and now want to convert them into an integer array.

My two strings are the start and end times of an earth quake and look like this

"00:39:59.946000"

"01:39:59.892652"

I want to convert these two into integer arrays so that I can use numpy.arange() or numpy.linspace(). The expected output should be an array that has a number of evenly spaced values between the start and end time. For example,

array = [00:39:59.946000, 00:49:59.946000, 00:59:59.946000, 01:09:59.946000, etc...]

I want to then use the values of this array as each increment on the x-axis of my graph. Any advice / assistance would be appreciated.

share|improve this question
    
What is the expected output? –  inspectorG4dget Aug 27 '13 at 3:24
    
The expected output of the above should be an array that has a number evenly spaced values between the start and end time. For example, array=[00:39:59.946000, 00:49:59.946000, 00:59:59.946000,01:09:59.946000,etc...] I want to then use the values of this array as each increment on the x-axis of my graph. –  Sebastian Alexander Neve White Aug 27 '13 at 3:29
    
Please update your question, not the comments. –  Droogans Aug 27 '13 at 3:30
1  
Uhh… hate to break it to you, but 00:39:59.946000 is not an int. What exactly are you after? –  inspectorG4dget Aug 27 '13 at 3:34
1  
@inspectorG4dget I think datetime is the answer here, but that's just me. –  Droogans Aug 27 '13 at 3:35

4 Answers 4

Can just you convert the timestamp to a epoch time?

share|improve this answer
    
This is probably the right approach. –  Droogans Aug 27 '13 at 3:35
>>> [int(x) for x in eq_time if x.isdigit()]
share|improve this answer
1  
You meant isdigit()? –  Sukrit Kalra Aug 27 '13 at 3:31
>>> import time
>>> t1="00:39:59.946000"
>>> t2=time.strptime(t1.split('.')[0]+':2013', '%H:%M:%S:%Y') #You probably want year as well.
>>> time.mktime(t2) #Notice that the decimal parts are gone, we need to add it back
1357018799.0
>>> time.mktime(t2)+float('.'+t1.split('.')[1]) #(add ms)
1357018799.946

#put things together:
>>> def str_time_to_float(in_str):
    return time.mktime(time.strptime(in_str.split('.')[0]+':2013', '%H:%M:%S:%Y'))\
           ++float('.'+in_str.split('.')[1])
>>> str_time_to_float("01:39:59.892652")
1357022399.892652
share|improve this answer

Since your strings represent time data, how about taking a look at time.strptime?

Something along the lines of

from datetime import datetime                                                                                                                                                                                                                                                      

t1 = datetime.strptime("2013:00:39:59.946000", "%Y:%H:%M:%S.%f")                                                                               
t2 = datetime.strptime("2013:01:39:59.892652", "%Y:%H:%M:%S.%f")
share|improve this answer
    
The part after decimal point are all gone... –  CT Zhu Aug 27 '13 at 3:48
    
Try datetime's version. Edited accordingly. –  jrs Aug 27 '13 at 3:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.