I have numpy ndarray which contains two columns: one is date, e.g. 2011-08-04, another one is time, e.g. 19:00:00:081.
How can I combine them into one array of datetime objects? Currently, they’re strings in numpy array.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the date and time string in the example.txt data file were given as one column with no separating whitespace, then
genfromtxtcould convert it into a datetime object like this:Given
example.txtas it is, you could form the desired numpy array withMerging two columns in a numpy array can be a slow operation especially if the array is large. That’s because merging, like resizing, requires allocating memory for a new array, and copying data from the original array to the new one. So I think it is worth trying to form the correct numpy array directly, instead of in stages (by forming a partially correct array and merging two columns).
By the way, I tested the above
csvcode versus merging two columns (below). Forming a single array fromcsv(above) was faster (and the memory usage was about the same):example4096.txtis the same asexample.txt, duplicated 4096 times. It’s about 12K lines long.