1

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.

6
  • What is the expected output? Commented Aug 27, 2013 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. Commented Aug 27, 2013 at 3:29
  • Please update your question, not the comments. Commented Aug 27, 2013 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? Commented Aug 27, 2013 at 3:34
  • 1
    @inspectorG4dget I think datetime is the answer here, but that's just me. Commented Aug 27, 2013 at 3:35

4 Answers 4

1
>>> [int(x) for x in eq_time if x.isdigit()]
Sign up to request clarification or add additional context in comments.

1 Comment

You meant isdigit()?
1

Can just you convert the timestamp to a epoch time?

Comments

0
>>> 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

Comments

0

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")

1 Comment

Try datetime's version. Edited accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.