0

Bascially, I have an array of datetime that I want to convert into timestamp, but I'm stuck. Below is my Time array.

Time
Out[31]: 
array([datetime.datetime(2014, 2, 1, 0, 0, 0, 100000),
       datetime.datetime(2014, 2, 1, 0, 0, 0, 300000),
       datetime.datetime(2014, 2, 1, 0, 0, 0, 500000), ...,
       datetime.datetime(2014, 2, 1, 19, 30, 0, 500000),
       datetime.datetime(2014, 2, 1, 19, 30, 0, 700000),
       datetime.datetime(2014, 2, 1, 19, 30, 0, 900000)], dtype=object)

I've tried

x = time.mktime(Time.timetuple())

but I get the error:

'numpy.ndarray' object has no attribute 'timetuple'

Any help would be greatly appreciated.

2
  • What do you think the difference is between datetime and timestamp? Commented Apr 25, 2017 at 3:20
  • Isn't timestamp in the form of seconds that has passed since 1970/01/01 ? Commented Apr 25, 2017 at 3:27

2 Answers 2

1

You could store the datetimes or the timestamps, which are just integers. Here the timestamps are calculated from the datetimes.

>>> from datetime import datetime
>>> dt = datetime(2014, 2, 1, 0, 0, 0, 100000)
>>> T = np.array([dt], dtype=np.datetime64)
>>> import time
>>> ts = int(time.mktime(dt.timetuple()))
>>> T = np.array([ts], dtype=np.int32)
Sign up to request clarification or add additional context in comments.

Comments

1

You can't use Time.timetuple() because Time is a numpy array. You can just iterate over the array though, like this:

x = [time.mktime(t.timetuple()) for t in Time]

Comments

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.